. Storage and reading of files (Pictures) in binary form under net

Source: Internet
Author: User

See the title, binary form of the file is very wide. Here I mainly talk about the image in binary form of data reading and storage.
. NET under the common storage and reading of pictures generally have the following:
Store Picture: When you store a picture in binary form, you set the field in the database to the image data type (SQL Server), and the data stored is byte[].
1. parameter is picture path: return byte[] Type:   public   byte [] getpicturedata (String imagepath)
         {
           /**/////uses file flow to open according to the path of the picture file. and save as byte[]  
            FileStream fs =  new FileStream (ImagePath, FileMode.Open);//Can be other overloaded methods
             byte[] Bydata = new byte[fs. Length];
            FS. Read (bydata, 0, bydata.length);
            FS. Close ();
            return bydata;
       }

2. Parameter type is Image object, return byte[] type: public byte [] Photoimageinsert (System.Drawing.Image imgphoto)
{
Convert image to stream data and save 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;
}

OK, so you can convert the image into a byte[object by using the above method, and then save the object in the database to save the image binary format to the database. Now I'll talk about how to read the pictures in the database, which is actually a reverse process.
Read Picture: Convert the corresponding field to byte[] namely: byte[] bt= (byte[]) XXXX
1. Parameter is a byte[] type, the return 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. Parameter is byte[] type, there is no return value, this is for asp.net the picture from output to Web page (response.binarywrite)   public   void Writephoto (Byte [] Streambyte)
        {
         The default value for    //Response.ContentType is "text/html"
             response.contenttype =  "Image/gif";
           //Picture output types are: image/gif  image/jpeg
            Response.BinaryWrite (Streambyte);
       }

Add:
For Response.ContentType values, there are other types, in addition to the types of pictures: Response.ContentType = "Application/msword";
Response.ContentType = "Application/x-shockwave-flash";
Response.ContentType = "application/vnd.ms-excel";

You can also use different output types for different types in 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";
}

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.