C # Binary picture string Mutual transfer

Source: Internet
Author: User

Functional Requirements:

1. Convert a picture (PNG bmp JPEG bmp GIF) to a byte array and store it in the database.

2. Converts a byte array read from the database to an Image object and assigns a value to the corresponding control display.

3, from the picture byte array to obtain the corresponding picture format, generates a picture to save to the disk.

The Image here is System.Drawing.Image.

1    // Get an image from file 2     Image image = Image.FromFile ("d:\\test.jpg"); 3     New Bitmap ("d:\\test.jpg");

The following three functions implement each of the three requirements:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Drawing;4 usingSystem.Drawing.Imaging;5 usingSystem.IO;6 usingSystem.Linq;7 usingSystem.Text;8 9 namespaceNetutilitylibTen { One      Public Static classImagehelper A     { -         /// <summary> -         ///Convert Image to byte[] the         /// </summary> -         /// <param name= "image" ></param> -         /// <returns></returns> -          Public Static byte[] imagetobytes (image image) +         { -ImageFormat format =image. Rawformat; +             using(MemoryStream ms =NewMemoryStream ()) A             { at                 if(format. Equals (imageformat.jpeg)) -                 { - image. Save (MS, imageformat.jpeg); -                 } -                 Else if(format. Equals (imageformat.png)) -                 { in image. Save (MS, imageformat.png); -                 } to                 Else if(format. Equals (imageformat.bmp)) +                 { - image. Save (MS, imageformat.bmp); the                 } *                 Else if(format. Equals (imageformat.gif)) $                 {Panax Notoginseng image. Save (MS, imageformat.gif); -                 } the                 Else if(format. Equals (Imageformat.icon)) +                 { A image. Save (MS, Imageformat.icon); the                 } +                 byte[] buffer =New byte[Ms. Length]; -                 //Image.Save () changes the position of MemoryStream and requires a re-seek to begin $Ms. Seek (0, seekorigin.begin); $Ms. Read (Buffer,0, buffer. Length); -                 returnbuffer; -             } the         } - Wuyi         /// <summary> the         ///Convert byte[] to Image -         /// </summary> Wu         /// <param name= "buffer" ></param> -         /// <returns></returns> About          Public StaticImage Bytestoimage (byte[] buffer) $         { -MemoryStream ms =NewMemoryStream (buffer); -Image image =System.Drawing.Image.FromStream (MS); -             returnimage; A         } +  the         /// <summary> -         ///Convert byte[] to a picture and Store it in file $         /// </summary> the         /// <param name= "FileName" ></param> the         /// <param name= "buffer" ></param> the         /// <returns></returns> the          Public Static stringCreateimagefrombytes (stringFileName,byte[] buffer) -         { in             stringFile =FileName; theImage image =bytestoimage (buffer); theImageFormat format =image. Rawformat; About             if(format. Equals (imageformat.jpeg)) the             { theFile + =". JPEG"; the             } +             Else if(format. Equals (imageformat.png)) -             { theFile + =". PNG";Bayi             } the             Else if(format. Equals (imageformat.bmp)) the             { -File + =". bmp"; -             } the             Else if(format. Equals (imageformat.gif)) the             { theFile + =". gif"; the             } -             Else if(format. Equals (Imageformat.icon)) the             { theFile + =". Icon"; the             }94System.IO.FileInfo info =NewSystem.IO.FileInfo (file); the System.IO.Directory.CreateDirectory (info. Directory.fullname); the file.writeallbytes (File, buffer); the             returnfile;98         } About     } -}

C # Binary picture string Mutual transfer

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.