Bitmaps with embedded alpha

Mr Plough

Member
Joined
Aug 24, 2003
Messages
8
hi,


im trying to display an image using its embedded alpha as transparency. but it seems to ignore the alpha channel.
i thought this would be automatic, but apparently not

im using a 32bpp ARGB .bmp file

Bitmap image(L"blah.bmp");
graphics.DrawImage(image,x,y);

ive trawled through msdn and all the gdi+ tuts i can find but they only mention using matrices to set a uniform alpha across the whole image.

I did manage to find a tutorial for doing using standard gdi, but that kinda defeats the whole point.

thanks.
Mr Plough.
 
ARGB BMPs do yes.
.png files dont, they have transparency like gifs.

im starting to dislike this gdi+ thing.....
 
wierd considering it supposedly supports per pixel alpha.

I guess i could load the alpha channel as a greyscale bitmap and use it to set the alpha manually.

hmm........
 
.png supports 256 levels of transparency, but it doesnt show an alpha channel in photoshop.

i tried loading one using gdi+ and it ignores all but full transparency. which makes it about as usefull as a gif.

whether this is a gdi+ limitation or me being crap remains to be seen :P
 
the app interface is rendered in Direct3D, then blitted to a 32bit GDI+ image to be blitted to the screen (thats why i say "getting OUT of Direct3D")... but the item config dialog is plain GDI+ and it SHOWS PNGs with per pixel alpha blending.... so does the configuration dialog.

if ur really doubting me, download the thing... look at it... delete it after if u want...

i searched this forum and did posts while programming that... look around and u will find my posts...
 
Last edited by a moderator:
i dont doubt u dood, i just cant get the f*@ing thing to work.

I loaded your ring.png file, just in case im making the file wrong -

Bitmap image(L"ring.png");
graphics.DrawImage(&image,x,y);

and it rendered the centre transparent, but the edges are NOT blended.

I cant post the image because my web space is down at the moment.

If you can tell me what im doing wrong, please do so.
 
this is all i do and it works :P

Code:
this.IconBgDisplay.Image=Image.FromFile(FileBrowse.FileName);

there IconBgDisplay is a PictureBox.

nothing special... dunno why it doesnt work there.
 
Png files should work fine in GDI+ (and yes, they have full transparency information) as long as theyre never added to an ImageList. This corrupts their alpha channel.

Have you experimented with drawing them to a graphics surface using DrawImage() in the Paint event of a control?
 
hmm.

i think this can only be caused by the way ive constructed my window. i dont use forms, and am drawing directly to the main window which is set to WS_EX_TRANSPARENT and WS_EX_LAYERED.

msdn doesnt mention any limitations, and all the functions return OK.

i dont really get the SetLayeredWindowAttributes() function. the colorkey setting works fine, but if you set it to LWA_ALPHA with 0 opacity, anything you draw on the window is invisible regardless of alpha. if you set it to anything other than 0, you lose the transparency of the window. and it STILL ignores anything other than fully transparent!

the idea is to get full alpha control on a transparent desktop overlay, much the same as Menges app (although im not making a dock).

should i be using forms and controls and such?
 
funny... coz thats what i doing...
heres a snip

Code:
Rectangle dock=new Rectangle(new Point(0,0), new Size(500, ProgramConfig.IconSize));
Microsoft.DirectX.Direct3D.GraphicsStream Image=front.LockRectangle(dock, LockFlags.None);
				
Bitmap b=new Bitmap(dock.Width, dock.Height, Convert.ToInt32(this.Width*ProgramConfig.BMPFIX), System.Drawing.Imaging.PixelFormat.Format32bppArgb, Image.InternalData);

IntPtr Bmap=b.GetHbitmap(Color.FromArgb(0));
IntPtr memDc = CreateCompatibleDC(GetDC(IntPtr.Zero));
IntPtr oldBitmap = SelectObject(memDc, Bmap);

try
{
	size.cx=dock.Width;
	size.cy=dock.Height;
	UpdateLayeredWindow(LabelDisp.Handle, IntPtr.Zero, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, ULW_ALPHA);
	if(Disp.Visible && ProgramDoing==ProgramState.Nothing)PopUp(false);
}
finally
{
	if (Bmap != IntPtr.Zero) 
	{
		SelectObject(memDc, oldBitmap);
		DeleteObject(Bmap);
	}
	DeleteDC(memDc);
	b.Dispose();
	Image=null;
	front.UnlockRectangle();
}

i think u can get something out of it.
 
Does anyone know what Alpha bitmaps are? Sorry to budge in like this but I was looking at the topic name and I was thinking to myself: "What the heck are they talking about?! Well I better ask or skip it"

;)
 
A PNG file (like a BMP but a different format) can contain not only the red, green and blue values of every pixel but can also contain an alpha value for each pixel. If drawn using the alpha channel, it allows a blending of the pixel in the PNG file with whatever is currently on the screen.

So if the top pixel has an rgb value of 255,0,0 (pure red) and an alpha of 128 and the pixel on the screen where the pixel in the bitmap will be drawn is white, youll end up with a pink dot. Alpha values go from 0 to 255. If its 0, there is nothing new drawn to the screen from the bitmap. If its 255, the pixel in the bitmap is drawn, overwriting whatever was on the screen. Any value in between is a blending of the two pixel colors. In the above, 128 is 50% blending - so blend the pure red with the pure white and you get pink.

-Nerseus
 
How to add controls?

I am using the UpdateLayeredWindow function to do per pixel alpha blending. It works great with a png file with alpha channel. (Yes, png does have an alpha channel.) To summarize what people have been saying in this thread. In order to do the transparency you load the png file into a DIB. The DIB can then be loaded into a CBitmap. If you are using .NET you can load it directly from the file. I am not so have to use the libpng library. I created a dialog app which I use to display the image. I used this code as a guide:
per pixel alpha blending example

Now that I have that working I need to know how to draw the controls I want on top of that. I want to make them ownerdraw, so if anyone has any good examples of that, Id apreciate it. I gather I must draw onto the bitmap I create from the png file with the control images. I then will need to figure out how to handle click and key events.

I also got it working using the SetLayeredWindowAttributes function, but that only does alpha blending of the entire window to a single value, not per pixel. It will also do a transparancy color, but that looks bad since it doesnt anti-alias. But it does show the controls on the form.

Xargon
 
Back
Top