以下代码实现将一张带透明度的png图片的非透明部分转换成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;
        }

以上方法小图还可以,大图速度上就显得慢了.....

相关文章:

  • 2021-12-13
  • 2021-12-04
  • 2021-05-11
  • 2022-02-20
  • 2021-10-20
  • 2022-01-30
  • 2021-05-31
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-26
  • 2021-12-13
  • 2022-12-23
  • 2022-12-23
  • 2022-02-14
  • 2021-08-15
相关资源
相似解决方案