Common Methods for storing and reading. NET Binary Images

Source: Internet
Author: User

. 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 (StringImagepath)
  2. {
  3. /**///// Open the file stream based on the path of the image file and save itByte[]
  4. FileStream fs =NewFileStream (imagepath, FileMode. Open );// It Can Be Another overload method 
  5. Byte[] ByData =New Byte[Fs. Length];
  6. Fs. Read (byData, 0, byData. Length );
  7. Fs. Close ();
  8. ReturnByData;
  9. }

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 =NewMemoryStream ();
  5. ImgPhoto. Save (mstream, System. Drawing. Imaging. ImageFormat. Bmp );
  6. Byte[] ByData =NewByte [mstream. Length];
  7. Mstream. Position = 0;
  8. Mstream. Read (byData, 0, byData. Length );
  9. Mstream. Close ();
  10. ReturnByData;
  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 VoidWritePhoto (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.             }  

The preceding describes common methods for storing and reading. NET binary images.

  1. Implementation of ASP. NET and SQL Server database image storage
  2. ASP. NET database image storage to Sql2000
  3. Implementation of ASP. NET database Image Upload and reading
  4. Analysis on adding watermarks to Images Using ASP. NET (VB)
  5. Several Methods for quick processing of. NET Images

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.