c accesses the image image example in sql
- 2020-06-15 10:09:08
- OfStack
(1) To demonstrate image insertion under the console application
public void InsertIMG()
{
// Read the image that needs to be stored as a data stream
FileStream fs = new FileStream(@"E:\c.jpg", FileMode.Open,FileAccess.Read);
Byte[] btye2 = new byte[fs.Length];
fs.Read(btye2 , 0, Convert.ToInt32(fs.Length));
fs.Close();
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insert into T_Img(imgfile) values(@imgfile)";
SqlParameter par = new SqlParameter("@imgfile", SqlDbType.Image);
par.Value = bt;
cmd.Parameters.Add(par);
int t=(int)(cmd.ExecuteNonQuery());
if (t > 0)
{
Console.WriteLine(" Insert the success ");
}
conn.Close();
}
}
(2) Read and generate images to physical locations under the console application
public void Read()
{
byte[] MyData = new byte[0];
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MyData = (byte[])sdr["ImgFile"];// Read the first 1 A bitstream of images
int ArraySize= MyData.GetUpperBound(0);// Gets the dimensional upper limit of the bitstream array stored in the database, which is used as the upper limit of the read stream
FileStream fs = new FileStream(@"c:\00.jpg", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0, ArraySize);
fs.Close(); //-- Written to the c:\00.jpg .
conn.Close();
Console.WriteLine(" Read the success ");// View files on your hard drive
}
}
(3) Under Web, the picshow. aspx page reads and writes the image to the browser for rendering
public void Read()
{
byte[] MyData = new byte[0];
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MyData = (byte[])sdr["ImgFile"];
Response.ContentType = "image/gif";
Response.BinaryWrite(MyData);
conn.Close();
Response.Write(" Read the success ");
}
(4) In web, the image can be read and displayed on the picshow.aspx page as shown above, and the image can be actually referenced as shown below
<img src="picshow.aspx" width="500" height="300" />
(5) Under Winform, the method of writing images to sql database image type field is basically the same as the above method, which is only different from that of using multiple dialog boxes to help select and store images, etc. Each attribute can be easily utilized
(6) Winform read the picture in picturebox control display
Method 1: Use MemoryStream and ES33en.Drawing.Image
public void Read()
{
byte[] MyData = new byte[0];
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MyData = (byte[])sdr["ImgFile"];
MemoryStream mystream = new MemoryStream(MyData);
// Creates with the specified data flow 1 a image The picture
System.Drawing.Image img = System.Drawing.Image.FromStream(mystream, true);
System.Windows.Forms.PictureBox picbox = new PictureBox();
picbox.Image = img;
picbox.Left = 30;
picbox.Top = 80;
picbox.Width = 800;
picbox.Height = 500;
this.Controls.Add(picbox);
mystream.Close();
conn.Close();
}
}
Method 2: Read the stream directly as an image and write it to the physical location, then use the image again for rendering
void Read()
{
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
byte[] Image_img = (byte[])sdr["ImgFile"];
if (Image_img.Length == 0)
{
return;
}
int filelength = Image_img.Length;
string imageName = "1.jpg";
string myUrl = Environment.CurrentDirectory + "\\" + imageName;
FileStream fs = new FileStream(myUrl, FileMode.OpenOrCreate,FileAccess.Write);
BinaryWriter BW = new BinaryWriter(fs);
BW.BaseStream.Write(Image_img, 0, filelength);
BW.Flush();
BW.Close();
System.Windows.Forms.PictureBox picbox = new PictureBox();
// for picbox Add image method 1
//picbox.ImageLocation = myUrl;
//picbox.Width = 800;
//picbox.Height = 300;
// for picbox Add image method 2
Bitmap bitmap = new Bitmap(myUrl);
picbox.Width = 100;//bitmap.Width;
picbox.Height = 80;//bitmap.Height;
picbox.Image = (Image)bitmap;
picbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
picbox.Left = 20;
picbox.Top = 30;
this.Controls.Add(picbox);
conn.Close();
}
}