Convert WriteableBitmap To Byte Array - CSharp System.Windows.Media.Imaging

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

Description

Convert WriteableBitmap To Byte Array

Demo Code


using System.Windows.Media.Imaging;
using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.IO;//from w w  w. j  a  va 2s  .  c  om
using System.Drawing.Imaging;
using System.Drawing;
using System.Collections.Generic;
using System;

public class Main{
        public static byte[] ConvertToByteArray(this WriteableBitmap renderTarget)
        {
            if (renderTarget == null || renderTarget.PixelHeight == 0 || renderTarget.PixelWidth == 0)
                return null;

            int stride = renderTarget.PixelWidth * renderTarget.Format.BitsPerPixel / 8;
            int size = stride * renderTarget.PixelHeight;

            byte[] buffer = new byte[size];

            renderTarget.CopyPixels(buffer, stride, 0);

            return buffer;
        }
        public static byte[] ConvertToByteArray(this RenderTargetBitmap renderTarget)
        {
            if (renderTarget == null || renderTarget.PixelHeight == 0 || renderTarget.PixelWidth == 0)
                return null;

            int stride = renderTarget.PixelWidth * renderTarget.Format.BitsPerPixel / 8;
            int size = stride * renderTarget.PixelHeight;

            byte[] buffer = new byte[size];

            renderTarget.CopyPixels(buffer, stride, 0);

            return buffer;
        }
}

Related Tutorials