3

約3000枚以上の画像を処理する必要のあるc#コードをいくつか作成しましたが、500枚目の画像までに、タスクマネージャーは1.5GBのメモリを使用してプログラムを表示しています。以下の機能は、主な原因の1つと思われます。ここで何がうまくいくでしょうか?ヘルプや提案をいただければ幸いです。ありがとう。

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

1 に答える 1

1

このようにコードを変更すると、メモリを減らすことができます

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が最終的に処理するまで待たないことをお勧めします。

于 2011-11-27T00:23:45.100 に答える