Aforge 内存使用情况

发布于 2024-12-17 23:28:17 字数 945 浏览 0 评论 0原文

我编写了一些 C# 代码,需要处理大约 3000 张以上的图像,但到第 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
    }

I have written some c# code that needs to process upwards of about 3000 images, but by the 500th image the task manager is showing the program using 1.5gb of memory. The function below seems to be one of the major culprits. What could I be doing better here? Any help or suggestions are appreciated. Thanks.

   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
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

遗心遗梦遗幸福 2024-12-24 23:28:17

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

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 最终处理它似乎是一个好主意。

i could reduce memory when changing your code like this

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;
}

so it seems to be a good idea, to free up that image memory right away, and not to wait until the GC takes care of it eventually.

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