Recent

Author Topic: Problem with StretchDraw()  (Read 2142 times)

shyub

  • Full Member
  • ***
  • Posts: 142
Problem with StretchDraw()
« on: October 05, 2023, 05:47:23 am »
You need to open the file with the picture:
Code: Pascal  [Select][+][-]
  1. Bmp.LoadFromFile(OpenPictureDialog1.FileName);
Then scale:
Code: Pascal  [Select][+][-]
  1. MyBmp:=TBitmap.Create;
  2. MyBmp.Width:=100;
  3. MyBmp.Height:=100;
  4. MyBmp.Canvas.StretchDraw(Rect(0, 0, MyBmp.Width, MyBmp.Height), Bmp);
However, after MyBMP is displayed on the screen, only a white background is obtained. (Output from Bmp - works without scaling.)

paweld

  • Hero Member
  • *****
  • Posts: 1278
Re: Problem with StretchDraw()
« Reply #1 on: October 05, 2023, 07:09:31 am »
What system, version of lazarus and fpc? It would be best to include an example showing the problem.
There is no such problem on the current version.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   bmp, bmpscale: TBitmap;
  4. begin
  5.   if not OpenDialog1.Execute then
  6.     exit;
  7.   Invalidate;
  8.   bmp := TBitmap.Create;
  9.   bmp.LoadFromFile(OpenDialog1.FileName);
  10.   bmpscale := TBitmap.Create;
  11.   bmpscale.Width := 100;
  12.   bmpscale.Height := 100;
  13.   bmpscale.Canvas.StretchDraw(Rect(0, 0, bmpscale.Width, bmpscale.Height), bmp);
  14.   Canvas.Draw(10, 50, bmpscale);
  15.   Canvas.Draw(10 + bmpscale.Width + 20, 50, bmp);
  16.   bmp.Free;
  17.   bmpscale.Free;
  18. end;  
  19.  
Best regards / Pozdrawiam
paweld

shyub

  • Full Member
  • ***
  • Posts: 142
Re: Problem with StretchDraw()
« Reply #2 on: October 05, 2023, 01:22:28 pm »
Windows-10 x64, Lazarus 2.0.12.
Problem still exists.

Josh

  • Hero Member
  • *****
  • Posts: 1350
Re: Problem with StretchDraw()
« Reply #3 on: October 05, 2023, 02:17:52 pm »
Hi

Tbitmap has very limited support of image filetypes.
TPicture has much better support.

Also note your drawing straight to form canvas, this is not persitant, any form repainting and the imagewill vanish.

IIRC When a bitmap is created with width, height its filled with $FF ie white, delphi fills with $00


Code: Pascal  [Select][+][-]
  1. var myPic:TPicture;
  2.     myBMP:Tbitmap;
  3. begin
  4.   try
  5.     myPic := TPicture.Create;
  6.     myPic.LoadFromFile(OpenDialog1.FileName);
  7.     try
  8.       myBMP := TBitmap.Create;
  9.       myBMP.Width := 100;
  10.       myBMP.Height :=100;
  11.       myBMP.Canvas.StretchDraw(Rect(0, 0, myBMP.Width, myBMP.Height), myPic.Graphic);
  12.       Canvas.Draw(10, 50, myBMP);
  13.       Canvas.Draw(10 + myBMP.Width + 20, 50, myPic.Graphic);
  14.     finally
  15.       myBMP.Free;
  16.     end;
  17.   finally
  18.     myPic.Free;
  19.   end;
  20. end;
« Last Edit: October 05, 2023, 02:26:24 pm by Josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

paweld

  • Hero Member
  • *****
  • Posts: 1278
Re: Problem with StretchDraw()
« Reply #4 on: October 05, 2023, 02:45:58 pm »
I checked on version 2.0.12 and it is also ok.
But maybe you have a problem somewhere else - as @Josh writes such way of drawing on the form is non-permanent, i.e. every time you refresh the form the canvas will be cleared.
If you want to have "permanent" drawing on the form then use the OnPaint event.

i.e:
Code: Pascal  [Select][+][-]
  1. var
  2.   bmp, bmpscale: TBitmap;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. begin
  6.   if not OpenDialog1.Execute then
  7.     exit;    
  8.   bmp.LoadFromFile(OpenDialog1.FileName);
  9.   bmpscale.Canvas.StretchDraw(Rect(0, 0, bmpscale.Width, bmpscale.Height), bmp);
  10.   Invalidate;
  11. end;
  12.  
  13. procedure TForm1.FormCreate(Sender: TObject);
  14. begin
  15.   bmp := TBitmap.Create;
  16.   bmpscale := TBitmap.Create;
  17.   bmpscale.Width := 100;
  18.   bmpscale.Height := 100;
  19. end;
  20.  
  21. procedure TForm1.FormDestroy(Sender: TObject);
  22. begin
  23.   bmp.Free;
  24.   bmpscale.Free;
  25. end;
  26.  
  27. procedure TForm1.FormPaint(Sender: TObject);
  28. begin
  29.   if bmp.Width > 0 then
  30.   begin;
  31.     Canvas.Draw(10, 50, bmpscale);
  32.     Canvas.Draw(10 + bmpscale.Width + 20, 50, bmp);
  33.   end;
  34. end;            
Best regards / Pozdrawiam
paweld

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Problem with StretchDraw()
« Reply #5 on: October 05, 2023, 05:12:43 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   bmp: TBitmap;
  4. begin
  5.   if OpenPictureDialog1.Execute then
  6.     begin
  7.       bmp := TBitmap.Create;
  8.       try
  9.         bmp.LoadFromFile(OpenPictureDialog1.FileName);
  10.         Image1.Proportional := False;
  11.         Image1.Stretch := False;
  12.         Image1.Picture.Bitmap.Assign(bmp);
  13.         Image1.Proportional := True;
  14.         Image1.Stretch := True;
  15.       finally
  16.         bmp.Free;
  17.       end;
  18.     end;
  19. end;
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

shyub

  • Full Member
  • ***
  • Posts: 142
Re: Problem with StretchDraw()
« Reply #6 on: October 06, 2023, 09:24:40 am »
The problem was solved this way:
Code: Pascal  [Select][+][-]
  1. MyBmp.Canvas.StretchDraw(Classes.Rect(0, 0, MyBmp.Width, MyBmp.Height), Bmp);

 

TinyPortal © 2005-2018