. 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);