The picture is saved to the database, if SQL Server is the image type, if it is saved to Oracle is the BLOB type, in C # corresponds to the byte[] type, and only need to cast the read data to the line (byte[]) object .
1. Save the picture as a byte array
parameter is picture path, return byte[] Type
Public byte [] Getpicturedata (string ImagePath) { new FileStream (ImagePath, FileMode.Open); byte New byte [File. Length]; 0 , by. Length); File. Close (); return by ; }
parameter is image, return byte[] Type
Public byte[] Getpicturedata (System.Drawing.Image imgphoto) {//convert image to stream data and save as byte[]MemoryStream Mstream=NewMemoryStream (); Imgphoto.save (mstream,system.drawing.imaging.imageformat.bmp); byte[]bydata=NewByte[mstream. Length]; Mstream. Position=0; Mstream. Read (Bydata,0, bydata.length); Mstream. Close (); returnBydata; }
2. Converting a byte array to a picture
//parameter is a byte[] type, and the return value is an Image object PublicSystem.Drawing.Image Returnphoto (byte[] streambyte) {MemoryStream me=NewMemoryStream (Streambyte); returnSystem.Drawing.Image.FromStream (MS); } //parameter is a byte[] type, there is no return value, which is for ASP. Export the picture from the output to the Web page Public voidWritephoto (byte[] streambyte) {Response.ContentType="Image/gif"; Response.BinaryWrite (Streambyte); }
3. Conversion of Byte[] and string
// byte[] Convert to string byte [] by; string str=System.Convert.ToBase64String (by); // convert String to byte[] string str; byte [] by=system.convert.frombase64string (str);
Save pictures to database and C # read pictures