none
saving a bitmap : "A generic error occurred in GDI" RRS feed

  • Question

  • I'm loading a bitmap from a file using the line 

    Bitmap bmpLoaded = (Bitmap) Bitmap.FromFile(ofd.FileName);

    then I try to save an edited version of that bitmap to the save filename with this line

    bmpWorkingCopy.Save(strCurrentFile);

    but it refuses to save the file and gives me a "A generic error occurred in GDI"  error message.

    I think the app is trying to share the file with itself but I'm not sure how to resolve this error.  any help?

    BadButBit


    my code is perfect until i don't find a bug

    Tuesday, December 18, 2018 5:17 PM

Answers

  • Check the next approach:

    MemoryStream s = new MemoryStream( File.ReadAllBytes( path ) );

    Bitmap bmp = (Bitmap)Bitmap.FromStream( s );

    // . . .

    bmp.Save( path );

     

    Keep the s stream in a variable or member that is valid for the lifetime of the bmp image, and keep the stream open.


    Tuesday, December 18, 2018 7:23 PM

All replies

  • Check the next approach:

    MemoryStream s = new MemoryStream( File.ReadAllBytes( path ) );

    Bitmap bmp = (Bitmap)Bitmap.FromStream( s );

    // . . .

    bmp.Save( path );

     

    Keep the s stream in a variable or member that is valid for the lifetime of the bmp image, and keep the stream open.


    Tuesday, December 18, 2018 7:23 PM
  • Correct, you cannot save an image back to itself as documented here. You need to load the image disconnected from the file it came from. Viorel's approach is one way of doing this.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, December 18, 2018 9:24 PM
    Moderator
  • Hi BadButBit,

    Thank you for posting here.

    You could try the two ways provided by Viorel_ and CoolDadTx to save the image.

    If it could not solve your question, please provide more details about your question. What is the bmpWorkingCopy? More details could help us to test your code.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 19, 2018 2:18 AM
    Moderator