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