使用 GDI 叠加图像+
我正在尝试将一个图像与其他几个图像叠加。 我使用这段代码来做到这一点:
Dim gbkn As Bitmap = New Bitmap(7001, 7001, Imaging.PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(CType(gbkn, Image))
g.DrawImage(Image.FromFile("C:\background.png"), New Point(0, 0))
g.DrawImage(Image.FromFile("C:\firstlayer.png"), New Point(0, 0))
g.DrawImage(Image.FromFile("C:\secondlayer.png"), New Point(0, 0))
这适用于前两张图片。 之后抛出 OutOfMemoryException。 我意识到图像的尺寸很大。 但是,是否有可能以某种方式进行覆盖并在某个地方将它们缓存起来?
即使我将第一个叠加的结果保存到磁盘、释放内存并添加另一层,我仍然会遇到异常。
我应该如何解决这个问题?
乔斯普
I'm trying to overlay a image with a couple of other images. I use this code to do that:
Dim gbkn As Bitmap = New Bitmap(7001, 7001, Imaging.PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(CType(gbkn, Image))
g.DrawImage(Image.FromFile("C:\background.png"), New Point(0, 0))
g.DrawImage(Image.FromFile("C:\firstlayer.png"), New Point(0, 0))
g.DrawImage(Image.FromFile("C:\secondlayer.png"), New Point(0, 0))
This works with the first two pictures. After that an OutOfMemoryException is thrown.
I realize the size of the images are large.
But isn't it somehow possible to do the overlays and chache them somewhere?
Even if I save the result of the first overlay to disk, free memory, and add another layer I still get the exception.
How should I approach this problem?
JosP
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不知道这是否确实是问题所在,但您没有处理在位图上绘制的图像。 这有帮助吗?
我严重怀疑这与图像有任何关系,因为我处理过 2-3 倍大小的图像,没有出现这个问题。 此外,OutOfMemoryError 异常似乎是<讽刺>非常有用的之一。 GDI 经常抛出的错误与内存无关。
Don't know if this is actually the problem, but you are not disposing of the images that you are drawing on the bitmap. Does this help?
I seriously doubt it has anything to do with the images, as I have worked with images 2-3 times that size without that problem. Also OutOfMemoryError exception seems to be one of the <sarcasm>extremely useful</sarcasm> errors that GDI throws that frequently has nothing to do with memory.
您需要第一个空位图吗? 如果没有它,您只分配 3*200 MB 而不是 4*200 MB,也许这会起作用:
奇怪的是,在几个步骤中叠加不起作用,我认为在这种情况下您没有正确释放内存。 也许最好发布您用于此方法的代码。
我还假设您确实需要其他地方的原始图像,或者特别想使用 C#/GDI+ 来执行此操作,因为使用某些图像编辑程序合并 PNG 文件非常容易。
Do you need the first empty bitmap? Without it, you allocate only 3*200 MB instead of 4*200 MB, perhaps this will work:
It's strange that overlaying in several steps doesn't work, I think you're not freeing memory correctly in this case. Perhaps it will be better to post the code you're using for this approach.
I also assume that you do need the original images somewhere else or do specifically want to do this using C#/GDI+, as it would be very easy to merge the PNG files using some image editing programs.