/// <summary>
/// 高斯模糊
/// </summary>
/// <param name="Bmp"></param>
/// <param name="Rect"></param>
/// <param name="Radius"></param>
public static void GaussianBlur(this Bitmap Bmp, ref Rectangle Rect, float Radius)
{
int Result;
IntPtr BlurEffect;
BlurParameters BlurPara;
if ((Radius < 0) || (Radius > 255))
{
throw new ArgumentOutOfRangeException("半径必须在[0,255]范围内");
}
BlurPara.Radius = Radius;
BlurPara.ExpandEdges = false;
Result = GdipCreateEffect(BlurEffectGuid, out BlurEffect);
if (Result == 0)
{
IntPtr Handle = Marshal.AllocHGlobal(Marshal.SizeOf(BlurPara));
Marshal.StructureToPtr(BlurPara, Handle, true);
GdipSetEffectParameters(BlurEffect, Handle, (uint)Marshal.SizeOf(BlurPara));
GdipBitmapApplyEffect(Bmp.NativeHandle(), BlurEffect, ref Rect, false, IntPtr.Zero, 0);
// 使用GdipBitmapCreateApplyEffect函数可以不改变原始的图像,而把模糊的结果写入到一个新的图像中
GdipDeleteEffect(BlurEffect);
Marshal.FreeHGlobal(Handle);
}
else
{
throw new ExternalException("不支持的GDI+版本,必须为GDI+1.1及以上版本,且操作系统要求为Win Vista及之后版本.");
}
}
C# 高斯模糊算法
最新推荐文章于 2024-08-18 10:36:08 发布