PNG图纸 [英] PNG drawing

查看:126
本文介绍了PNG图纸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在具有Control的背景色和Control的transparentKey的表单上绘制png图像(因此表单是透明的). png图像具有不透明度(这就是我想要的),但是当它在窗体上绘制时,具有不透明度的图像部分会与窗体背景混合,并且窗体不再透明.

我需要一种将不透明的图像绘制为透明形式的方法,以便
图像不受表格背景色的影响.

thanx.

Hi,

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.

thanx.

推荐答案

使用png图像创建了一个半透明背景的窗口
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;
	try 
	{
		// 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
		APIHelp.BLENDFUNCTION blend = new APIHelp.BLENDFUNCTION();
		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);
	}
	finally 
	{
		// Release device context
		APIHelp.ReleaseDC(IntPtr.Zero, screenDc);
		if (hBitmap != IntPtr.Zero) 
		{
			APIHelp.SelectObject(memDc, hOldBitmap);
			APIHelp.DeleteObject(hBitmap);										// Remove bitmap resources
		}
		APIHelp.DeleteDC(memDc);
	}
}


使要绘制到表单透明部分的图像部分完全透明,因此它们不会更改表单背景的颜色和它保持透明.
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.


这篇关于PNG图纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆