Mutual conversion of Image Binary C # (conversion) mutual conversion of Image Binary C #
Common image storage and reading methods include:
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:
Public byte [] GetPictureData (string imagepath)
{
/** // Use the file stream to open the image file path 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:
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 [], that is, Byte [] bt = (Byte []) XXXX
1. the parameter is of the Byte [] type, and the returned 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. 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)
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: 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:
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 ";
}
========================================================== ========================================================== ======================================
Binary file conversion:
String strpath;
Protected void Page_Load (object sender, EventArgs e)
{
Strpath = HttpContext. Current. Request. PhysicalApplicationPath + "1.bmp ";
}
// Convert the image to binary
Public byte [] getBytes (string filePath)
{
System. IO. FileStream fs = new System. IO. FileStream (filePath, System. IO. FileMode. Open );
Byte [] imgData = new byte [fs. Length];
Fs. Read (imgData, 0, (int) fs. Length );
Return imgData;
}
// Convert binary to image
Private System. Drawing. Image convertByteToImg (byte [] imgData)
{
System. IO. MemoryStream MS = new System. IO. MemoryStream (imgData );
System. Drawing. Image img = System. Drawing. Image. FromStream (MS );
Return img;
}
Protected void button#click (object sender, EventArgs e)
{
// The following two lines can directly display the image
// System. Drawing. Image img = convertByteToImg (getBytes (strpath ));
// Img. Save (Response. OutputStream, System. Drawing. Imaging. ImageFormat. Jpeg );
// The following code displays the IMAGE
Byte [] photo = getBytes (strpath );
// Image path
String strPath = "2.JPG ";
String strPhotoPath = Server. MapPath (strPath );
// Save the image file
BinaryWriter bw = new BinaryWriter (File. Open (strPhotoPath, FileMode. OpenOrCreate ));
Bw. Write (photo );
Bw. Close ();
// Display the image
This. Image1.ImageUrl = strPath;
}