Conversion of image to byte[] Array

Source: Internet
Author: User



Recent projects have a need for picture operations, you need to save the picture to the database, after an attempt to know that the image type file is not directly stored in the database. We need to do a step conversion before saving: Convert image to byte array type byte[]. Conversion requires the MemoryStream class to read and write memory:

The MemoryStream class is used to read and write data to memory instead of disk. The MemoryStream encapsulates data stored as an array of unsigned bytes, initialized when the MemoryStream object is created, or the array can be created as an empty array. These encapsulated data can be accessed directly in memory. Memory streams can reduce the need for temporary buffers and temporary files in your application.

Image to byte[] method:

        Public byte[] ImageToByteArray (System.Drawing.Image imagein)        {            MemoryStream ms = new MemoryStream ();            Imagein.save (MS, System.Drawing.Imaging.ImageFormat.Jpeg);//saves this image in the specified format to the specified stream,            return Ms. ToArray ();   Write stream content to byte array        }

When the conversion is complete, an error will be added to the Oracle database:

this is because fields in the blob type in Oracle are used to store binary data, not byte[]. Therefore, further conversions are still required.

Bytep[] Convert to BLOB type:

Oracleparameter[] para = new oracleparameter[2];    Similar to Sqlparameter,shape for parameter                                para[0] = new OracleParameter ("shape", Oracletype.blob, picBo.BPicBig.Length);                 Para[0]. Value = Picbo.bpicbig;      Picbo.bpicbig for byte[] Type                 para[1] = new OracleParameter ("Shape1", Oracletype.blob, picBo.BPicSmall.Length);                 PARA[1]. Value = Picbo.bpicsmall;


Once the conversion is complete, you can save the picture to the database.

If the interface needs to present a picture in the database, it needs to be byte[] to image conversion:

Public Image bytearraytoimage (byte[] bytearrayin) {     MemoryStream ms = new MemoryStream (bytearrayin);     Image Returnimage = Image.fromstream (ms);     return returnimage;}




Conversion of image to byte[] Array

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.