Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 586507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:06:51+00:00 2026-05-13T15:06:51+00:00

This seems to be in infamous error . I remember getting it a while

  • 0

This seems to be in infamous error. I remember getting it a while back for different code, but it’s back, with a vengeance, but with some new code that I can’t seem to figure out.
It’s definitely generic, for sure!

The Goal

I’m trying to build a form that allows a user to:

  1. Select an image.
  2. Press save (which closes the form), and saves the image to a byte[] into a database.
  3. Open the form (which loads the image from the byte[]).
  4. Allow them to press save again.
  5. Allow them to open the form again, displaying the image (again).

A pretty standard load/save scenario.

The Problem

Everything works fine regarding loading and saving to the SQL Server. The problem I’m getting is to do with repeatedly loading and saving to and from a byte[] even though I’m using the same settings. Take a look at this code which I mocked up to demonstrate the problem:

static void Main(string[] args)
{
    // Load the image
    var initialImage = (Bitmap)Bitmap.FromFile(@"D:\picture.jpg");

    // Save to a memory stream and get the bytes
    var initialImageBytes = SaveImageToBytes(initialImage);

    // Load again from this saved image
    Bitmap byteLoadedImage = LoadImageFromBytes(initialImageBytes);

    // Save again to bytes, throws "A generic error occurred in GDI+."
    var secondaryImageBytes = SaveImageToBytes(byteLoadedImage);
}

private static byte[] SaveImageToBytes(Bitmap image)
{
    byte[] imageBytes;
    using (MemoryStream imageStream = new MemoryStream())
    {
        image.Save(imageStream, image.RawFormat);
        // "A generic error occurred in GDI+." thrown when saved second time
        imageBytes = imageStream.ToArray();
    }

    return imageBytes;
}

private static Bitmap LoadImageFromBytes(byte[] bytes)
{
    using (MemoryStream imageStream = new MemoryStream(bytes))
    {
        Bitmap image = (Bitmap)Bitmap.FromStream(imageStream);
        return image;
    }
}

The error A generic error occurred in GDI+. is thrown as the image is saved once more to the MemoryStream the second time round. I checked this wasn’t to do with the RawFormat by inspecting the value before the first save, and before the second:

1st Save : {b96b3cae-0728-11d3-9d7b-0000f81ef32e}
2nd Save : {b96b3cae-0728-11d3-9d7b-0000f81ef32e}

The values are identical, so it can’t be a problem with it losing the ImageFormat information.

Can anyone help debug this problem? The code sample I used is tested with a JPEG, and you can get it here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:06:52+00:00Added an answer on May 13, 2026 at 3:06 pm

    I had what I believe was the same problem recently. You need to skip the using statement around the creation of your MemoryStream. Creating a bitmap keeps a reference to the stream that created it. You can read about it on MSDN.

    private static Bitmap LoadImageFromBytes(byte[] bytes)
    {
        var imageStream = new MemoryStream(bytes))
        var image = (Bitmap)Bitmap.FromStream(imageStream);
        return image;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 320k
  • Answers 320k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here's a tip I've used from Tom Kyte's book (forget… May 14, 2026 at 12:20 am
  • Editorial Team
    Editorial Team added an answer You really shouldn't be getting your view (smarty template) to… May 14, 2026 at 12:20 am
  • Editorial Team
    Editorial Team added an answer Is there something in the Android developer guidelines that disuadea… May 14, 2026 at 12:20 am

Related Questions

This seems to be a bit of an infamous error all over the web.
I've created a SubVersion server on one of the machines in my workgroup. From
I've devoted a large number of lines of C code to cleanup-labels/conditionals for failed
I have a page where the infamous 3px extra vertical space (below each li
I'm new to C# and .Net in general so this may be a naive

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.