This document describes how to store and read. NET binary images.
. NET binary image storage and reading methods are as follows:
. NET binary Image storage: When storing images in binary format, you must 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:
1. public byte [] GetPictureData (string imagepath)
2 .{
3. // open the file stream based on the path of the image file and save it as byte []
4. FileStream fs = new FileStream (imagepath, FileMode. Open );
5. byte [] byData = new byte [fs. Length];
6. fs. Read (byData, 0, byData. Length );
7. fs. Close ();
8. return byData;
9 .}
10.
2. If the parameter type is an Image object, the returned Byte [] type is:
1. public byte [] PhotoImageInsert (System. Drawing. Image imgPhoto)
2 .{
3. // convert the Image into streaming data and save it as byte []
4. MemoryStream mstream = new MemoryStream ();
5. imgPhoto. Save (mstream, System. Drawing. Imaging. ImageFormat. Bmp );
6. byte [] byData = new Byte [mstream. Length];
7. mstream. Position = 0;
8. mstream. Read (byData, 0, byData. Length );
9. mstream. Close ();
10. return byData;
11 .}
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.
. NET binary image reading: Convert the corresponding fields into Byte []: Byte [] bt = (Byte []) XXXX
1. the parameter is of the Byte [] type, and the returned value is an Image object:
1. public System. Drawing. Image ReturnPhoto (byte [] streamByte)
2 .{
3. System. IO. MemoryStream MS = new System. IO. MemoryStream (streamByte );
4. System. Drawing. Image img = System. Drawing. Image. FromStream (MS );
5. return img;
6 .}
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)
1. public void WritePhoto (byte [] streamByte)
2 .{
3. // The default value of Response. ContentType is "text/html"
4. Response. ContentType = "image/GIF ";
5. // image output types: image/GIF image/JPEG
6. Response. BinaryWrite (streamByte );
7 .}
Supplement:
For the value of Response. ContentType, in addition to the image type, there are other types:
1. Response. ContentType = "application/msword ";
2. Response. ContentType = "application/x-shockwave-flash ";
3. Response. ContentType = "application/vnd. ms-excel ";
In addition, different output types can be used for different formats:
1. switch (dataread ("document_type "))
2 .{
3. case "doc ":
4. Response. ContentType = "application/msword ";
5. case "swf ":
6. Response. ContentType = "application/x-shockwave-flash ";
7. case "xls ":
8. Response. ContentType = "application/vnd. ms-excel ";
9. case "gif ":
10. Response. ContentType = "image/gif ";
11. case "Jpg ":
12. Response. ContentType = "image/jpeg ";
13 .}