C byte array and Image conversion method

  • 2020-05-19 05:40:15
  • OfStack

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 objects and assign the value to the corresponding control for display.

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

Image here is System.Drawing.Image.

The following three functions respectively realize the above three requirements:


// Convert Image to Byte[]
        private byte[] ImageToByte(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 the Position , need to be renewed Seek to Begin
                ms.Seek(0, SeekOrigin.Begin);
                ms.Read(buffer, 0, buffer.Length);
                return buffer;
            }
        }
        // Convert Byte[] to Image
        private Image ByteToImage(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer);
            Image image = System.Drawing.Image.FromStream(ms);
            return image;
        }
        // Convert Byte[] to a picture
        private string CreateImageFromByte(string fileName, byte[] buffer)
        {
            string file = fileName; // File name (no extension included) 
            Image image = ByteToImage(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";
            }
            // The file path directory must exist, otherwise it will be used first Directory Create a directory 
            File.WriteAllBytes(file, buffer);
            return file;
        }


Related articles: