Click here to Skip to main content
15,877,168 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am drawing a png image on a form with a backcolor of Control and a transparencyKey of Control (so the forms transparent). The png image has opacity (which is how I want it), but it when it is drawn on the form the parts of the image that have opacity blend with the form background and the form is no longer transparent.

I need a way to draw an image with opacity to a transparent form so that
the image is not effected by the forms backcolor.


Looks like a window with a semi-transparent background using a PNG image was created here. If you look at the Splash.cs file in the solution in the zip file you'll find at that link you'll see they use interop to create the effect. Here is a snippet:
// Sets the current bitmap
public void SelectBitmap(Bitmap bitmap) 
	// Does this bitmap contain an alpha channel?
	if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
		throw new ApplicationException("The bitmap must be 32bpp with alpha-channel.");
	// Get device contexts
	IntPtr screenDc = APIHelp.GetDC(IntPtr.Zero);
	IntPtr memDc = APIHelp.CreateCompatibleDC(screenDc);
	IntPtr hBitmap = IntPtr.Zero;
	IntPtr hOldBitmap = IntPtr.Zero;
		// Get handle to the new bitmap and select it into the current device context
		hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
		hOldBitmap = APIHelp.SelectObject(memDc, hBitmap);
		// Set parameters for layered window update
		APIHelp.Size newSize = new APIHelp.Size(bitmap.Width, bitmap.Height);	// Size window to match bitmap
		APIHelp.Point sourceLocation = new APIHelp.Point(0, 0);
		APIHelp.Point newLocation = new APIHelp.Point(this.Left, this.Top);		// Same as this window
		blend.BlendOp             = APIHelp.AC_SRC_OVER;						// Only works with a 32bpp bitmap
		blend.BlendFlags          = 0;											// Always 0
		blend.SourceConstantAlpha = 255;										// Set to 255 for per-pixel alpha values
		blend.AlphaFormat         = APIHelp.AC_SRC_ALPHA;						// Only works when the bitmap contains an alpha channel
		// Update the window
		APIHelp.UpdateLayeredWindow(Handle, screenDc, ref newLocation, ref newSize,
			memDc, ref sourceLocation, 0, ref blend, APIHelp.ULW_ALPHA);
		// Release device context
		APIHelp.ReleaseDC(IntPtr.Zero, screenDc);
		if (hBitmap != IntPtr.Zero) 
			APIHelp.SelectObject(memDc, hOldBitmap);
			APIHelp.DeleteObject(hBitmap);										// Remove bitmap resources
Share this answer
me773 12-Aug-10 14:32pm    
how they did it with there image is exactly what i want, but when i substituted my image for theirs it did not work
AspDotNetDev 12-Aug-10 15:40pm    
Is it a 32-bit PNG? What do you mean by "it did not work"? Also, put your image in a webpage or something to ensure the transparency is set properly.
me773 12-Aug-10 18:28pm    
I searched up "Layered Windows" on google and it gave me this which does work for me:

Thanks so much for your help! I have been looking for this solution FOREVER!
me773 12-Aug-10 18:28pm    
Reason for my vote of 4
pointed me in the right direction
Make the parts of the image that will be drawn to the transparent sections fo the form fully transparent, so they don't change the color of the form background and it remains transparent.
Share this answer
me773 12-Aug-10 13:04pm    
I still need those parts semi-transparent with the desktop, just not with form.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900