软件编程
位置:首页>> 软件编程>> C#编程>> C# 将透明图片的非透明区域转换成Region的实例代码

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

  发布时间:2021-10-25 19:28:05 

标签:透明图片,非透明区域,Region

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


/// <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
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com