Recent

Author Topic: [SOLVED] Bitmap showing up as on black  (Read 3552 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
[SOLVED] Bitmap showing up as on black
« on: October 10, 2017, 12:53:52 pm »
hi All,
i started working with bitmaps and got into issues at the code below.
basically i loaded some images into a images list and want to push to a TImage a stretched version so it fits the TImage dimensions.
please advise what i can do so buffer2 saves as original image stretched but not on black (like film negative).

Code: Pascal  [Select][+][-]
  1. unit main;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     Button1: TButton;
  13.     Image1: TImage;
  14.     ImageList1: TImageList;
  15.     procedure Button1Click(Sender: TObject);
  16.   end;
  17.  
  18. var
  19.   Form1: TForm1;
  20.  
  21. implementation
  22.  
  23. {$R *.lfm}
  24.  
  25. procedure TForm1.Button1Click(Sender: TObject);
  26. var
  27.   buffer1: TBitmap;
  28.   buffer2: TBitmap;
  29.  
  30. begin
  31.   try
  32.     buffer1 := TBitmap.Create;
  33.     buffer2 := TBitmap.Create;
  34.  
  35.     ImageList1.GetBitmap(0, buffer1);
  36.     buffer1.SaveToFile('buff1.bmp');
  37.  
  38.     buffer2.SetSize(Image1.Width, Image1.Height);
  39.     buffer2.Canvas.StretchDraw(Rect(0, 0, Image1.Width, Image1.Height), buffer1);
  40.     buffer2.SaveToFile('buff2.bmp');
  41.  
  42.     Image1.Canvas.Clear;
  43.     Image1.Canvas.Draw(0, 0, buffer2);
  44.  
  45.   finally
  46.     FreeAndNil(buffer1);
  47.     FreeAndNil(buffer2);
  48.   end;
  49. end;
  50.  
  51. end.
  52.  

thank you

Lazarus 1.8RC4, cross platform
« Last Edit: October 10, 2017, 04:31:20 pm by tudi_x »
Lazarus 2.0.2 64b on Debian LXDE 10

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Bitmap showing up as on black
« Reply #1 on: October 10, 2017, 01:42:29 pm »
You could try this

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   bmp: TBitmap;
  4. begin
  5.   bmp:=TBitmap.Create;
  6.   try
  7.     bmp.SetSize(ImageList.Height, ImageList.Height);
  8.     bmp.Canvas.Brush.Color:=clBtnFace; // or whatever background color you wish
  9.     bmp.Canvas.FillRect(bmp.Canvas.ClipRect);
  10.     ImageList.Draw(bmp.Canvas, 0, 0, 0);
  11.     Image1.Canvas.StretchDraw(Image1.ClientRect, bmp);
  12.   finally
  13.     bmp.Free;
  14.   end;
  15. end;

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Bitmap showing up as on black
« Reply #2 on: October 10, 2017, 04:31:03 pm »
thank you again.
now i understood that i was missing a background to the bitmap.
below now works:

Code: Pascal  [Select][+][-]
  1.     buffer2.SetSize(Image1.Width, Image1.Height);
  2.     buffer2.Canvas.Brush.Color:= clBtnFace;
  3.     buffer2.Canvas.FillRect(buffer2.Canvas.ClipRect);
  4.     buffer2.Canvas.StretchDraw(Rect(0, 0, Image1.Width, Image1.Height), buffer1);
  5.     buffer2.SaveToFile('buff2.bmp');
Lazarus 2.0.2 64b on Debian LXDE 10

 

TinyPortal © 2005-2018