how to load a image in picture dynamically and draw a line o

来自中国的朋友,欢迎您在本版面使用中文讨论问题。请注意,如果您想得到不懂中文的人的帮助,请同时提供英文译文。
Help and discussions in Chinese.
Post Reply
swordrong
Posts: 4
Joined: Sun May 29, 2011 10:58 am

how to load a image in picture dynamically and draw a line o

Post by swordrong »

dear sir:
how to load a image in picture dynamically and draw a line on image?
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: how to load a image in picture dynamically and draw a li

Post by jerome »

Hi there!

First, what do you mean by 'loading an image dynamically'?

There are two constructs in Ecere for loading bitmaps.

The lowest level is the 'Bitmap' class. You can instantiate a bitmap, then invoke the 'Load' method on it, specifying a filename. You can also specify a 'displaySystem' (e.g. obtained from Window::displaySystem) to make the bitmap device dependent (for better performance). As part of an Ecere application, this is usually done from within the Window::OnLoadGraphics method. (and similarly, the bitmap is freed using Bitmap::Free in Window::OnUnloadGraphics). This system ensures that the bitmap will properly adapt to switching graphics mode, even graphics driver (e.g. 16bit/32bit/OpenGL/Direct3D/etc.).

Because this can seem a bit cumbersome, the BitmapResource class can be used to simplify the process. Please take a look at these samples. BitmapResource will automatically load and unload bitmaps for you. They also ensure that bitmaps get reused in multiple instances of a Window with compatible displays.

If you want to build your own bitmap, or directly access the bitmap memory, you need to use the low level Bitmap class. With Bitmap::Allocate for example you can allocate memory for a bitmap.

Sample code to load a bitmap resource from the project resources and draw a blue line on top:

Code: Select all

import "ecere"
 
class MainWindow : Window
{
   text = "Bitmap Demo";
   background = black;
   borderStyle = sizable;
   hasMaximize = true;
   hasMinimize = true;
   hasClose = true;
   size = { 576, 432 };
 
   BitmapResource castleGfx { ":castle.png", window = this };
 
   void OnRedraw(Surface surface)
   {
      Bitmap bitmap = castleGfx.bitmap;
 
      if(bitmap)
         surface.Blit(bitmap, 100, 100, 0,0, bitmap.width, bitmap.height);
      surface.foreground = blue;
      surface.DrawLine(110, 110, 130, 140);
   }
}
 
MainWindow mainWindow {};
swordrong
Posts: 4
Joined: Sun May 29, 2011 10:58 am

Re: how to load a image in picture dynamically and draw a li

Post by swordrong »

非常感谢jerome,但是您的回答中有一个小bug. 应为surface.SetForeground。这个功能我已经实现过了,我想通过文件菜单打开一个图像文件,在picture控件中显示,但没成功,再次感谢jerome.
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: how to load a image in picture dynamically and draw a li

Post by jerome »

Which version of Ecere are you using?

If you are using a relatively recent build (a preview of 0.44), it will include the 'foreground' property so you can simply do 'surface.foreground ='.

The Picture control is typically used to insert some small images as part of your GUI layout.
However you can use it this way as well. Were you trying to do something like this?

Code: Select all

import "ecere"
 
class Form1 : Window
{
   text = "Form1";
   background = activeBorder;
   borderStyle = sizable;
   hasMaximize = true;
   hasMinimize = true;
   hasClose = true;
   size = { 640, 480 };
   hasMenuBar = true;
 
   Picture picture1 { this };
 
   Menu fileMenu { menu, "File", f };
   MenuItem fileOpen
   {
      fileMenu, "Open", o, ctrlO;
      bool NotifySelect(MenuItem selection, Modifiers mods)
      {
         FileDialog dialog { type = open };
         if(dialog.Modal() == ok)
            picture1.image = { dialog.filePath };
         return true;
      }
   };
}
 
Form1 form1 {};
swordrong
Posts: 4
Joined: Sun May 29, 2011 10:58 am

Re: how to load a image in picture dynamically and draw a li

Post by swordrong »

thanks a lot.
Post Reply