Converting an image with a general error in GDI + to a byte array

Source: Internet
Author: User

The previous time in the project, when the byte array is converted to an image, a general error occurs occasionally in "GDI +". This is the case when the image is converted to a byte array. The solution is as follows:Code(Error code:

 
1. Public static byte [] imagetobytearray (image imagein) 2. {3. Using (memorystream MS = new memorystream () 4. {5. If (imagein! = NULL) 6. {7. imagein. Save (MS, imageformat. JPEG); 8.} 9. Return Ms. toarray (); 10.} 11 .}

 

Method 1:

 
1. Public static byte [] imagetobytearray (image imagein) 2. {3. Using (memorystream MS = new memorystream () 4. {5. If (imagein! = NULL) 6. {7. bitmap T = new Bitmap (imagein); 8. t. save (MS, system. drawing. imaging. imageformat. JPEG); 9 .} 10. return Ms. toarray (); 11 .} 12 .}

 

Method 2:

 
1. Public static byte [] imagetobytearray (image imagein) 2. {3. Using (memorystream MS = new memorystream () 4. {5. If (imagein! = NULL) 6. {7. bitmap PIC = new Bitmap (imagein. width, imagein. height); 8. graphics G = graphics. fromimage (PIC); 9. g. drawimage (imagein, 0, 0, imagein. width, imagein. height); 10. g. dispose (); 11. PIC. save (MS, imageformat. JPEG); 12. PIC. dispose (); 13 .} 14. return Ms. toarray (); 15 .} 16 .}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.