. NET binary form of file (picture) storage and reading detailed analysis _ Practical skills

Source: Internet
Author: User
Tags serialization

. NET under the common storage and reading of pictures generally have the following:
Storing pictures:
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:

Copy Code code as follows:

Public byte[] Getpicturedata (string imagepath)
{
Use file stream to open according to the path of 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. The parameter type is an Image object, returning the byte[] type:
Copy Code code as follows:

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 the above method can convert the picture into a byte[] object, and then save the object to the database to achieve the image of the binary format saved to the database. Now I'll talk about how to read the pictures in the database, which is actually a reverse process.

Read pictures:Convert the corresponding field to byte[] i.e.: byte[] bt= (byte[) XXXX

1. Parameter is byte[] type, the return value is an Image object:

Copy Code code as follows:

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 to asp.net the picture from the output to the Web page (response.binarywrite)
Copy Code code as follows:

public void Writephoto (byte[] streambyte)
{
The default value for Response.ContentType is "text/html"
Response.ContentType = "Image/gif";
The types of picture output are: Image/gif image/jpeg
Response.BinaryWrite (Streambyte);
}

Add:
For Response.ContentType values, there are other types, in addition to the types of pictures:
Copy Code code as follows:

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:
Copy Code code as follows:

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";
}

something relevant that can be used as a reference
Copy Code code as follows:

Image image= Getimagefromclipboard ()///To achieve the function of getting images from the Clipboard
System.IO.MemoryStream stream = new System.IO.MemoryStream ();
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter Formatter
= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter (); Formatter. Serialize (stream, image);

FileStream fs=new FileStream ("xx", filemode.open,fileaccess.write);
Fs. Write (stream. ToArray (), 0,stream. ToArray (). Length);

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.