使用 GDI 叠加图像+

发布于 2024-08-01 23:58:24 字数 595 浏览 1 评论 0原文

我正在尝试将一个图像与其他几个图像叠加。 我使用这段代码来做到这一点:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬色的秋千 2024-08-08 23:58:24

不知道这是否确实是问题所在,但您没有处理在位图上绘制的图像。 这有帮助吗?

Dim gbkn As Bitmap = New Bitmap(7001, 7001, Imaging.PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(CType(gbkn, Image))
Dim img As Image = Image.FromFile("C:\background.png")
g.DrawImage(img, New Point(0, 0))
img.Dipose()
img As Image = Image.FromFile("C:\firstlayer.png")
g.DrawImage(img, New Point(0, 0))
img.Dispose()
img As Image = Image.FromFile("C:\secondlayer.png")
g.DrawImage(Image.FromFile("C:\secondlayer.png"), New Point(0, 0))
img.Dispose()

我严重怀疑这与图像有任何关系,因为我处理过 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?

Dim gbkn As Bitmap = New Bitmap(7001, 7001, Imaging.PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(CType(gbkn, Image))
Dim img As Image = Image.FromFile("C:\background.png")
g.DrawImage(img, New Point(0, 0))
img.Dipose()
img As Image = Image.FromFile("C:\firstlayer.png")
g.DrawImage(img, New Point(0, 0))
img.Dispose()
img As Image = Image.FromFile("C:\secondlayer.png")
g.DrawImage(Image.FromFile("C:\secondlayer.png"), New Point(0, 0))
img.Dispose()

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.

忘羡 2024-08-08 23:58:24

您需要第一个空位图吗? 如果没有它,您只分配 3*200 MB 而不是 4*200 MB,也许这会起作用:

Dim g As Graphics = Graphics.FromImage("C:\background.png")
g.DrawImage(Image.FromFile("C:\firstlayer.png"), New Point(0, 0))
// and so on

奇怪的是,在几个步骤中叠加不起作用,我认为在这种情况下您没有正确释放内存。 也许最好发布您用于此方法的代码。

我还假设您确实需要其他地方的原始图像,或者特别想使用 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:

Dim g As Graphics = Graphics.FromImage("C:\background.png")
g.DrawImage(Image.FromFile("C:\firstlayer.png"), New Point(0, 0))
// and so on

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文