0

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 works sometimes and sometimes i get a error in gdi+ on the line I save the image to the file system.

Here is my code:

public static void CutImage(Image image)
    {
        //create new image
        Bitmap source = new Bitmap(image);
        //cut image
        Bitmap cuttedImage = source.Clone(new System.Drawing.Rectangle(250, 0, 5550, 4000), source.PixelFormat);
        //copy bitmap to avoid "general error in gdi+"
        Bitmap copyImage = new Bitmap(cuttedImage.Width, cuttedImage.Height, PixelFormat.Format24bppRgb);
        Graphics g = Graphics.FromImage(copyImage);
        g.DrawImageUnscaled(cuttedImage, 0, 0);
        //dispose graphics object
        g.Dispose();
        //dispose image
        cuttedImage.Dispose();
        //save image to filesystem
        copyImage.Save(@"path\tmp.jpg", ImageFormat.Jpeg);
    }

        //get image
        Image i = Image.FromFile(path\image.jpg);
        //cut image
        CutImage(i);

I searched for a solution and somebody said I have to create a copy of the image I got from Image.FromFile(). But the error still happens sometimes. I tried it a few times and it seems to be random when it happens. The error is always on the Image.Save() line.

Does somebody know how to solve this problem or is there a alternative to Image.Save()?

Thanks for your help!

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • 1
    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. – Enigmativity Jul 09 '18 at 09:15
  • 1
    Why at all are you using System.Drawing.Bitmap? It is WinForms, not WPF. – Clemens Jul 09 '18 at 09:21

1 Answers1

1

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);
                }
            }
        }
    }
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Worth adding is that any unmanaged resource (Bitmaps, RegKeys, Streams, DB Connection & Commands, etc) any object that implements `IDisposable` need to be explicitly disposed, the using pattern helps us not forget. To see if a class implements `IDisposable` put the mouse cursor on it and F12 – Jeremy Thompson Jul 09 '18 at 09:27
  • @Enigmativity Thanks for your help! I testet your code and it works good. I have a question about the 'using': Does 'using' automatically dispose the object or why dont I need to dispose it? Thanks again – Noé Perracini Jul 09 '18 at 09:34
  • 1
    it calls `object.dispose()` at the end of its scope. https://stackoverflow.com/questions/212198/what-is-the-c-sharp-using-block-and-why-should-i-use-it – mcy Jul 09 '18 at 09:37