Programming C++Builder How to Set the Initial Bitmap Size and Draw on the Bitmap?
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/codeback.net/plugins/content/tags.php on line 149

How to set the initial bitmap size?

When you place an image control, it is simply a container. However, you can set the image control's Picture property at design time to contain a static graphic. The control can also load its picture from a file at runtime, as described in .

To create a blank bitmap when the application starts,

  1. Attach a handler to the OnCreate event for the form that contains the image.
  2. Create a bitmap object, and assign it to the image control's Picture->Graphic property.

In this example, the image is in the application's main form, Form1, so the code attaches a handler to Form1's OnCreate event:

void __fastcallTForm1::FormCreate(TObject *Sender)
{
  Graphics::TBitmap *Bitmap = new Graphics::TBitmap();   // create the bitmap object
  Bitmap->Width = 200;               // assign the initial width...
  Bitmap->Height = 200;              // ...and the initial height
  Image->Picture->Graphic = Bitmap;  // assign the bitmap to the image control
  delete Bitmap;                     // free the bitmap object
}

Assigning the bitmap to the picture's Graphic property copies the bitmap to the picture object. However, the picture object does not take ownership of the bitmap, so after making the assignment, you must free it.

If you run the application now, you see that client area of the form has a white region, representing the bitmap. If you size the window so that the client area cannot display the entire image, you'll see that the scroll box automatically shows scroll bars to allow display of the rest of the image. But if you try to draw on the image, you don't get any graphics, because the application is still drawing on the form, which is now behind the image and the scroll box.

How to draw on the bitmap?

To draw on a bitmap, use the image control's canvas and attach the mouse-event handlers to the appropriate events in the image control. Typically, you would use region operations (fills, rectangles, polylines, and so on). These are fast and efficient methods of drawing.

An efficient way to draw images when you need to access individual pixels is to use the bitmap ScanLine property. For general-purpose usage, you can set up the bitmap pixel format to 24 bits and then treat the pointer returned from ScanLine as an array of RGB. Otherwise, you will need to know the native format of the ScanLine property. This example shows how to use ScanLine to get pixels one line at a time.

void __fastcallTForm1::Button1Click(TObject *Sender)
{
  Graphics::TBitmap *pBitmap = new Graphics::TBitmap();
// This example shows drawing directly to the Bitmap
  Byte *ptr;
  try
  {
    pBitmap->LoadFromFile("C:\\Program Files\\Borland\\C++Builder\\Images\\Splash\\256color\\factory.bmp ");
   for (int y = 0; y Height; y++)
    {
      ptr = pBitmap->ScanLine[y];
      for (int x = 0; x Width; x++)
        ptr[x] = (Byte)y;
    }
    Canvas->Draw(0,0,pBitmap);
  }

  catch (...)
  {
    ShowMessage("Could not load or alter bitmap");
  }
  delete pBitmap;
}

CLX: For cross-platform applications, change Windows- and VCL-specific code so that your application can run on Linux. For example, the pathnames in Linux use a forward slash / as a delimiter. For more information on CLX and cross-platform applications, see porting your application.


Add comment


Security code
Refresh

Programming - C++Builder