用于 ASP.NET 的 System.Drawing 的替代方案? [英] Alternatives to System.Drawing for use with ASP.NET?

查看:31
本文介绍了用于 ASP.NET 的 System.Drawing 的替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过几天追踪奇怪的 GDI+ 错误,我在 MSDN:

After several days of tracking down bizarre GDI+ errors, I've stumbled across this little gem on MSDN:

不支持在 Windows 或 ASP.NET 服务中使用 System.Drawing 命名空间中的类.尝试从这些应用程序类型之一中使用这些类可能会产生意外问题,例如服务性能下降和运行时异常.

Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions.

我不知道在这种情况下ASP.NET 服务"是否意味着Web 应用程序",但服务性能下降"似乎涵盖了GDI+ 中发生一般错误"和超出范围"的随机分类.内存"错误,我的应用程序抛出 - 读取和写入 JPEG 图像的间歇性、不可重现的错误 - 在许多情况下 - 实际上首先由 System.Drawing.Imaging 创建.

I don't know whether "ASP.NET service" means "web application" in this context, but "diminished service performance" certainly seems to cover the random assortment of "A generic error occurred in GDI+" and "Out of memory" errors that my app is throwing - intermittent, non-reproducible errors reading and writing JPEG images that - in many cases - were actually created by System.Drawing.Imaging in the first place.

那么 - 如果 GDI+ 无法在 Web 应用程序中可靠地读取和写入 JPEG 文件,我应该改用什么?

我希望用户能够上传图像(需要 JPEG,其他格式也不错),可靠地重新采样,并在出现任何问题时显示有用的错误消息.有任何想法吗?WPF 中的 System.Media 命名空间值得考虑吗?

I want users to be able to upload images (JPEG required, other formats nice-to-have), resample them reliably, and display useful error messages if anything goes wrong. Any ideas? Are the System.Media namespaces from WPF worth considering?

是的,我知道 GDI+大部分时间"都可以工作.这还不够好,因为当它失败时,它以一种不可能优雅地隔离或恢复的方式发生.我对适用于您的 GDI+ 代码示例不感兴趣:我正在寻找用于图像处理的替代库.

Yeah, I know GDI+ works "most of the time". That's not good enough, because when it fails, it does so in a way that's impossible to isolate or recover from gracefully. I am not interested in examples of GDI+ code that works for you: I am looking for alternative libraries to use for image processing.

推荐答案

有一篇很棒的博客文章,包括关于使用 ImageMagick 图形库通过 Interop 在 TopTen 软件博客.这篇文章专门讨论了在 linux 上在 mono 下运行 ASP.net;但是,C# 代码应该是完全可以复制粘贴的,如果您在引用窗口二进制文件 (DLL) 的 Windows 下运行,您唯一需要更改的就是互操作属性.

There is an excellent blog post including C# code about using the ImageMagick graphics library through Interop over at TopTen Software Blog. This post deals specifically with running ASP.net on linux under mono; however, the C# code should be perfectly copy-paste-able, the only thing you'll need to change is the Interop attributes if you are running under windows referencing a window binary (DLL).

ImageMagick® 是一个软件套件,用于创建、编辑、合成或转换位图图像.它可以读写多种格式的图像(超过 100)包括 DPX、EXR、GIF、JPEG、JPEG-2000、PDF、PhotoCD、PNG、Postscript、SVG 和 TIFF.使用 ImageMagick 调整大小、翻转、镜像、旋转、扭曲、剪切和变换图像,调整图像颜色,应用各种特殊效果,或绘制文本、线条、多边形,椭圆和贝塞尔曲线.

ImageMagick® is a software suite to create, edit, compose, or convert bitmap images. It can read and write images in a variety of formats (over 100) including DPX, EXR, GIF, JPEG, JPEG-2000, PDF, PhotoCD, PNG, Postscript, SVG, and TIFF. Use ImageMagick to resize, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves.

在 codeplex 上还有一个 ImageMagick .Net 开发项目,可以为您打包所有内容.但它自 2009 年以来并没有表现出积极的发展,因此它可能落后于当前的 ImageMagick 库版本.对于一个小的琐碎的调整大小例程,我可能会坚持使用互操作.你只需要仔细观察你的实现是否有你自己的内存泄漏或未释放的资源(库本身已经过良好的测试和社区审查).

There is also an ImageMagick .Net development project on codeplex that wraps up everything for you. But it doesn't show active development since 2009, so it may be lagging behind the current ImageMagick library version. For a small trivial resizing routine, I'd probably stick with the interop. You just need to watch your implementation carefully for your own memory leak or unreleased resources (the library itself is well tested and vetted by the community).

该库是免费和开源的.Apache 2 许可证似乎与个人和商业目的兼容.请参阅 ImageMagick 许可页面.

The library is free and open source. The Apache 2 license appears to be compatible with both personal and commercial purposes. See ImageMagick License Page.

该库是完全跨平台的,并实现了许多在 GDI+ 中没有的强大的图像处理和转换例程(或没有在单声道下实现),并且作为 ASP.net 图像处理的替代方案享有盛誉.

The library is totally cross platform and implements many powerful image handling and transformation routines that are not found in GDI+ (or not implemented under mono) and has a good reputation as an alternative for ASP.net image processing.

更新:看起来这里有一个 .NET 包装器的更新版本:http://magick.codeplex.com/

Update: Looks like there is an updated version of a .NET wrapper here: http://magick.codeplex.com/

这篇关于用于 ASP.NET 的 System.Drawing 的替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆