To convert a byte array to an image in C#, you can use the following code:

[TestMethod]
public void Test()
{
    byte[] bytes = new byte[100]; // Supply your bytes here. 
    Image image = bytes.ToImage();
}
using System.Drawing;
using System.IO;

namespace Services.Extensions
{
    /// <summary>
    /// Contains extensions methods to the byte[] type.
    /// </summary>
    public static class ByteArrayExtensions
    {
        /// <summary>
        /// Creates an images, based on the given bytes.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="byteArray">The byte array.</param>
        /// <returns></returns>
        public static Image ToImage(this byte[] byteArray)
        {
            return new Bitmap(new MemoryStream(byteArray));
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.