OutOfMemoryException 使用 Compact Framework 将大图像加载到 Bitmap 对象 [英] OutOfMemoryException loading big image to Bitmap object with the Compact Framework

查看:25
本文介绍了OutOfMemoryException 使用 Compact Framework 将大图像加载到 Bitmap 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了内存泄漏问题.

I have a problem with a memory leak.

我在 button_click 中有这个代码:

I have this code in a button_click :

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    Dim ms As New IO.MemoryStream
    Dim bm As New Bitmap("Application DataimgsIMG22.jpg")
    bm.Save(ms, Drawing.Imaging.ImageFormat.Jpeg)
End Sub

当我在笔记本电脑上运行 .exe 时,此代码工作正常(我的意思是在具有完整 .net 框架的 Windows7/32 位下)但是当我在装有 WindowsMo​​bile 6.1 的设备上运行该应用程序时,该应用程序会抛出此异常:

This code works just fine when I'm running the .exe at my laptop (I mean under windows7/32bits with the full .net framework) but when I run the app in a device with WindowsMobile 6.1 the app throws this exception:

SmartDeviceProject22.exe
OutOfMemoryException

Microsoft.AGL.Common.MISC.HandleAr(PAL_ERROR ar)
at
System.Drawing.Image.Save(Stream stream, ImageFormat format)
at
SmartDeviceProject22.Form1.Button3_Click(Object sender, EventArgs e)
at
....

图片大小约为 200kb,宽度和高度约为 1500px.图片详情:

The image size is around 200kb and the width and height around 1500px. Details of image:

  • 尺寸:1536x2048
  • 水平分辨率:72dpi
  • 水平分辨率:72dpi
  • 位深度:24
  • 分辨率单位:2
  • 颜色表示:sRGB -

任何帮助将不胜感激.

我尝试了@asawyer 的代码,甚至删除了所有代码、引用等,但问题仍然存在,我猜这与图像的宽度/高度或紧凑框架有关.

I try the code of @asawyer even remove ALL the code,reference, etc and the problem keeps, I guess it's something about the width/height of the image or with the compact framework.

还有什么建议吗?

问题的解决和说明好吧,经过测试,真正的问题不是内存泄漏,正如@pdriegen 所说,这是可用内存的问题.

Solution and explanation of the problem Well after test somethings the real problem it was not a memory leak, just as @pdriegen said its a problem of memory available .

我将代码更改为此(并在移动设备上进行了测试):

I change my code to this (and tested at the mobile device):

 Dim fs As IO.FileStream = IO.File.OpenRead("Application Data
yderIMG23.jpg")
 Dim arrb(fs.Length) As Byte     
 fs.Read(arrb, 0, arrb.Length)
 fs.Close()
 fs.Dispose()

通过上面的代码(显然),我得到了图像的一个字节()(数组),以使用 dataSet 存储在数据库中.

And with the code above (apparently) I get a byte() (array) of the image to store in the database using dataSet.

结论:将位图对象加载到 memoryStream,坏主意.非常感谢所有花时间阅读我的问题的人,特别是那些发布答案的人.

In conclusion: load a bitmap object to memoryStream, bad idea. Many thanks to everyone who take its time to read my problem,and specially those who post their answer.

几周后,这可能是最好的(免费)解决方案:按照此处的说明实现 ImageHelper:ImageHelper

After a few weeks, this probably the best (for free) solution: Implement an ImageHelper as is explained here: ImageHelper

更新了 ImageHelper 的链接https://opennetcf.com/2010/10/13/loading-parts-of-large-images-in-the-compact-framework/

此类/示例使用来自 OpenNetCF 的绘图命名空间 (http://www.opennetcf.com/)

This class/sample uses the Drawing NameSpace from OpenNetCF (http://www.opennetcf.com/)

效果很好,它解决了我将大位图加载到内存的内存问题,实际上我们加载了一个缩略图,因此内存中的大小大大减少并避免了 OutOfMemory 异常问题.

It works great and it solve my memory troubles loading big bitmaps to memory, actually we load a thumbnail, so the size in memory is reduced considerably and avoid the OutOfMemory exception problem.

关于克里斯·塔克我刚刚意识到有关 ImageHelper 和 OpenNetCF 联合创始人的帖子的作者在 stackoverflow,这是他的个人资料:https://stackoverflow.com/users/13154/cacke

About Chris Tacke I just realize that the author of the post about ImageHelper and co-founder of OpenNetCF it's here at stackoverflow, here is his profile: https://stackoverflow.com/users/13154/ctacke

更新链接https://opennetcf.com/2010/10/13/loading-parts-of-large-images-in-the-compact-framework/

推荐答案

我不认为问题是内存泄漏.相反,问题是缺乏可用内存.

I don't believe the problem is a memory leak. Instead, the problem is a lack of available memory.

即使压缩图像大小为 200kb,当您将其作为位图加载时,它也会被解压缩并以原生位图格式存储在内存中.给定高度和宽度各为 1500 像素,并假设位图格式为 32bpp(未指定时的默认值),您将看到 9MB 的分配内存

Even though the compressed image size is 200kb, when you load it as a bitmap it will be decompressed and stored in memory in native Bitmap format. Given a height and width of 1500px each, and assuming a bitmap format of 32bpp (the default when not specified), you're looking at 9MB of allocated memory

1500 * 1500 * 4 = 9MB.

1500 * 1500 * 4 = 9MB.

鉴于移动设备操作系统中存在的内存限制(32MB/进程 - 由系统 dll 分配的空间),您很可能会遇到内存紧缩情况.我当然不知道您运行此代码的应用程序分配了哪些其他内存.

Given the memory constraints present in the mobile device OS (32MB/process - space allocated by system dlls), you may well be in a memory crunch scenario. It's unknown to me of course what other memory is allocated by the application you are running this code in.

在同一台设备上使用较小的图像尝试相同的代码.您应该会看到它运行良好.

Try the same code on the same device with a smaller image. You should see that it executes fine.

这篇关于OutOfMemoryException 使用 Compact Framework 将大图像加载到 Bitmap 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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