C# 将透明图片的非透明区域转换成Region的实例代码
时间:2021-10-25 19:28:05
需要设置允许不安全代码.....项目->属性->生成->允许不安全代码
/// <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;
}
标签:透明图片,非透明区域,Region
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java实现哈弗曼编码与反编码实例分享(哈弗曼算法)
2023-11-25 04:54:05
Android RecyclerView添加搜索过滤器的示例代码
2022-03-08 21:44:49
![](https://img.aspxhome.com/file/2023/8/108278_0s.gif)
Spring AOP面向切面编程实现原理方法详解
2021-07-22 00:26:07
C# DataGridView添加新行的2个方法
2023-06-23 05:42:19
C#中label内容显示不全、不完整的解决方法
2022-09-13 02:43:46
Java装饰器设计模式_动力节点Java学院整理
2023-11-11 03:03:10
![](https://img.aspxhome.com/file/2023/5/59085_0s.png)
C#控件picturebox实现图像拖拽和缩放
2023-08-09 08:23:05
详解 Corba开发之Java实现Service与Client
2023-09-23 03:42:48
![](https://img.aspxhome.com/file/2023/3/60183_0s.png)
java实现文件变化监控的方法(推荐)
2023-11-08 01:18:26
IDEA中配置文件模板的添加方法
2023-10-28 17:52:31
![](https://img.aspxhome.com/file/2023/4/58674_0s.jpg)
Maven中怎么手动添加jar包到本地仓库详解(repository)
2023-09-08 07:33:20
![](https://img.aspxhome.com/file/2023/3/82013_0s.jpg)
android View 绘制完成监听的实现方法
2023-08-06 20:47:05
Java Map接口及其实现类原理解析
2022-06-04 22:54:29
关于springboot集成阿里云短信的问题
2023-08-23 09:46:15
![](https://img.aspxhome.com/file/2023/4/58294_0s.png)
Spring Cloud升级最新Finchley版本的所有坑
2021-09-02 07:21:51
![](https://img.aspxhome.com/file/2023/3/88593_0s.png)
SpringBoot中如何对actuator进行关闭
2022-11-30 01:56:37
![](https://img.aspxhome.com/file/2023/9/62349_0s.png)
java跟踪执行的sql语句示例分享
2022-07-30 20:13:18
JAVA实现长连接(含心跳检测Demo)
2023-08-05 09:51:13
C++形参与实参的区别实例解析
2023-11-02 23:39:05
深入了解Java中String、Char和Int之间的相互转换
2022-09-14 10:18:54
![](https://img.aspxhome.com/file/2023/2/64382_0s.jpg)