Storage and reading of binary files (images) in. net
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. The returned byte [] type is public byte [] getpicturedata (string ImagePath)
{
/** // Use the file stream to open the image file path 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. The parameter type is an image object. The returned byte [] type is 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;
} Okay. In this way, the image can be converted 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 [], that is, byte [] bt = (byte []) xxxx
1. the parameter is of the byte [] type, and the returned value is an image object:
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;
}
If the image file is stored as a local file, you can use the IMG. Save () method.
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)
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: 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 types: 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 ";
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zhanghongqiao/archive/2008/03/19/2197256.aspx