. Net binary format file (image) Storage and read detailed Parsing

Source: Internet
Author: User

Common storage and reading of. Net slices include the following:
Storage image:
When you store images in binary format, 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:
Copy codeThe Code is as follows:
Public byte [] GetPictureData (string imagepath)
{
//// Open the file stream based on the path of the image file and save it as byte []
FileStream fs = new FileStream (imagepath, FileMode. Open); // It Can Be Another overload method.
Byte [] byData = new byte [fs. Length];
Fs. Read (byData, 0, byData. Length );
Fs. Close ();
Return byData;
}

2. If the parameter type is an Image object, the returned Byte [] type is:
Copy codeThe Code is as follows:
Public byte [] PhotoImageInsert (System. Drawing. Image imgPhoto)
{
// Convert the Image into streaming data and save it 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;
}

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.

Read image:Convert the corresponding field to Byte []: Byte [] bt = (Byte []) XXXX

1. the parameter is of the Byte [] type, and the returned value is an Image object:
Copy codeThe Code is 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. 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)
Copy codeThe Code is as follows:
Public void WritePhoto (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:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is 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 ";
}

Some related things can be used as a reference
Copy codeThe Code is as follows:
Image image = GetImageFromClipboard (); // you can obtain an Image from a 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 );

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.