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:
-
- PublicSystem. Drawing. Image returnphoto (Byte[] Streambyte)
-
- {
-
- System. Io. memorystream MS =NewSystem. Io. memorystream (streambyte );
-
- System. Drawing. Image IMG = system. Drawing. image. fromstream (MS );
- ReturnIMG;
-
- }
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.