Recent

Author Topic: [SOLVED] TImage - Transparency - PNG - Doesn't Work When Zoomed In/Out  (Read 1799 times)

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Hi,

I can use a TImage to load a transparent PNG by setting the Transparent flag to True
But, if I create a Bitmap and re-assign it, then the zoom in/out... transparency is lost.
And it create some memory leak issues even though I am freeing the bitmap

Code: Pascal  [Select][+][-]
  1.  
  2. ... none essential code ...
  3.  
  4. else if (panExt.Caption = '.bmp') or (panExt.Caption = '.jpg') or (panExt.Caption = '.png') or (panExt.Caption = '.tif') or (panExt.Caption = '.tiff') Then
  5.       begin
  6.          if (imgPic.Width<=50) or (imgPic.height<=50) Then
  7.             begin
  8.                Exit;
  9.             end;
  10.  
  11.         {ZOOM-IN-enlarge}
  12.         { Calculate Zoom factor }
  13.         FZoomFactor := FZoomFactor *0.9;
  14.  
  15.         { set the new image.picture }
  16.         if abs(FZoomFactor - 1.0) < 1e-2 then
  17.            begin
  18.               { Is it close enough to 1?  Then reset to one ...    }
  19.               FZoomFactor := 1.0;
  20.               imgPic.Picture.Assign(TheImage);
  21.            end
  22.         else
  23.            begin {Otherwise, create and assign the new picture}
  24.               TempBmp := TBitmap.Create;
  25.               try
  26.                 TempBmp.PixelFormat := pf24bit;
  27.                 TempBmp.Width := Trunc(TheImage.Width * FZoomFactor);
  28.                 TempBmp.Height := Trunc(TheImage.Height * FZoomFactor);
  29.                 Dest := Rect(1, 1, TempBmp.Width, TempBmp.Height);
  30.                 TempBmp.Canvas.StretchDraw(Dest, TheImage.Graphic);
  31.                 imgPic.Picture.Assign(TempBmp);
  32.               finally
  33.                 TempBmp.Free
  34.               end
  35.            end;
  36.       end
  37.  
  38.  
  39.  
  40.  
  41. procedure TForm2.FormDestroy(Sender: TObject);
  42. begin
  43.   TheImage.Free;
  44. end;  
  45.  
  46.  



Here is code and image
« Last Edit: July 16, 2019, 12:28:39 am by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: TImage - Transparency - PNG - Doesn't Work When Zoomed In/Out
« Reply #1 on: July 15, 2019, 05:08:22 pm »
And.... as stated in another thread...

Quote
Well... I tried to just use the TImage and got it figured out.
However, the anti-alias is awful.

Is there a way to make it look nicer when zooming in/out
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: TImage - Transparency - PNG - Doesn't Work When Zoomed In/Out
« Reply #2 on: July 15, 2019, 05:39:19 pm »
Well... no response, so I decide to just manfully resize the control and turned on stretch.
That work... and it fixed the memory leak.


No if I can just apply antialias on the image to make it look nice when zoomin-in/out
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: TImage - Transparency - PNG - Doesn't Work When Zoomed In/Out
« Reply #3 on: July 15, 2019, 05:50:03 pm »
Well... no response
Getting impatient already half an hour after posting? You can get an answer within this time here, but this is not the rule. What if the person who can easily answer your question lives on the other side of the earth and is asleep?

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: TImage - Transparency - PNG - Doesn't Work When Zoomed In/Out
« Reply #4 on: July 15, 2019, 07:45:49 pm »
The "Transparent" property of TBitmap is a ancient feature from the last century.  A certain color of the image is made transparent by not painting it. Instead, use the Alpha channel for "true" transparency which makes the background partially shine through. png is the image format which is well-prepared for this feature.

I am attaching a demo with a transparent png image. You can move the image by dragging it with the left mouse button, and you can zoom by means of the mouse wheel. Instead of a TImage I am painting to a TPaintbox because it gives me more control of what is happening. But TImage should work in principle similarly.

There are two options:
  • Original bitmap: stretches the original image onto the painting canvas. Moving it with the mouse is a bit sluggish because StretchDraw is executed with every pixel moved, and this is an expensive operation
  • Stretched bitmap: Does not paint the original image but a stretched bitmap created only when needed. The stretchdraw operation uses a routine from the Lazarus wiki (https://wiki.freepascal.org/Developing_with_Graphics#Using_the_non-native_StretchDraw_from_LazCanvas) which retains the alpha channel. Interpolation is made with the TFPSharpInterpolation, but there are others in unit FPCanvas, see https://wiki.freepascal.org/fcl-image. Since the current procedure stretches the entire image calculation will become noticably slow at high zoom levels - this could be improved by processing only the part of the original image which is visible.
Of course, you could also use the BGRABitmap or Graphics32 libraries instead which are optimized for high performance. You can find them easily using the Online-Package-Manager of Lazarus.

[P.S.]
I could not include the image displayed because the zip file would have become too large. You can download it from http://pngimg.com/download/1156 (or simply use one of your own images and adjust the file name in FormCreate.

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: TImage - Transparency - PNG - Doesn't Work When Zoomed In/Out
« Reply #5 on: July 15, 2019, 11:31:42 pm »
The "Transparent" property of TBitmap is a ancient feature from the last century.  A certain color of the image is made transparent by not painting it. Instead, use the Alpha channel for "true" transparency which makes the background partially shine through. png is the image format which is well-prepared for this feature.

I am attaching a demo with a transparent png image. You can move the image by dragging it with the left mouse button, and you can zoom by means of the mouse wheel. Instead of a TImage I am painting to a TPaintbox because it gives me more control of what is happening. But TImage should work in principle similarly.

There are two options:
  • Original bitmap: stretches the original image onto the painting canvas. Moving it with the mouse is a bit sluggish because StretchDraw is executed with every pixel moved, and this is an expensive operation
  • Stretched bitmap: Does not paint the original image but a stretched bitmap created only when needed. The stretchdraw operation uses a routine from the Lazarus wiki (https://wiki.freepascal.org/Developing_with_Graphics#Using_the_non-native_StretchDraw_from_LazCanvas) which retains the alpha channel. Interpolation is made with the TFPSharpInterpolation, but there are others in unit FPCanvas, see https://wiki.freepascal.org/fcl-image. Since the current procedure stretches the entire image calculation will become noticably slow at high zoom levels - this could be improved by processing only the part of the original image which is visible.
Of course, you could also use the BGRABitmap or Graphics32 libraries instead which are optimized for high performance. You can find them easily using the Online-Package-Manager of Lazarus.

[P.S.]
I could not include the image displayed because the zip file would have become too large. You can download it from http://pngimg.com/download/1156 (or simply use one of your own images and adjust the file name in FormCreate.

Thank you for all that info... good to learn.
I will check everything out.

:)
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: TImage - Transparency - PNG - Doesn't Work When Zoomed In/Out
« Reply #6 on: July 16, 2019, 12:28:22 am »
@WP.... tried the sample.

Works great!

I have actually been trying to use the BGRAGprahic control, but having issues.
See my other thread here.
https://forum.lazarus.freepascal.org/index.php/topic,46089.0.html


Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

 

TinyPortal © 2005-2018