简体   繁体   English

使用大位图会导致 Out of Memory 异常

[英]Working with large bitmaps causes Out of Memory Exception

I need to edit(Increase the height) the Image on the fly.我需要即时编辑(增加高度)图像。 The file is mostly 5000*4000 in dimension.该文件的尺寸大多为 5000*4000。 I see the memory shoots up to peak level when I create a bmp of large dimensions and call Graphics.DrawImage method on the bmp instance.当我创建大尺寸的 bmp 并在 bmp 实例上调用 Graphics.DrawImage 方法时,我看到 memory 达到峰值水平。

How do I get rid of the Out Of Memory exception?如何摆脱 Out Of Memory 异常? Is there a way to work with large bitmaps in c#?有没有办法在 c# 中处理大位图?

The problem is the Huge amount of Memory required for the operation.问题是操作需要大量的 Memory。 Yours is taking about some GigaBytes, so the solution could be to use a Stream and process the file in chunks.你的大约需要一些千兆字节,所以解决方案可能是使用 Stream 并分块处理文件。

Or the the best option would be to use some Third party library for it.或者最好的选择是使用一些第三方库。 Below are some for.Net下面是一些for.Net

AForge锻造

Image Resizer图像调整器

Also have a look at this SO question.也看看这个 SO question。

https://stackoverflow.com/questions/158756/what-is-the-best-image-manipulation-library https://stackoverflow.com/questions/158756/what-is-the-best-image-manipulation-library

It's depends on you application specific requeirements, it's not very clear from yuor post, but generaly, working with big media files (images, sounds, videos) I think really good solution is这取决于您的应用程序特定要求,从您的帖子中不是很清楚,但总的来说,使用大型媒体文件(图像、声音、视频)我认为非常好的解决方案是

Memory Mapped Files Memory 映射文件

Save yuor image on the disk in memory mapped file and resize it having on disk, by free yuor RAM as much as possible from a lot of data that you, probably, don't need to have a fast access(in that moment at least)将您的图像保存在 memory 映射文件中的磁盘上,并调整它在磁盘上的大小,通过尽可能多地从您可能不需要快速访问的大量数据中释放您的 RAM(至少在那一刻)

Hope this helps.希望这可以帮助。 Regards.问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM