C converts the opaque area of a transparent image into the instance code of Region
- 2020-05-19 05:35:04
- OfStack
Need to be set to allow unsafe code... The project - > Property - > Generate - > Allow unsafe code
/// <summary>
/// According to the picture 1 An area of an opaque part of a picture
/// </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;
}