📄 bitmaptoregion.cs
字号:
namespace Imps.Utils
{
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public class BitmapToRegion
{
private BitmapToRegion()
{
}
public static Region Convert(Bitmap bitmap, Color transparencyKey)
{
return Convert(bitmap, transparencyKey, TransparencyMode.ColorKeyTransparent);
}
public static unsafe Region Convert(Bitmap bitmap, Color transparencyKey, TransparencyMode mode)
{
if (bitmap == null)
{
throw new ArgumentNullException("Bitmap", "Bitmap cannot be null!");
}
bool flag = mode == TransparencyMode.ColorKeyOpaque;
GraphicsUnit pageUnit = GraphicsUnit.Pixel;
RectangleF bounds = bitmap.GetBounds(ref pageUnit);
Rectangle rect = new Rectangle((int) bounds.Left, (int) bounds.Top, (int) bounds.Width, (int) bounds.Height);
uint num = (uint) ((((transparencyKey.A << 0x18) | (transparencyKey.R << 0x10)) | (transparencyKey.G << 8)) | transparencyKey.B);
BitmapData bitmapdata = bitmap.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
uint* numPtr = (uint*) bitmapdata.Scan0.ToPointer();
int height = (int) bounds.Height;
int width = (int) bounds.Width;
GraphicsPath path = new GraphicsPath();
for (int i = 0; i < height; i++)
{
byte* numPtr2 = (byte*) numPtr;
int num5 = 0;
while (num5 < width)
{
if (!(flag ^ (numPtr[0] == num)))
{
int x = num5;
while ((num5 < width) && !(flag ^ (numPtr[0] == num)))
{
num5++;
numPtr++;
}
path.AddRectangle(new Rectangle(x, i, num5 - x, 1));
}
num5++;
numPtr++;
}
numPtr = (uint*) (numPtr2 + bitmapdata.Stride);
}
Region region = new Region(path);
path.Dispose();
bitmap.UnlockBits(bitmapdata);
return region;
}
public static unsafe Region Convert(Bitmap bitmap, Color transparencyKey, TransparencyMode mode, int orignX, int orignY)
{
if (bitmap == null)
{
throw new ArgumentNullException("Bitmap", "Bitmap cannot be null!");
}
bool flag = mode == TransparencyMode.ColorKeyOpaque;
GraphicsUnit pageUnit = GraphicsUnit.Pixel;
RectangleF bounds = bitmap.GetBounds(ref pageUnit);
Rectangle rect = new Rectangle((int) bounds.Left, (int) bounds.Top, (int) bounds.Width, (int) bounds.Height);
uint num = (uint) ((((transparencyKey.A << 0x18) | (transparencyKey.R << 0x10)) | (transparencyKey.G << 8)) | transparencyKey.B);
BitmapData bitmapdata = bitmap.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
uint* numPtr = (uint*) bitmapdata.Scan0.ToPointer();
int height = (int) bounds.Height;
int width = (int) bounds.Width;
GraphicsPath path = new GraphicsPath();
for (int i = 0; i < height; i++)
{
byte* numPtr2 = (byte*) numPtr;
int num5 = 0;
while (num5 < width)
{
if (!(flag ^ (numPtr[0] == num)))
{
int num6 = num5;
while ((num5 < width) && !(flag ^ (numPtr[0] == num)))
{
num5++;
numPtr++;
}
path.AddRectangle(new Rectangle(num6 + orignX, i + orignY, num5 - num6, 1));
}
num5++;
numPtr++;
}
numPtr = (uint*) (numPtr2 + bitmapdata.Stride);
}
Region region = new Region(path);
path.Dispose();
bitmap.UnlockBits(bitmapdata);
return region;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -