C# 将透明图片的非透明区域转换成Region的实例代码
更新时间:2013年10月08日 14:33:32 作者:
以下代码实现将一张带透明度的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;
}
相关文章
C# Winform使用扩展方法实现自定义富文本框(RichTextBox)字体颜色
这篇文章主要介绍了C# Winform使用扩展方法实现自定义富文本框(RichTextBox)字体颜色,通过.NET的静态扩展方法来改变RichTextBox字体颜色,需要的朋友可以参考下2015-06-06
C# Dictionary和SortedDictionary的简介
今天小编就为大家分享一篇关于C# Dictionary和SortedDictionary的简介,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧2018-10-10


最新评论