源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C# 将透明图片的非透明区域转换成Region的实例代码

  • 时间:2020-06-30 04:13 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C# 将透明图片的非透明区域转换成Region的实例代码
需要设置允许不安全代码.....项目->属性->生成->允许不安全代码
[u]复制代码[/u] 代码如下:
/// <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;         }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部