. 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:
- Public Byte[] GetPictureData (StringImagepath)
- {
- /**///// Open the file stream based on the path of the image file and save itByte[]
- FileStream fs =NewFileStream (imagepath, FileMode. Open );// It Can Be Another overload method
- Byte[] ByData =New Byte[Fs. Length];
- Fs. Read (byData, 0, byData. Length );
- Fs. Close ();
- ReturnByData;
- }
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 =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;
- }
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:
- 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 VoidWritePhoto (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";
- }
The preceding describes common methods for storing and reading. NET binary images.
- Implementation of ASP. NET and SQL Server database image storage
- ASP. NET database image storage to Sql2000
- Implementation of ASP. NET database Image Upload and reading
- Analysis on adding watermarks to Images Using ASP. NET (VB)
- Several Methods for quick processing of. NET Images