Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A generic error occurred in GDI+ when saving Image

I read the answer for the same question and did whatever is asked to do. I also gave the write permission to the folder for current user as mentioned in one of the previous answer but still getting this error. So Please can anyone provide me the specific answer what is to be done? This is my code

    protected void Page_Load(object sender, EventArgs e)
{
    //get all the files in a directory
    string[] files = Directory.GetFiles(@"D:\Naved\BasicDotNet\Images");

    //combine them into one image
    System.Drawing.Bitmap stitchedImage = Combine(files);

    //save the new image
    stitchedImage.Save(@"D:\Naved\BasicDotNet\Images", System.Drawing.Imaging.ImageFormat.Jpeg);//Error:-A generic error occurred in GDI+

}
public static System.Drawing.Bitmap Combine(string[] files)
{
    //read all images into memory
    List<System.Drawing.Bitmap> images = new List<System.Drawing.Bitmap>();
    System.Drawing.Bitmap finalImage = null;

    try
    {
        int width = 0;
        int height = 0;

        foreach (string image in files)
        {
            //create a Bitmap from the file and add it to the list
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image);

            //update the size of the final bitmap
            width += bitmap.Width;
            height = bitmap.Height > height ? bitmap.Height : height;

            images.Add(bitmap);
        }

        //create a bitmap to hold the combined image
        finalImage = new System.Drawing.Bitmap(width, height);

        //get a graphics object from the image so we can draw on it
        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
        {
            //set background color
            g.Clear(System.Drawing.Color.Black);

            //go through each image and draw it on the final image
            int offset = 0;
            foreach (System.Drawing.Bitmap image in images)
            {
                g.DrawImage(image,
                  new System.Drawing.Rectangle(offset, 0, image.Width, image.Height));
                offset += image.Width;
            }
        }

        return finalImage;
    }
    catch (Exception ex)
    {
        if (finalImage != null)
            finalImage.Dispose();

        throw ex;
    }
    finally
    {
        //clean up memory
        foreach (System.Drawing.Bitmap image in images)
        {
            image.Dispose();
        }
    }
}
}
like image 546
Naved Ansari Avatar asked Dec 02 '25 06:12

Naved Ansari


2 Answers

Try

stitchedImage.Save(@"D:\Naved\BasicDotNet\Images\stitchedImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

instead of

stitchedImage.Save(@"D:\Naved\BasicDotNet\Images", System.Drawing.Imaging.ImageFormat.Jpeg);

The first argument to Image.Save (String, ImageFormat) is the file name to which to save, not the directory to which to save. Previously you had done

string[] files = Directory.GetFiles(@"D:\Naved\BasicDotNet\Images");

So @"D:\Naved\BasicDotNet\Images" must be a directory, not a file.

(Incidentally, I suggest saving the stitched image to a different directory; otherwise, every time you run your code you'll stitch the previous stitch together with the previous contents.)

like image 55
dbc Avatar answered Dec 03 '25 19:12

dbc


stitchedImage.Save(@"D:\Naved\BasicDotNet\Images", System.Drawing.Imaging.ImageFormat.Jpeg); - you need to specify the file name here. I.e edit it to something like this:     `stitchedImage.Save(@"D:\Naved\BasicDotNet\Images\myjpeg.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)`
like image 43
Radin Gospodinov Avatar answered Dec 03 '25 19:12

Radin Gospodinov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!