RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TBitmap.Assign Method

Copies a new bitmap image to the bitmap object.

Pascal
procedure Assign(Source: TPersistent); override;
C++
virtual __fastcall Assign(TPersistent * Source);

Assign copies the bitmap image contained in Source to the bitmap object. If Source is not a bitmap, Assign calls the inherited Assign method, which can copy an image from any class that knows how to copy to a TBitmap object. If the bitmap needs to be changed, the actual bitmap image is copied before the changes are made (copy on write).

Note: An object of one type can always be assigned to another object of the same type. Also, the Source can be of type TPicture if the Graphic property of the picture is a bitmap.
 

C++ Examples: 

 

/*
This example uses a button on a form and creates two bitmaps
dynamically.  One bitmap is monchrome, which means all
non-white colors become black.  The bitmap file path is
relative to the Debug directory.
Note: For the C++ Builder, capitalization matters for the file
name and path.
*/

#include <memory>       //for STL auto_ptr class

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  std::auto_ptr<Graphics::TBitmap> Bitmap1(new Graphics::TBitmap());
  std::auto_ptr<Graphics::TBitmap> Bitmap2(new Graphics::TBitmap());
  try
  {
    Bitmap1->LoadFromFile("..\\FACTORY.BMP");
    Bitmap2->Assign(Bitmap1.get());     // Copy Bitmap1 into Bitmap2
    Bitmap2->Dormant();           // Free up GDI resources
    Bitmap2->FreeImage();         // Free up Memory.
    Canvas->Draw(20, 20, Bitmap2.get());  // Note that previous calls don't lose the image
    Bitmap2->Monochrome = true;
    Canvas->Draw(80, 80, Bitmap2.get());
    Bitmap2->ReleaseHandle();       // This will actually lose the bitmap;
  }
  catch (...)
  {
    MessageBeep(0);
  }
}

 

Delphi Examples: 

{
This example uses a button on a form and creates two bitmaps
dynamically.  One bitmap is monchrome, which means all
non-white colors become black.  The bitmap file path is
relative to the Debug directory.
}
procedure TForm1.Button1Click(Sender: TObject);
var
 BitMap1,BitMap2 : TBitMap;
 MyFormat : Word;
begin
   BitMap2 := TBitMap.Create;
   BitMap1 := TBitMap.Create;
try
   BitMap1.LoadFromFile('factory.bmp');
   BitMap2.Assign(BitMap1);     // Copy BitMap1 into BitMap2
   BitMap2.Dormant;             // Free up GDI resources
   BitMap2.FreeImage;           // Free up Memory.
   Canvas.Draw(20,20,BitMap2);  // Note that previous calls don't lose the image
   BitMap2.Monochrome := true;
   Canvas.Draw(80,80,BitMap2);
   BitMap2.ReleaseHandle;       // This will actually lose the bitmap;
 finally
   BitMap1.Free;
   BitMap2.Free;
 end;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!