. Net binary image reading and storage upload image to database binary

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

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.