Interconversion of Picture Binary and String in C

  • 2021-10-16 02:30:30
  • OfStack

In this paper, the conversion method of picture, binary and string in C # is described with examples. Share it for your reference, as follows:


protected void Button1_Click(object sender, EventArgs e)
{
  // Picture turn 2 Binary system 
  byte[] imageByte = GetPictureData(Server.MapPath("./uploadfile/111.png"));
  //2 Binary conversion to string 
  string picStr = Convert.ToBase64String(imageByte);
  // Output string 
  Response.Write(picStr);
  // String conversion 2 Binary system 
  byte[] imageBytes = Convert.FromBase64String(picStr);
  // Read in MemoryStream Object 
  MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
  memoryStream.Write(imageBytes, 0, imageBytes.Length);
  //2 Convert binary into picture to save 
  System.Drawing.Image image = System.Drawing.Image.FromStream(memoryStream);
  image.Save(Server.MapPath("./uploadfile/222.png"));
}
/// <summary>
/// 2 Binary flow picture 
/// </summary>
/// <param name="streamByte">2 Binary flow </param>
/// <returns> Picture </returns>
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;
}
/// <summary>
///  Picture turn 2 Binary system 
/// </summary>
/// <param name="imagepath"> Picture address </param>
/// <returns>2 Binary system </returns>
public byte[] GetPictureData(string imagepath)
{
  // Open with a file stream based on the path of the picture file and save it as byte[]
  FileStream fs = new FileStream(imagepath, FileMode.Open);// Can be another overloaded method 
  byte[] byData = new byte[fs.Length];
  fs.Read(byData, 0, byData.Length);
  fs.Close();
  return byData;
}
/// <summary>
///  Picture turn 2 Binary system 
/// </summary>
/// <param name="imgPhoto"> Picture object </param>
/// <returns>2 Binary system </returns>
public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto)
{
  // Will Image Convert to stream data and save it 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;
}

PS: Here, this site recommends one online conversion tool for transforming pictures into BASE64 format, which is of great practical value:

Online image conversion BASE64 tool:
http://tools.ofstack.com/transcoding/img2base64

For more readers interested in C # related content, please check out the topics on this site: "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: