Loads a Bitmap from a byte array - CSharp System.Drawing

CSharp examples for System.Drawing:Image Convert

Description

Loads a Bitmap from a byte array

Demo Code


using System.IO;// w  ww  .ja va2 s  . c  o  m
using System.Windows.Media;
using System.Runtime.InteropServices;
using System.Windows.Media.Imaging;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        #region Public
        /// <summary>
        /// Loads a Bitmap from a byte array
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static BitmapSource Load(byte[] data, bool crop = true)
        {
            var bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.StreamSource = new MemoryStream(data);
            bitmap.EndInit();
            var result = bitmap as BitmapSource;
            if (crop)
            {
                result = ImageAutoCropper.Crop(bitmap);
            }            
            return result;
        }
}

Related Tutorials