Recent

Author Topic: Paste Transparent PNG from Clipboard  (Read 392 times)

pixelink

  • Hero Member
  • *****
  • Posts: 1178
    • Pixelink Software
Paste Transparent PNG from Clipboard
« on: July 18, 2019, 06:49:49 pm »
Okay...

I am copying a transaprent PNG from the TImage control.
I have Transparent setting set to True on the Timage i properties panel.... so far that works fine

I copy the image to the clipboard
But if I paste in my RichMemo.... the alpha is black.
Tried pasting it into MSPaint and Word... still black.

So, I am guessing that it has something to do with how I am copying the PNG to the clipboard

Can anyone shed some light as to How to fix this??

FYI... imgCB is my TImage control in code below

Thanks in advance.

Code: Pascal  [Select]
  1. procedure TForm1.btnImportClick(Sender: TObject);
  2. var
  3.  bmpCB: TBitmap;
  4. begin
  5.  
  6.   OpenPictureDialog1.Options := [ofEnableSizing, ofViewDetail, ofHideReadOnly];
  7.  
  8.   if OpenPictureDialog1.Execute then
  9.   begin
  10.      imgCB.Picture.LoadFromFile(OpenPictureDialog1.FileName);
  11.      if imgCB.Picture.Graphic = nil then
  12.        Exit
  13.      else
  14.         begin
  15.            bmpCB := TBitmap.Create;
  16.            bmpCB.Width := imgCB.Width;
  17.            bmpCB.Height := ImgCB.Height;
  18.            bmpCB.Canvas.Draw(0,0,imgCB.Picture.Graphic);
  19.       end;
  20.  
  21.       Clipboard.Assign(bmpCB);
  22.       bmpCB.Free;
  23.  
  24.   end;
  25.  
  26. end;
Can't Type - Forgetful - Had Stroke = Forgive this old man!

Pixelink Software
LAZ 2.02 •  VSSTUDIO(.Net) 2019 • Win10 • 16G RAM • Nivida GForce RTX 2060

winni

  • Hero Member
  • *****
  • Posts: 560
Re: Paste Transparent PNG from Clipboard
« Reply #1 on: July 18, 2019, 07:17:30 pm »
You are not loading correct. Dont' load your file into the picture, but into the declared format

* for PNG use imgCB.Picture.PNG.LoadFromFile(OpenPictureDialog1.FileName);
* for BMP use imgCB.Picture.Bitmap.LoadFromFile(OpenPictureDialog1.FileName);
* for JP[E]G use imgCB.Picture.JPEG.LoadFromFile(OpenPictureDialog1.FileName);

Winni

lucamar

  • Hero Member
  • *****
  • Posts: 2124
Re: Paste Transparent PNG from Clipboard
« Reply #2 on: July 18, 2019, 07:45:50 pm »
You are not loading correct. Dont' load your file into the picture, but into the declared format

I don't think that matters. After all that's what Picture is for: to be able to load any supported graphic and let the control decide how (and to where) do it.

I've not tested but the culprit is most probably drwing into a bitmap. Bitmaps and transparency are strange bed-fellows. Can't you copy the PNG (this time, yes, from Image.Picture.PNG) to the clipboard? Try using Clipboard.SetFormat()
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

pixelink

  • Hero Member
  • *****
  • Posts: 1178
    • Pixelink Software
Re: Paste Transparent PNG from Clipboard
« Reply #3 on: July 18, 2019, 08:00:14 pm »
Will check it out... thanks
Can't Type - Forgetful - Had Stroke = Forgive this old man!

Pixelink Software
LAZ 2.02 •  VSSTUDIO(.Net) 2019 • Win10 • 16G RAM • Nivida GForce RTX 2060

winni

  • Hero Member
  • *****
  • Posts: 560
Re: Paste Transparent PNG from Clipboard
« Reply #4 on: July 18, 2019, 08:16:20 pm »
There where issus with transparent images and loading them into a TImage. I dont know if they are fixed now.  That's why I load explicit
img.Picture.PNG.LoadFromFile (....

On the other hand, that was one point to start using BGRA: the handling of transparency  is easy and clear. And at the end of your operations you can just draw it on a TImage - with transpareny.

Winni

pixelink

  • Hero Member
  • *****
  • Posts: 1178
    • Pixelink Software
Re: Paste Transparent PNG from Clipboard
« Reply #5 on: July 19, 2019, 05:52:26 pm »
There where issus with transparent images and loading them into a TImage. I dont know if they are fixed now.  That's why I load explicit
img.Picture.PNG.LoadFromFile (....

On the other hand, that was one point to start using BGRA: the handling of transparency  is easy and clear. And at the end of your operations you can just draw it on a TImage - with transpareny.

Winni



Yeah, you can load alpha PNG in TImage by just setting Transparent to True in properties.
So, I assume it was fixed.

It's taking a loaded PNG and copying it to the clipboard I have to work out.

Going to work on this in a day or so.

If it doesn't work, I will try the BGRA control instead.
Can't Type - Forgetful - Had Stroke = Forgive this old man!

Pixelink Software
LAZ 2.02 •  VSSTUDIO(.Net) 2019 • Win10 • 16G RAM • Nivida GForce RTX 2060

lucamar

  • Hero Member
  • *****
  • Posts: 2124
Re: Paste Transparent PNG from Clipboard
« Reply #6 on: July 19, 2019, 06:11:02 pm »
ETA: I've been testing around quite a bit and finally managed to make it work. It's just a question of calling:
Code: [Select]
Image.Picture.SaveToClipboardFormat(CF_Bitmap);So your code might look like this:
Code: Pascal  [Select]
  1. procedure TForm1.btnImportClick(Sender: TObject);
  2. begin
  3.   OpenPictureDialog1.Options := [ofEnableSizing, ofViewDetail, ofHideReadOnly];
  4.   if OpenPictureDialog1.Execute then begin
  5.     imgCB.Picture.LoadFromFile(OpenPictureDialog1.FileName);
  6.     if Assigned(imgCB.Picture.Graphic) then
  7.       imgCB.Picture.SaveToClipboardFormat(CF_Bitmap);
  8.   end;
  9. end;



Disregard all that follows: it didn't work!
Didn't notice before but you should set PixelFormat for the bitmap after creating it, like in:
Code: Pascal  [Select]
  1. {... not relevant anymore...}

and probably you should also set:
Code: Pascal  [Select]
  1. {... not relevant anymore...
  2. was setting  Transparent and TransparentMode}
« Last Edit: July 19, 2019, 07:25:17 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.