Cbyte Array and Image Mutual Conversion Instance Code

  • 2021-12-12 09:32:21
  • OfStack

C # byte Array and Image Interconversion Instance Code

Functional requirements:

1. Convert a picture (png bmp jpeg bmp gif) into an byte array and store it in the database.

2. Convert the byte array read from the database into Image object, and assign it to the corresponding control for display.

3. Get the format of the corresponding picture from the picture byte array, generate a picture and save it to the disk.

Image here is System. Drawing. Image.


  //Get an image from file
    Image image = Image.FromFile("D:\\test.jpg");
    Bitmap bitmap = new Bitmap("D:\\test.jpg");

The following three functions fulfill the above three requirements:


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;

namespace NetUtilityLib
{
  public static class ImageHelper
  {
    /// <summary>
    /// Convert Image to Byte[]
    /// </summary>
    /// <param name="image"></param>
    /// <returns></returns>
    public static byte[] ImageToBytes(Image image)
    {
      ImageFormat format = image.RawFormat;
      using (MemoryStream ms = new MemoryStream())
      {
        if (format.Equals(ImageFormat.Jpeg))
        {
          image.Save(ms, ImageFormat.Jpeg);
        }
        else if (format.Equals(ImageFormat.Png))
        {
          image.Save(ms, ImageFormat.Png);
        }
        else if (format.Equals(ImageFormat.Bmp))
        {
          image.Save(ms, ImageFormat.Bmp);
        }
        else if (format.Equals(ImageFormat.Gif))
        {
          image.Save(ms, ImageFormat.Gif);
        }
        else if (format.Equals(ImageFormat.Icon))
        {
          image.Save(ms, ImageFormat.Icon);
        }
        byte[] buffer = new byte[ms.Length];
        //Image.Save() Will change MemoryStream Adj. Position , need to re- Seek To Begin
        ms.Seek(0, SeekOrigin.Begin);
        ms.Read(buffer, 0, buffer.Length);
        return buffer;
      }
    }

    /// <summary>
    /// Convert Byte[] to Image
    /// </summary>
    /// <param name="buffer"></param>
    /// <returns></returns>
    public static Image BytesToImage(byte[] buffer)
    {
      MemoryStream ms = new MemoryStream(buffer);
      Image image = System.Drawing.Image.FromStream(ms);
      return image;
    }

    /// <summary>
    /// Convert Byte[] to a picture and Store it in file
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="buffer"></param>
    /// <returns></returns>
    public static string CreateImageFromBytes(string fileName, byte[] buffer)
    {
      string file = fileName;
      Image image = BytesToImage(buffer);
      ImageFormat format = image.RawFormat;
      if (format.Equals(ImageFormat.Jpeg))
      {
        file += ".jpeg";
      }
      else if (format.Equals(ImageFormat.Png))
      {
        file += ".png";
      }
      else if (format.Equals(ImageFormat.Bmp))
      {
        file += ".bmp";
      }
      else if (format.Equals(ImageFormat.Gif))
      {
        file += ".gif";
      }
      else if (format.Equals(ImageFormat.Icon))
      {
        file += ".icon";
      }
      System.IO.FileInfo info = new System.IO.FileInfo(file);
      System.IO.Directory.CreateDirectory(info.Directory.FullName);
      File.WriteAllBytes(file, buffer);
      return file;
    }
  }
}

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: