昨天刚写了《哞哞快的 C# 高斯模糊实现》,里边提到了用原作者的方法实现对图像快速的高斯模糊处理,说实话,我没看懂,主要是没看懂原理,怎么就“把图片给处理了”,大概是调用了 GDIPlus.dll 里边的函数,所以我看不到算法和细节,但这正是我要的——专业的人才做专业的事儿,我不懂图像处理,我只想有个 void 帮我随时把某个图像处理掉,最好还是免费、开源、快速的 ^^

昨天写完这个之后,继续去研究方法里作者提到的一个函数“int GdipBitmapCreateApplyEffect()”,据说可以不修改原图,将处理后的效果写进另一个图像中,自己照猫画虎的写了写,不出意外的,不成功。程序也不报错,调试了一下,发现函数的处理结果是“OK”,那就是我自己写的有问题。冷静想想……找找资料

谷歌出一篇,在 CodePlex 上有一个对图像处理的项目,里边使用到了 GDIPlus.dll,下载下来步步跟踪,发现了自己的问题:函数的参数传递的是 IntPtr,就是句柄,也可以理解为“指针”,经过函数处理之后,即便成功了,也需要额外的工作,就是把处理后的图像“写回”到某个 Bitmap 对象中去。我之前没成功就是因为函数执行完了,以为定义的新的 Bitmap 对象就被自动修改了,实际上没有。

想到了自己的问题,就看人家怎么处理的,结果,又是两个俺不懂的函数,不过不影响理解原理。拷贝到方法内,运行,成功!Project 下载

         /// <summary>
/// 使用高斯模糊效果创建一个新的图像
/// </summary>
/// <returns></returns>
public static Bitmap CreateNewWithEffect(this Bitmap image, ref Rectangle Rect, float Radius = , bool ExpandEdge = false)
{
// 新图像
Bitmap newImage = new Bitmap(image); int Result;
IntPtr BlurEffect;
BlurParameters BlurPara;
if ((Radius < ) || (Radius > ))
{
throw new ArgumentOutOfRangeException("Radius 参数错误,半径必须在 [0,255] 范围内");
}
BlurPara.Radius = Radius;
BlurPara.ExpandEdges = ExpandEdge;
Result = GdipCreateEffect(BlurEffectGuid, out BlurEffect); if (Result == )
{
IntPtr Handle = Marshal.AllocHGlobal(Marshal.SizeOf(BlurPara));
Marshal.StructureToPtr(BlurPara, Handle, true);
GdipSetEffectParameters(BlurEffect, Handle, (uint)Marshal.SizeOf(BlurPara));
// 准备参数
IntPtr scrImagePointer = image.NativeHandle(); // 原图像的句柄
Rectangle newImageRect = new Rectangle(Rect.Location, Rect.Size); // 创建一个和要处理的范围同样尺寸的 Rectangle
IntPtr newImagePointer = IntPtr.Zero; //GdipBitmapApplyEffect(image.NativeHandle(), BlurEffect, ref Rect, false, IntPtr.Zero, 0);
// 使用GdipBitmapCreateApplyEffect函数可以不改变原始的图像,而把模糊的结果写入到一个新的图像中
int ok = GdipBitmapCreateApplyEffect(ref scrImagePointer, , BlurEffect, ref Rect,ref newImageRect,out newImagePointer, false, IntPtr.Zero, ); if (ok == ) // 成功
{
// 执行后,newImagePointer 应不为 IntPtr.Zero
if (newImagePointer != IntPtr.Zero)
{
newImage = newImagePointer.NativeBitmapPtrToBitmap();
}
} GdipDeleteEffect(BlurEffect);
Marshal.FreeHGlobal(Handle);
}
else
{
throw new ExternalException("不支持的GDI+版本,必须为GDI+1.1及以上版本,且操作系统要求为Win Vista及之后版本.");
}
return newImage;
}

下面是函数运行完,负责将处理后的数据“赋值”到新的图像的两个俺不懂的方法

         /// <summary>
/// Gets a Bitmap object for a native GDI+ bitmap handle.
/// </summary>
/// <param name="nativeBitmap">The native handle to get the bitmap for.</param>
/// <returns>A Bitmap.</returns>
public static Bitmap NativeBitmapPtrToBitmap(this IntPtr nativeBitmap)
{
return typeof(Bitmap).InvokeStaticPrivateMethod<Bitmap>("FromGDIplus", nativeBitmap);
} /// <summary>
/// Invokes a non-public static method for a Type.
/// </summary>
/// <typeparam name="TResult">The return type of the static method.</typeparam>
/// <param name="type">The Type to invoke the static method for.</param>
/// <param name="methodName">The name of the static method.</param>
/// <param name="args">The arguments for the static method.</param>
/// <returns>The return value of the static method.</returns>
/// <exception cref="System.InvalidOperationException">Static method could not be located.</exception>
public static TResult InvokeStaticPrivateMethod<TResult>(this Type type, string methodName, params object[] args)
{
MethodInfo lmiInfo = type.GetMethod(methodName,
BindingFlags.Static | BindingFlags.NonPublic); if (lmiInfo != null)
return (TResult)(lmiInfo.Invoke(null, args));
else
throw new InvalidOperationException(
string.Format(
"Static method '{0}' could not be located in object type '{1}'.",
methodName, type.FullName));
}

三个函数配合后,就会得到一个新的经过高斯模糊的图像,看 MSDN 介绍,有个很重要的四儿:经过“GdiBitmapCreateApplyEffect()”会返回一个指向新图像的指针对象,在资源不用时需要手动进行释放,我的方法里还没有添加完善的释放资源的逻辑,回头还需要研究研究。原文地址是:http://msdn.microsoft.com/en-us/library/windows/desktop/ms536320(v=vs.85).aspx

运行后

最新文章

  1. SQL增强之Merge
  2. bzoj3295: [Cqoi2011]动态逆序对(树套树)
  3. java循环遍历map
  4. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
  5. uva 108
  6. 怎么在AptanaStudio中把电脑中的浏览器设置为默认执行方法
  7. 犯罪团伙利用POS机刷信用卡积分转卖 年获利千万
  8. PhoneGap-----Contacts
  9. Android 游戏开发 View框架
  10. 设计模式C++实现_1_Singleton设计模式(简单的实现)
  11. Nexpose
  12. css3 3D效果
  13. HDU1423 LCIS
  14. FI-盘盈盘亏借贷科目
  15. C++: cin
  16. c++之&amp;
  17. cf666E. Forensic Examination(广义后缀自动机 线段树合并)
  18. ssh 免密登陆
  19. 使用VMware将Linux装在物理硬盘上,开机即可进入Linux
  20. ui-grid angularjs

热门文章

  1. (c#)SKYPE API项目总结(一)
  2. 处理器(CPU)调度问题
  3. Meteor入门
  4. 【转】Appium基于安卓的各种FindElement的控件定位方法实践
  5. 深入解析java乱码
  6. MAC 命令行工具(Command Line Tools)安装
  7. file 上传文件后缀名 限制
  8. ASP.NET MVC3中Model验证
  9. WCF常见问题(1) -- WebService/WCF Session Cookie
  10. MySQL 存储过程 经常使用语法