用C# winform开发界面时会遇到很多小问题,比如贴透明的png图片或GIF图片后,图片无法透明,这点可以通过属性设置:外观/BackColor (Web TransParent)可以解决。
但当用多个控件时,如PictureBox,控件都贴透明图片后,两个PictureBox有重叠的地方,希望重叠的地方能透明,就是透视下面的控件部分,结果测试后发现两控件重叠透明的地方对整个框架的背景透明,但对下面控件无法透明,最后在网上找了一段代码,解决了这个问题。
解决时,对每个控件使用:ControlTrans(pic,pic.Image);然后层叠之后就可以透明了。
private
unsafe
static GraphicsPath subGraphicsPath(Image img)
{
if (img == null) return null;
// 建立GraphicsPath, 给我们的位图路径计算使用
GraphicsPath g = new GraphicsPath(FillMode.Alternate);
Bitmap bitmap = new Bitmap(img);
int width = bitmap.Width;
int height = bitmap.Height;
BitmapData bmData = bitmap.LockBits( new Rectangle( 0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte* p = ( byte*)bmData.Scan0;
int offset = bmData.Stride - width * 3;
{
if (img == null) return null;
// 建立GraphicsPath, 给我们的位图路径计算使用
GraphicsPath g = new GraphicsPath(FillMode.Alternate);
Bitmap bitmap = new Bitmap(img);
int width = bitmap.Width;
int height = bitmap.Height;
BitmapData bmData = bitmap.LockBits( new Rectangle( 0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte* p = ( byte*)bmData.Scan0;
int offset = bmData.Stride - width * 3;