(1) Demonstrate insertion pictures under the console application
public void InsertIMG()
{
//Read the image to be stored as a data stream
FileStream fs = new FileStream(@"E:\", ,);
Byte[] btye2 = new byte[];
(btye2 , 0, Convert.ToInt32());
();
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
();
SqlCommand cmd = new SqlCommand();
= conn;
= "insert into T_Img(imgfile) values(@imgfile)";
SqlParameter par = new SqlParameter("@imgfile", );
= bt;
(par);
int t=(int)(());
if (t > 0)
{
("Insert successful");
}
();
}
}
(2) Read and generate pictures to physical location under the console application
public void Read()
{
byte[] MyData = new byte[0];
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
();
SqlCommand cmd = new SqlCommand();
= conn;
= "select * from T_img";
SqlDataReader sdr = ();
();
MyData = (byte[])sdr["ImgFile"];//Read the bitstream of the first image
int ArraySize= (0);//Get the upper dimension limit of the bitstream array stored in the database and use it as the upper limit of the read stream
FileStream fs = new FileStream(@"c:\", , );
(MyData, 0, ArraySize);
(); //- Write to c:\.
();
("Read successfully");//View files on the hard disk
}
}
(3) Read the image on the next page of the web and write it to the browser for rendering
public void Read()
{
byte[] MyData = new byte[0];
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
();
SqlCommand cmd = new SqlCommand();
= conn;
= "select * from T_img";
SqlDataReader sdr = ();
();
MyData = (byte[])sdr["ImgFile"];
= "image/gif";
(MyData);
();
("Read successful");
}
(4) In the web, you can read and display the picture as above, and the following example is the following when the picture is actually referenced.
<img src="" width="500" height="300" />
(5) The method of writing images into the image type field of the SQL database under Winform is basically the same as the above methods. It is only different from using multiple dialog boxes to help select stored images, etc., and each attribute can be easily utilized.
(6) Reading pictures under Winform is displayed in the picturebox control
Method 1: Use MemoryStream and
public void Read()
{
byte[] MyData = new byte[0];
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
();
SqlCommand cmd = new SqlCommand();
= conn;
= "select * from T_img";
SqlDataReader sdr = ();
();
MyData = (byte[])sdr["ImgFile"];
MemoryStream mystream = new MemoryStream(MyData);
//Create an image image with the specified data stream
img = (mystream, true);
picbox = new PictureBox();
= img;
= 30;
= 80;
= 800;
= 500;
(picbox);
();
();
}
}
Method 2: Read the stream directly into a picture and write it to a physical location, and then render it with the picture.
void Read()
{
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
();
SqlCommand cmd = new SqlCommand();
= conn;
= "select * from T_img";
SqlDataReader sdr = ();
();
byte[] Image_img = (byte[])sdr["ImgFile"];
if (Image_img.Length == 0)
{
return;
}
int filelength = Image_img.Length;
string imageName = "";
string myUrl = + "\\" + imageName;
FileStream fs = new FileStream(myUrl, ,);
BinaryWriter BW = new BinaryWriter(fs);
(Image_img, 0, filelength);
();
();
picbox = new PictureBox();
//Add image method to picbox
// = myUrl;
// = 800;
// = 300;
//Add image method two to picbox
Bitmap bitmap = new Bitmap(myUrl);
= 100;//;
= 80;//;
= (Image)bitmap;
= ;
= 20;
= 30;
(picbox);
();
}
}