none
Using a type converter to convert a JPEG to a byte array causes a "A generic error occurred in GDI+" exception RRS feed

  • Question

  • I have extracted the problem code from the project. This is it:

                Image image = Bitmap.FromFile(@"D:\Control Toolbox\test.jpg");

                TypeConverter converter = TypeDescriptor.GetConverter(image);
                byte[] data = (byte[])converter.ConvertTo(null, CultureInfo.InvariantCulture, image, typeof(byte[]));

    This gives me the above exception. If I save the file out as a PNG file and run the same code it all works.

    Any ideas on what could be going wrong?

    Thanks

    Ian

    Friday, February 12, 2016 9:28 AM

Answers

  • Hi

    There is no problem with your code or logic.

    It seems the problem could be with your original image in jpg format.

    when you save it other format the erroneous data block portion might have modified and its not throwing the exception.  if you share your image here it might help us to analyze. 

    Alternatively you can try like this and check

         Image image = Bitmap.FromFile(@"D:\Control Toolbox\test.jpg");
                Bitmap bmp = new Bitmap(image);
                TypeConverter converter = TypeDescriptor.GetConverter(bmp);
                byte[] data = (byte[])converter.ConvertTo(null, CultureInfo.InvariantCulture, bmp, typeof(byte[]));


    • Edited by thiru.mg Friday, February 12, 2016 10:05 AM
    • Proposed as answer by Kristin Xie Friday, February 19, 2016 9:55 AM
    • Marked as answer by DotNet WangModerator Monday, February 22, 2016 5:12 AM
    Friday, February 12, 2016 10:05 AM
  • Hi,

    Thanks for your suggestion. Interestingly enough that does work but I do not understand why. I am just seeing if I can upload the image.

    Thanks

    Ian

    @Lan,

    I agree with thiru.mg. I've tested with your code, It works fine on my side. All are same except the JPG file. So I doubt there is something wrong with your JPG file. As thiru said:

    when you save it other format the erroneous data block portion might have modified.

    And by copying the image internally into a Bitmap instance and saving this image instead of the original image, the error disappeared.

    I also suggest you must dispose image object to release the memory . Try use using statement. Make sure destination directory exists too.

     Image img = Image.FromFile(@"D:\printscreen.jpg");
                byte[] arr;
                using (MemoryStream ms = new MemoryStream())
                {
                    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    arr = ms.ToArray();
                }

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 15, 2016 3:36 AM

All replies

  • Hi

    There is no problem with your code or logic.

    It seems the problem could be with your original image in jpg format.

    when you save it other format the erroneous data block portion might have modified and its not throwing the exception.  if you share your image here it might help us to analyze. 

    Alternatively you can try like this and check

         Image image = Bitmap.FromFile(@"D:\Control Toolbox\test.jpg");
                Bitmap bmp = new Bitmap(image);
                TypeConverter converter = TypeDescriptor.GetConverter(bmp);
                byte[] data = (byte[])converter.ConvertTo(null, CultureInfo.InvariantCulture, bmp, typeof(byte[]));


    • Edited by thiru.mg Friday, February 12, 2016 10:05 AM
    • Proposed as answer by Kristin Xie Friday, February 19, 2016 9:55 AM
    • Marked as answer by DotNet WangModerator Monday, February 22, 2016 5:12 AM
    Friday, February 12, 2016 10:05 AM
  • Hi,

    Thanks for your suggestion. Interestingly enough that does work but I do not understand why. I am just seeing if I can upload the image.

    Thanks

    Ian

    Friday, February 12, 2016 4:01 PM
  • Hi,

    Thanks for your suggestion. Interestingly enough that does work but I do not understand why. I am just seeing if I can upload the image.

    Thanks

    Ian

    @Lan,

    I agree with thiru.mg. I've tested with your code, It works fine on my side. All are same except the JPG file. So I doubt there is something wrong with your JPG file. As thiru said:

    when you save it other format the erroneous data block portion might have modified.

    And by copying the image internally into a Bitmap instance and saving this image instead of the original image, the error disappeared.

    I also suggest you must dispose image object to release the memory . Try use using statement. Make sure destination directory exists too.

     Image img = Image.FromFile(@"D:\printscreen.jpg");
                byte[] arr;
                using (MemoryStream ms = new MemoryStream())
                {
                    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    arr = ms.ToArray();
                }

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 15, 2016 3:36 AM
  • This is the image that is causing issues for me. If anyone has an ideas why this is incorrect it would be appreciated.

    Thanks

    Ian

    Thursday, February 25, 2016 11:55 AM