Friends, I like to give some simple and practical things to garden friends. This is my consistent style. In addition, I want to add that this article is collected in my usual projects. It is a small skill. I hope it will bring some benefits to my friends. If there is anything inaccurate, I also hope that my friends can give reasonable suggestions. The following is the question:
Seeing the title, binary files are very wide. Here I will mainly talk about reading and storing images in binary format.
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:
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. If 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;
}
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 [], 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;
}
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 formats:
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 ";
}