Graphics

Create bmp with different PixelFormat

Posted in Graphics

Use Image1.Picture.Bitmap.PixelFormat:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Image1.Picture.Bitmap.LoadFromFile('factory.bmp');
  case ComboBox1.ItemIndex of
    1: Image1.Picture.Bitmap.PixelFormat:=pf1bit;
    2: Image1.Picture.Bitmap.PixelFormat:=pf8bit;
    3: Image1.Picture.Bitmap.PixelFormat:=pf16bit;
    4: Image1.Picture.Bitmap.PixelFormat:=pf24bit;
  end;
  Image1.Picture.Bitmap.SaveToFile('factory2.bmp');
end;

Copy part of one image to another

Posted in Graphics

You may use CopyRect function for copying part of one canvas to another.

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    Image1.Picture.LoadFromFile(OpenDialog1.FileName);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Image2.Canvas.CopyRect(
    Rect(0,0,Image2.Width,Image2.Height),
    Image1.Canvas,
    Rect(
      0,
      0,
      Image1.Picture.Width-50,
      Image1.Picture.Height-50));
end;

Change bitmap at runtime

Posted in Graphics

The simplest way to change bitmap at runtime is to use Assign method. Keep in mind, that when a property is an object, it has memory associated with it. The memory associated with the old value has to be freed, the new memory has to be allocated.

procedure TForm1.Button1Click(Sender: TObject);
var 
  Image: TBitmap;
begin
  Image:=TBitmap.Create;
  if N<ImageList1.Count then ImageList1.GetBitmap(N,Image);
  BitBtn1.Glyph.Assign(Image);
  Inc(N);
  if N>ImageList1.Count then N:=0;
  Image.Free;
end;