C# save image to file system, error in gdi+

前端 未结 1 721
被撕碎了的回忆
被撕碎了的回忆 2021-01-27 06:22

I have a C# WPF application which cuts a image to the size I need it. The WPF window is used to put in a user ID to save the image into a database. When I test my application it

1条回答
  •  别那么骄傲
    2021-01-27 07:13

    You're creating so many bitmaps and you're not disposing them. Every single IDisposable must be explicitly disposed when you're finished with them.

    Try this and see if the error goes away:

    public static void CutImage(Image image)
    {
        using (Bitmap source = new Bitmap(image))
        {
            using (Bitmap cuttedImage = source.Clone(new System.Drawing.Rectangle(250, 0, 5550, 4000), source.PixelFormat))
            {
                using (Bitmap copyImage = new Bitmap(cuttedImage.Width, cuttedImage.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
                {
                    using (Graphics g = Graphics.FromImage(copyImage))
                    {
                        g.DrawImageUnscaled(cuttedImage, 0, 0);
                        copyImage.Save(@"path\tmp.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                }
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题