Image Tile Gen:OutOfMemory

时间:2014-05-09 09:45:31

标签: c# image garbage-collection out-of-memory

我试图编写一个程序,根据用户提供的维度来删除png文件。我终于让程序工作到一定程度,但是在给我一个OutOfMemory异常之前它只生成3个图像。

我已尝试将Bitmap设为Null,使用.Dispose()GC.Collect()

调用GC.WaitForPendingFinalizers()

我似乎无法释放资源继续我的循环。

    public void ChopSheet(int dim, Bitmap image)
    {
        try
        {
            int x_Max = image.Size.Width;
            int y_Max = image.Size.Height;

            Point[,] Tiles = GetTiles(x_Max, y_Max, dim);

            int xPosition = 0;
            int yPosition = 0;

            sfd.Filter = "PNG | *.png";
            sfd.ShowDialog();
            output = sfd.FileName;

            int numb=0;
            //Gen columns then drop to next row
            for (int r = 0; r <= tiles_Y; r++)
            {
                for (int c = 0; c <= tiles_X; c++) // extract images
                {
                    //cropped = null;

                    numb = c + r;
                    MessageBox.Show("making tile #" + (numb));
                    srcRect.X = xPosition;
                    srcRect.Y = yPosition;

                    srcRect.Width = Tiles[r, c].X;
                    srcRect.Height = Tiles[r, c].Y;

                    cropped = image.Clone(srcRect, image.PixelFormat);

                    xPosition += dim;

                    sb = new StringBuilder(output);
                    if (numb == 0)
                        output = sb.Insert(sb.Length - 4, "_" + numb).ToString();
                    else if (numb > 0)
                        output = sb.Replace("_" + (numb - 1), "_" + numb).ToString();


                    if (output != "")
                        cropped.Save(output);

                    //freeup memory.. somehow
                    //cropped.Dispose();
                }

                xPosition = 0;
                yPosition += dim;
            }
        }//end Try block
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
        }//end catch
    }

0 个答案:

没有答案