gpt4 book ai didi

c# - Aforge 内存使用

转载 作者:行者123 更新时间:2023-11-30 18:02:11 25 4
gpt4 key购买 nike

我已经编写了一些需要处理大约 3000 张图像的 C# 代码,但是到第 500 张图像时,任务管理器显示该程序使用了 1.5GB 的内存。下面的功能似乎是罪魁祸首之一。我可以在这里做得更好吗?任何帮助或建议表示赞赏。谢谢。

   private void FixImage(ref Bitmap field)
{
//rotate 45 degrees
RotateBilinear rot = new RotateBilinear(45);
field = rot.Apply(field); //Memory spikes 2mb here
//crop out unwanted image space
Crop crop = new Crop(new Rectangle(cropStartX, cropStartY, finalWidth, finalHeight));
field = crop.Apply(field); //Memory spikes 2mb here
//correct background
for (int i = 0; i < field.Width; i++)
{
for (int j = 0; j < field.Height; j++)
{
if (field.GetPixel(i, j).ToArgb() == Color.Black.ToArgb())
field.SetPixel(i, j, Color.White);
}
} //Memory usuage increases 0.5mb by the end
}

最佳答案

像这样更改代码时我可以减少内存

private void FixImage(ref Bitmap field)
{
//rotate 45 degrees
RotateBilinear rot = new RotateBilinear(45);
var rotField = rot.Apply(field); //Memory spikes 2mb here
field.Dispose();
//crop out unwanted image space
Crop crop = new Crop(new Rectangle(cropStartX, cropStartY, finalWidth, finalHeight));
var cropField = crop.Apply(rotField); //Memory spikes 2mb here
rotField.Dispose();
//correct background
for (int i = 0; i < cropField.Width; i++)
{
for (int j = 0; j < cropField.Height; j++)
{
if (cropField.GetPixel(i, j).ToArgb() == Color.Black.ToArgb())
cropField.SetPixel(i, j, Color.White);
}
} //Memory usuage increases 0.5mb by the end
field = cropField;
}

所以这似乎是一个好主意,立即释放该图像内存,而不是等到 GC 最终处理它。

关于c# - Aforge 内存使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8282597/

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