当前位置: 移动技术网 > IT编程>开发语言>c# > C# 将透明图片的非透明区域转换成Region的实例代码

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

2019年07月18日  | 移动技术网IT编程  | 我要评论

需要设置允许不安全代码.....项目->属性->生成->允许不安全代码

复制代码 代码如下:

/// <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;
        }

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网