How to save jpg images in MySQL

Source: Internet
Author: User

The following articles mainly introduce the actual operation process of saving jpg images in MySQL. We all know that images in MySQL databases can be saved through blob, mediumbolb, and ongblob. of course, the operations of different image files may be different, such as. BMP.

Sample Code:

Save the image to MySQL

 
 
  1. private void btnOpenFile_Click(object sender, EventArgs e)  

Open an image file

 
 
  1. this.openFileDialog1.InitialDirectory = "C:\\";  
  2. this.openFileDialog1.FileName = "";  
  3. this.openFileDialog1.ShowDialog(); 

Connection string

 
 
  1. string connStr = "server=vitus;User Id=root;Password=******;Persist Security Info=True;database=Test";  
  2. string sql = string.Format("insert into ImageTest values(@id,@picture)");  
  3. FileStream fs = new FileStream(this.openFileDialog1.FileName,FileMode.Open);  
  4. Byte[] bts = new Byte[fs.Length-1];  
  5. fs.Read(bts,0,(int)fs.Length-1);  
  6. MySqlConnection sqlConn = new MySqlConnection(connStr);  
  7. MySqlCommand sqlComm = new MySqlCommand(sql,sqlConn);  
  8. sqlComm.Parameters.Add("@id", MySqlDbType.Int32, 1);  
  9. sqlComm.Parameters["@id"].Value = 2;  
  10. sqlComm.Parameters.AddWithValue("@picture", bts);  
  11. sqlConn.Open();  
  12. sqlComm.ExecuteNonQuery();  
  13. sqlConn.Clone();  
  14. }  

Read and display images from MySQL

 
 
  1. private void btnImageView_Click(object sender, EventArgs e)  
  2. {  
  3. string connStr = "server=vitus;User Id=root;Password=******;Persist Security Info=True;database=Test";  
  4. string sql = string.Format("select * from ImageTest where id=2");  
  5. MySqlConnection sqlConn = new MySqlConnection(connStr);  
  6. MySqlCommand sqlComm = new MySqlCommand(sql, sqlConn);  
  7. sqlConn.Open();  
  8. MySqlDataReader dr = sqlComm.ExecuteReader(CommandBehavior.CloseConnection);  
  9. Image image = null;  
  10. while (dr.Read())  
  11. {  
  12. MemoryStream buff = new MemoryStream((byte[])dr[1]);  
  13. image = Image.FromStream(buff, true);  
  14. buff.Close();  
  15. }  
  16. this.pictureBox1.Image = image;  
  17. }  

The above content is an introduction to saving JPG images in MySQL. I hope you will find some gains.

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.