using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Configuration;
using System.Windows.Media.Imaging;

namespace VideoDistanc
{
    public class ImageConverter
    {
        public ImageConverter()
        {

        }

        public static byte[] ToByteArray(this WriteableBitmap bmp)
        {
            // Init buffer
            int w = bmp.PixelWidth;
            int h = bmp.PixelHeight;
            int[] p = bmp.Pixels;
            int len = p.Length;
            byte[] result = new byte[4 * w * h];
            // Copy pixels to buffer
            for (int i = 0, j = 0; i < len; i++, j += 4)
            {
                int color = p[i];
                result[j + 0] = (byte)(color >> 24); // A
                result[j + 1] = (byte)(color >> 16); // R
                result[j + 2] = (byte)(color >> 8);  // G
                result[j + 3] = (byte)(color);       // B
            }
            return result;
        }
        
        // Convert byte array to Bitmap (byte[] to Bitmap)
        public WriteableBitmap ConvertToBitmap(byte[] b)
        {
            WriteableBitmap bmp=null;
            if (b != null)
            {
                int w = bmp.PixelWidth;
                int h = bmp.PixelHeight;
                int[] p = bmp.Pixels;
                int len = p.Length;
                byte[] result = new byte[4 * w * h];

                return bmp;
            }
            else return null;
        }

    }
}
