Recent

Author Topic: [SOLVED] Setting Transparency from Image Created by bgraThumbs  (Read 1501 times)

zxandris

  • Full Member
  • ***
  • Posts: 126
Hi there, I've got the following code.
Code: Pascal  [Select][+][-]
  1.       Thumb := TBgraBitmap.create(0,0);
  2.       try
  3.          thumb.SetSize(pnlImageContainer.width, pnlImageContainer.height);
  4.          try
  5.             GetFileThumbnail(AnsiToUTF8(fname), pnlImageContainer.width, pnlImageContainer.height, $001E1E1E, false, thumb);
  6.             if assigned(thumb) then
  7.             begin
  8.                 rst.x := thumb.width;
  9.                 rst.y := thumb.Height;
  10.                 propSize.x := thumb.width;
  11.                 propSize.y := thumb.height;
  12.             end;
  13.          except
  14.                Thumb.setSize(0,0);
  15.                rst.x := 0;
  16.                rst.y := 0;
  17.          end;
  18.          try
  19.             Bgra.SetSize(Thumb.Width, thumb.height);
  20.             Bgra.Assign(thumb);
  21.          except
  22.                bgra.setSize(0,0);
  23.                rst.x := 0;
  24.                rst.y := 0;
  25.          end;
  26.       except
  27.             Bgra.setSize(0,0);
  28.       end;
  29.  

Now as you can see that load's a bgra bitmap, it returns a massive blotch of color where I was hoping it would be nothing, and if I use clNone, it still does.  I was wondering if there was anyway to set that color to say clFuschia and then turn that into transparent pixels for the display?

Any help would be most appreciated.

CJ
« Last Edit: March 08, 2024, 02:27:53 am by zxandris »

Josh

  • Hero Member
  • *****
  • Posts: 1346
Re: Setting Transparency from Image Created by bgraThumbs
« Reply #1 on: March 02, 2024, 02:14:06 pm »
what happens if you set the size and fill on creation, note donot use the setsize after.
Code: Pascal  [Select][+][-]
  1. Thumb := TBgraBitmap.Create(pnlImageContainer.width, pnlImageContainer.height,ColorToBGRA(clWhite,0));
  2.  
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

zxandris

  • Full Member
  • ***
  • Posts: 126
Re: Setting Transparency from Image Created by bgraThumbs
« Reply #2 on: March 02, 2024, 03:21:26 pm »
what happens if you set the size and fill on creation, note donot use the setsize after.
Code: Pascal  [Select][+][-]
  1. Thumb := TBgraBitmap.Create(pnlImageContainer.width, pnlImageContainer.height,ColorToBGRA(clWhite,0));
  2.  

Essentially it just writes white.  No Transparency

Code: Pascal  [Select][+][-]
  1.      try
  2.          try
  3.             GetFileThumbnail(AnsiToUTF8(fname), pnlImageContainer.width-5, pnlImageContainer.height-5, ColorToBGRA($001E1E1E), false, BGRA); //
  4.             if assigned(BGRA) then
  5.             begin
  6.                 rst.x := Bgra.width;
  7.                 rst.y := Bgra.Height;
  8.                 propSize.x := Bgra.width;
  9.                 propSize.y := bgra.height;
  10.             end;
  11.          except
  12.                bgra.setSize(0,0);
  13.                rst.x := 0;
  14.                rst.y := 0;
  15.          end;
  16.      except
  17.             Bgra.setSize(0,0);
  18.             rst.x := 0;
  19.             rst.y := 0;
  20.      end;
  21.  

Is what I've got now, seems faster all told.  Also BGRA is a global BgraBitmap that get's painted to a panel.

Josh

  • Hero Member
  • *****
  • Posts: 1346
Re: Setting Transparency from Image Created by bgraThumbs
« Reply #3 on: March 02, 2024, 05:38:15 pm »
just done a quick example.
not sure if its what u afer though.

edit: updated tpanel and tshape
« Last Edit: March 02, 2024, 06:47:56 pm by Josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

zxandris

  • Full Member
  • ***
  • Posts: 126
Re: Setting Transparency from Image Created by bgraThumbs
« Reply #4 on: March 03, 2024, 01:49:15 am »
just done a quick example.
not sure if its what u afer though.

edit: updated tpanel and tshape

For some reason, doing what you do with the color doesn't seem to work for me.  I now have, (due to dealing with special file formats), the following, and as you can see I set the transparency.

Code: Pascal  [Select][+][-]
  1.      ext := lowercase(ExtractFileExt(fname));
  2.      try
  3.          if pos(ext, cntSpecialLoad)>0 then
  4.          begin
  5.               try
  6.                  LoadToBgra(fname, bgra);
  7.                  rst.x := bgra.Width;
  8.                  rst.y := bgra.Height;
  9.                  PropSize.x := rst.x;
  10.                  PropSize.Y := rst.y;
  11.               except
  12.                     rst.x := 0;
  13.                     rst.y := 0;
  14.                     BGRA.SetSize(0,0);
  15.               end;
  16.          end else if ext = '.png' then
  17.          begin
  18.              try
  19.                 LoadToBgra(fname, bgra);
  20.                 rst.x := bgra.Width;
  21.                 rst.y := bgra.Height;
  22.                 PropSize.x := rst.x;
  23.                 PropSize.Y := rst.y;
  24.              except
  25.                    rst.x := 0;
  26.                    rst.y := 0;
  27.                    BGRA.SetSize(0,0);
  28.              end;
  29.          end else
  30.          begin
  31.              try
  32.                 GetFileThumbnail(AnsiToUTF8(fname), pnlImageContainer.width, pnlImageContainer.height, ColorToBGRA($001E1E1E, 255), false, BGRA);
  33.                 if assigned(BGRA) then
  34.                 begin
  35.                     rst.x := Bgra.width;
  36.                     rst.y := Bgra.Height;
  37.                     propSize.x := Bgra.width;
  38.                     propSize.y := bgra.height;
  39.                 end;
  40.              except
  41.                    bgra.setSize(0,0);
  42.                    rst.x := 0;
  43.                    rst.y := 0;
  44.              end;
  45.          end;
  46.      except
  47.             Bgra.setSize(0,0);
  48.             rst.x := 0;
  49.             rst.y := 0;
  50.      end;
  51.      pnlImagePaint(self);
  52.  

I honestly don't know what I'm doing wrong, I tried replacing 255 with 0 in case I had that the wrong way around and that didn't work.  My bg is automatically checkered, so I should be able to see it if that was nil right/?

circular

  • Hero Member
  • *****
  • Posts: 4368
    • Personal webpage
Re: Setting Transparency from Image Created by bgraThumbs
« Reply #5 on: March 05, 2024, 05:03:24 pm »
Hello zxandris,

I understand you're having difficulty in having a transparent background.

Have you tried giving BGRAPixelTransparent as background color? That is the simplest way to specify a transparent color.

Also note that when you paint it on the panel, the parameter Opaque is to be set to False in order to render with transparency.

Regards
Conscience is the debugger of the mind

zxandris

  • Full Member
  • ***
  • Posts: 126
Re: Setting Transparency from Image Created by bgraThumbs
« Reply #6 on: March 07, 2024, 11:13:30 am »
Hello zxandris,

I understand you're having difficulty in having a transparent background.

Have you tried giving BGRAPixelTransparent as background color? That is the simplest way to specify a transparent color.

Also note that when you paint it on the panel, the parameter Opaque is to be set to False in order to render with transparency.

Regards

Okay that for some reason didn't work so, just in case I'm missin something

Code: Pascal  [Select][+][-]
  1. procedure TframeImageBrowser.pnlImagePaint(Sender: TObject);
  2. begin
  3.     if assigned(bgra) then
  4.     begin
  5.         CalculateSizePos;
  6.         if (Bgra.width<>0) and (bgra.height<>0) then
  7.         begin
  8.             if rst.x<>BGRA.width then
  9.             begin
  10.                  BGRA.Draw(pnlImage.canvas, RECT(0,0, rst.x, rst.y), false);
  11.             end else
  12.             begin
  13.                  BGRA.Draw(pnlImage.canvas, RECT(0,0, PropSize.x, PropSize.y), false);
  14.             end;
  15.         end;
  16.     end;
  17.     try
  18.        if assigned(BGRA) then pnlImage.Visible := true
  19.        else pnlImage.Visible := false;
  20.     except
  21.            //
  22.     end;
  23. end;    
  24.  

That's what I use to paint a global var of Bgrabitmap

and this is where I'm loading (JPG for testing)

Code: Pascal  [Select][+][-]
  1.              try
  2.                 GetFileThumbnail(AnsiToUTF8(fname), pnlImageContainer.width, pnlImageContainer.height, BGRAPixelTransparent, false, BGRA);
  3.                 if assigned(BGRA) then
  4.                 begin
  5.                     rst.x := Bgra.width;
  6.                     rst.y := Bgra.Height;
  7.                     propSize.x := Bgra.width;
  8.                     propSize.y := bgra.height;
  9.                 end else
  10.                 begin
  11.                   bgra.setSize(0,0);
  12.                   rst.x := 0;
  13.                   rst.y := 0;
  14.                   PropSize := rst;
  15.                 end;
  16.              except
  17.                    bgra.setSize(0,0);
  18.                    rst.x := 0;
  19.                    rst.y := 0;
  20.              end;
  21.  

So far nothing is working and I'm a little confused why not to be honest.

zxandris

  • Full Member
  • ***
  • Posts: 126
Re: Setting Transparency from Image Created by bgraThumbs
« Reply #7 on: March 07, 2024, 11:17:39 am »
NVM, I know what I was doing wrong there!  Idiot that I am, the pnl was painting a hard color, not a grid, and sized to the full size of the area it's painting, but I've fixed that now and it seems to be working.

Thanks Guys,


CJ.

circular

  • Hero Member
  • *****
  • Posts: 4368
    • Personal webpage
Re: Setting Transparency from Image Created by bgraThumbs
« Reply #8 on: March 07, 2024, 11:45:48 am »
Ok, I am not sure I understand but I am glad it is solved.  :)
Conscience is the debugger of the mind

zxandris

  • Full Member
  • ***
  • Posts: 126
Re: Setting Transparency from Image Created by bgraThumbs
« Reply #9 on: March 07, 2024, 11:48:16 am »
Ok, I am not sure I understand but I am glad it is solved.  :)

Your tip for the transparency worked like a charm, but I was still drawing to a panel which had a fixed color.  I've made that a checkered grid, and now the image sits pretty :)

CJ

circular

  • Hero Member
  • *****
  • Posts: 4368
    • Personal webpage
Re: Setting Transparency from Image Created by bgraThumbs
« Reply #10 on: March 07, 2024, 12:04:27 pm »
Ok got it. Thanks for clarifying.  :-*
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018