gpt4 book ai didi

c# - 写入 300dpi 图像时出现内存不足异常

转载 作者:太空宇宙 更新时间:2023-11-03 14:23:27 25 4
gpt4 key购买 nike

我要求创建一个 300DPI 的图像,但是在合成图像的过程中,比如添加资源(字体、图像),我遇到了内存不足的异常。

using (Bitmap pg = new Bitmap(GetPixelsFromInches(float.Parse(pageWidth), dpi, actualDpi), GetPixelsFromInches(float.Parse(pageHeight), dpi, actualDpi)))
{
pg.SetResolution(float.Parse(dpi), float.Parse(dpi));

Graphics gr = Graphics.FromImage(pg);
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

System.Drawing.Rectangle pgRect = new System.Drawing.Rectangle(0, 0, pg.Width, pg.Height);
SolidBrush solidWhite = new SolidBrush(Color.White);
gr.FillRectangle(solidWhite, pgRect);


currentPageDisplayed = xNode.Attributes["id"].Value;

foreach (XmlElement xElement in xNode)
{
//Here I am writing each elements, like texts or images.
DrawImageForElements(xElement, , dpi, actualDpi);

}


MemoryStream myMemoryStream = new MemoryStream();
//pg.Save(myMemoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
String filename=@"c:\images\" + currentPageDisplayed + "+.png";
pg.Save(filename, System.Drawing.Imaging.ImageFormat.Png);



myMemoryStream.Dispose();
gr.Dispose();
pg.Dispose();
}

如果有其他更好的选择,请提出建议!

最佳答案

我看到这里有两个基本选项。

  1. 获得更广泛的架构。您可能需要在具有大量交换空间的 x64 计算机上执行此操作。这些尺寸的 24 位像素图将占用 300 × 300 × 12 × 12 × 11 × 15 × 3 = 6,415,200,000 字节的内存,这在 x64 机器上是合理的,但大于 x86 能够支持的虚拟内存空间寻址。

  2. strip 或平铺。创建一系列较小的像素图,一次一个,每个对应于图像的某个部分并渲染该部分。 ( strip 意味着从图像的一侧到另一侧进行 strip 化,同时沿两个维度平铺印章。)我不完全确定如何使用 System.Drawing 中的工具将这些片段拼接在一起.** 工具箱。

关于c# - 写入 300dpi 图像时出现内存不足异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4732277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com