Board index » delphi » Transparency with GDI+?

Transparency with GDI+?


2007-10-31 03:22:01 AM
delphi33
Hi,
I need to draw a partly transparent bitmap onto the screen (like
TransparentBlt did).
The image is loaded by Graphics.TBitmap.LoadFromFile and then converted
to GDI+ Bitmap with Create(TBitmap.Handle, TBitmap.Palette). (Don't ask,
I need to do this, at least for now.)
It's a BMP file which can have different pixel formats.
I've tried to set the alpha channel of the relevant pixels to 0 using
the bitmap's SetPixel method. DrawImage then displays the image as if
the alpha channel was 255, so fully opaque. Setting TBitmap.PixelFormat
to pf32bit prior to converting gives the same result.
Do I need to convert the pixel format of the bitmap before? And how
would I achieve this?
And another related question, does a DrawCachedBitmap also use translucency?
Thanks in advance for your help.
Danny
// ---8<---
{ This code snippet shows what's actually done. The real code is much
too complex to be posted here. }
var
bmp: TBitmap;
gpbmp: TGpBitmap;
gpgr: TGpGraphics;
col: TGpColor;
begin
bmp := TBitmap.Create;
bmp.LoadFromFile('bitmap.bmp');
gpbmp := TGpBitmap.Create(bmp.Handle, bmp.Palette);
{ Set transparency, actually done with multiple pixels }
col := TGpColor.MakeARGB(0, 0, 0, 0);
gpbmp.SetPixel(0, 0, col);
{ Drawing to a TImage component gives a fully opaque image }
gpgr := TGpGraphics.Create(Image1.Canvas.Handle);
gpgr.DrawImage(gpbmp, 0, 0);
gpgr.Free;
gpbmp.Free;
bmp.Free;
end;
// --->8---
 
 

Re:Transparency with GDI+?

"Danny Strümpel" <XXXX@XXXXX.COM>writes
Quote
The image is loaded by Graphics.TBitmap.LoadFromFile and then converted to
GDI+ Bitmap with Create(TBitmap.Handle, TBitmap.Palette). (Don't ask, I
need to do this, at least for now.)
Why, whats wrong with using that constructor?
Quote
Do I need to convert the pixel format of the bitmap before? And how would
I achieve this?
If you want a fixed alpha transparency regardless of the value stored in the
pixels, I believe you will have to assign a color matrix to the bitmap before
you draw it. Unfortunately I dont have a code example handy at the moment.
It's done via the SetColorMatrix() method of the ImageAttributes class.
You *may* also have to convert the pixel format, but I would try the color
matrix first.
 

Re:Transparency with GDI+?

GrandmasterB schrieb:
Quote
"Danny Strümpel" <XXXX@XXXXX.COM>writes
news:fg80bt$mdu$03$XXXX@XXXXX.COM...
>The image is loaded by Graphics.TBitmap.LoadFromFile and then converted to
>GDI+ Bitmap with Create(TBitmap.Handle, TBitmap.Palette). (Don't ask, I
>need to do this, at least for now.)

Why, whats wrong with using that constructor?
I just wanted to avoid questions like, "Why not use
TGpBitmap.Create(Filename)?" :-)
Quote
If you want a fixed alpha transparency regardless of the value stored in the
pixels, I believe you will have to assign a color matrix to the bitmap before
you draw it. Unfortunately I dont have a code example handy at the moment.
It's done via the SetColorMatrix() method of the ImageAttributes class.
Okay, found it out myself.
The hint to use ImageAttributes led me to the solution.
I'm using a single color which shall be completely transparent.
So I picked the color, changed the Alpha value to 0 and used
SetRemapTable. Now everything looks like expected.
Btw, I don't have any clue on how to operate color matrices. And for
now, I don't need to... :-)
Thanks GrandmasterB!
--
Danny.
 

Re:Transparency with GDI+?

"Danny Strümpel" <XXXX@XXXXX.COM>writes
Quote
I'm using a single color which shall be completely transparent.
So I picked the color, changed the Alpha value to 0 and used
SetRemapTable. Now everything looks like expected.
Ah, you left that part out in your original post. It sounded like you
wanted to draw the bitmap at a fixed level of transparency (ala AlphaBlt).
For making certain colors transparent, you want SetColorKey(), which does
exactly what you are looking for.
 

Re:Transparency with GDI+?

"GrandmasterB" <XXXX@XXXXX.COM>writes
Quote
For making certain colors transparent, you want SetColorKey(), which does
exactly what you are looking for.
But I am glad you got it worked out in any event :-)