以下代码实现将一张带透明度的png图片的非透明部分转换成Region输出
需要设置允许不安全代码.....项目->属性->生成->允许不安全代码
class="code_img_closed" src="/Upload/Images/2013100614/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('414087b5-42d0-45e2-9b1f-e4ebf33f4ca1',event)" src="/Upload/Images/2013100614/2B1B950FA3DF188F.gif" alt="" />/// <summary> /// 根据图片得到一个图片非透明部分的区域 /// </summary> /// <param name="bckImage"></param> /// <returns></returns> private unsafe Region GetRegion(Bitmap bckImage) { GraphicsPath path = new GraphicsPath(); int w = bckImage.Width; int h = bckImage.Height; BitmapData bckdata = null; try { bckdata = bckImage.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); uint* bckInt = (uint*)bckdata.Scan0; for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { if ((*bckInt & 0xff000000) != 0) { path.AddRectangle(new Rectangle(i, j, 1, 1)); } bckInt++; } } bckImage.UnlockBits(bckdata); bckdata = null; } catch { if (bckdata != null) { bckImage.UnlockBits(bckdata); bckdata = null; } } Region region = new System.Drawing.Region(path); path.Dispose(); path = null; return region; }View Code