C Implementation Converts Picture to Binary and Converts Binary to Picture Method Example

  • 2021-12-13 16:48:22
  • OfStack

This article describes the example of C # to achieve the conversion of pictures into binary and binary conversion into pictures. Share it for your reference, as follows:


private void button1_Click(object sender, EventArgs e)
{
 string path = this.textBox1.Text;
 byte[] imgBytesIn = SaveImage(path);
 ShowImgByByte(imgBytesIn);
 //Parameters.Add("@Photo", SqlDbType.Binary).Value = imgBytesIn;
}
// Set the picture to 2 Binary flow 
public byte[] SaveImage(String path)
{
 FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); // Save the picture as a file stream 
 BinaryReader br = new BinaryReader(fs);
 byte[] imgBytesIn = br.ReadBytes((int)fs.Length); // Read the stream into a byte array 
 return imgBytesIn;
}
// Reality 2 Picture represented by binary flow 
public void ShowImgByByte(byte[] imgBytesIn)
{
 MemoryStream ms = new MemoryStream(imgBytesIn);
 pictureBox1.Image = Image.FromStream(ms);
}

2. Save the picture to the database and read it from the database:


#region  Read pictures from the database 
/// <summary>
///  Read pictures from the database 
/// </summary>
/// <param name="xs_ID"> Student number to read the picture </param>
/// <param name="ph">pictureBox1 Control name </param>
public void get_photo(string xs_ID, PictureBox ph)// Read pictures from the database 
{
 byte[] imagebytes = null;
 getcon();
 SqlCommand con = new SqlCommand("select * from S_jiben where S_num='" + xs_ID + "'", link);
 SqlDataReader dr = con.ExecuteReader();
 while (dr.Read())
 {
  imagebytes =(byte[])dr.GetValue(18);
 }
 dr.Close();
 con_close();
 MemoryStream ms = new MemoryStream(imagebytes);
 Bitmap bmpt = new Bitmap(ms);
 ph.Image = bmpt;
}
#endregion
#region
public void SaveImage(string MID, OpenFileDialog openF)// Set the picture to 2 The binary system is stored in the database 
{
 string strimg = openF.FileName.ToString(); // Record the path of the picture 
 FileStream fs = new FileStream(strimg, FileMode.Open, FileAccess.Read); // Save the picture as a file stream 
 BinaryReader br = new BinaryReader(fs);
 byte[] imgBytesIn = br.ReadBytes((int)fs.Length); // Read the stream into a byte array 
 getcon();
 StringBuilder strSql = new StringBuilder();
 strSql.Append("update S_jiben Set xs_photo=@Photo where S_num=" + MID);
 SqlCommand cmd = new SqlCommand(strSql.ToString(), link);
 cmd.Parameters.Add("@Photo", SqlDbType.Binary).Value = imgBytesIn;
 cmd.ExecuteNonQuery();
 con_close();
}
#endregion

For more readers interested in C # related content, please check the topics on this site: "Summary of C # Picture Operation Skills", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"

I hope this article is helpful to everyone's C # programming.


Related articles: