Convert BitmapSource To Bytes - CSharp System.Windows.Media.Imaging

CSharp examples for System.Windows.Media.Imaging:BitmapSource

Description

Convert BitmapSource To Bytes

Demo Code


using System.Windows.Media.Imaging;
using System.Windows;
using System.Drawing.Imaging;
using System.Drawing;
using System;//from www  .ja va2  s  . c o  m

public class Main{

        public static byte[] ConvertToBytes(this BitmapSource bs, int x, int y, int width, int height)
        {
            var rect = new Int32Rect(x, y, width, height);
            var stride = bs.Format.BitsPerPixel * rect.Width / 8;
            byte[] data = new byte[rect.Height * stride];
            bs.CopyPixels(rect, data, stride, 0);
            return data;
        }

        public static byte[] ConvertToBytes(this BitmapSource bs)
        {
            return ConvertToBytes(bs, 0, 0, (int)bs.Width, (int)bs.Height);
        }
}

Related Tutorials