user9408453
user9408453

Reputation:

Saving Images in SQL using C#

I have a dialog box in a C# Winforms application. I want to save images. But each time I click on the save button, I get an error

A generic error occurred in gdi+

This is my code for saving the image:

var SavedFileName = string.Format(@"{0}.png", Guid.NewGuid());

var path = Application.StartupPath + "/passport/" + SavedFileName.ToString();

if(passportsize.Image == null)
{
    SavedFileName = "";
}
else
{
    passportsize.Image.Save(path,System.Drawing.Imaging.ImageFormat.Png);
}

Upvotes: 0

Views: 131

Answers (2)

Terry Tyson
Terry Tyson

Reputation: 623

Try using a back slash instead of a forward slash. "\\passport\\" instead of "/passport/"

Upvotes: 1

ProgrammingLlama
ProgrammingLlama

Reputation: 38850

You haven't provided the full message, so I can't be sure, but it's likely that the source stream that created the image has been disposed of but the image is still tied to it.

When you create the image, you should clone it. For example:

private Image ImageFromBytes(byte[] imageBytes)
{
    using (var ms = new MemoryStream(imageBytes))
    {
        using (var image = Image.FromStream(ms))
        {
            return (Image)image.Clone();
        }
    }
}

Then when you come to save it later, you shouldn't encounter any issues.

Upvotes: 0

Related Questions