SoFunction
Updated on 2025-03-07

C# Convert the non-transparent area of ​​a transparent picture into Region instance code


/// <summary>
///Accord the area of ​​the non-transparent part of the picture based on the picture
      /// </summary>
        /// <param name="bckImage"></param>
        /// <returns></returns>
        private unsafe Region GetRegion(Bitmap bckImage)
        {
            GraphicsPath path = new GraphicsPath();
            int w = ;
            int h = ;
            BitmapData bckdata = null;
            try
            {
                bckdata = (new Rectangle(0, 0, w, h), , 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)
                        {
                            (new Rectangle(i, j, 1, 1));
                        }
                        bckInt++;
                    }
                }
                (bckdata); bckdata = null;
            }
            catch
            {
                if (bckdata != null)
                {
                    (bckdata);
                    bckdata = null;
                }
            }
            Region region = new (path);
            (); path = null;
            return region;
        }