Common storage and reading of. Net slices include the following:
Storage image:When you store images in binary format, set the fields in the database to the Image data type (SQL Server). the stored data is Byte [].
1. the parameter is the image path: returned Byte [] type:
Copy codeThe Code is as follows:
Public byte [] GetPictureData (string imagepath)
{
//// Open the file stream based on the path of the image file and save it as byte []
FileStream fs = new FileStream (imagepath, FileMode. Open); // It Can Be Another overload method.
Byte [] byData = new byte [fs. Length];
Fs. Read (byData, 0, byData. Length );
Fs. Close ();
Return byData;
}
2. If the parameter type is an Image object, the returned Byte [] type is:
Copy codeThe Code is as follows:
Public byte [] PhotoImageInsert (System. Drawing. Image imgPhoto)
{
// Convert the Image into streaming data and save it as byte []
MemoryStream mstream = new MemoryStream ();
ImgPhoto. Save (mstream, System. Drawing. Imaging. ImageFormat. Bmp );
Byte [] byData = new Byte [mstream. Length];
Mstream. Position = 0;
Mstream. Read (byData, 0, byData. Length );
Mstream. Close ();
Return byData;
}
Now, we can convert the image into a Byte [] object through the above method, then, the object is saved to the database, and the binary format of the image is saved to the database. Next I will talk about how to read images from the database. In fact, this is the opposite process.
Read image:Convert the corresponding field to Byte []: Byte [] bt = (Byte []) XXXX
1. the parameter is of the Byte [] type, and the returned value is an Image object:
Copy codeThe Code is as follows:
Public System. Drawing. Image ReturnPhoto (byte [] streamByte)
{
System. IO. MemoryStream MS = new System. IO. MemoryStream (streamByte );
System. Drawing. Image img = System. Drawing. Image. FromStream (MS );
Return img;
}
2. the parameter is of the Byte [] type and has no return value. This is for asp.net to output the image to the webpage (Response. BinaryWrite)
Copy codeThe Code is as follows:
Public void WritePhoto (byte [] streamByte)
{
// The default value of Response. ContentType is "text/html"
Response. ContentType = "image/GIF ";
// Image output types: image/GIF image/JPEG
Response. BinaryWrite (streamByte );
}
Supplement:
For the value of Response. ContentType, in addition to the image type, there are other types:
Copy codeThe Code is as follows:
Response. ContentType = "application/msword ";
Response. ContentType = "application/x-shockwave-flash ";
Response. ContentType = "application/vnd. ms-excel ";
In addition, different output types can be used for different formats:
Copy codeThe Code is as follows:
Switch (dataread ("document_type "))
{
Case "doc ":
Response. ContentType = "application/msword ";
Case "swf ":
Response. ContentType = "application/x-shockwave-flash ";
Case "xls ":
Response. ContentType = "application/vnd. ms-excel ";
Case "gif ":
Response. ContentType = "image/gif ";
Case "Jpg ":
Response. ContentType = "image/jpeg ";
}
Some related things can be used as a reference
Copy codeThe Code is as follows:
Image image = GetImageFromClipboard (); // you can obtain an Image from a clipboard.
System. IO. MemoryStream stream = new System. IO. MemoryStream ();
System. Runtime. Serialization. Formatters. Binary. BinaryFormatter formatter
= New System. Runtime. Serialization. Formatters. Binary. BinaryFormatter (); formatter. Serialize (stream, image );
FileStream fs = new FileStream ("xx", FileMode. Open, FileAccess. Write );
Fs. Write (stream. ToArray (), 0, stream. ToArray (). Length );