public static CachedBitmap ByteToImage(byte[] buffer, int width, int height, PixelFormat format)
{
var stride = ((width * format.BitsPerPixel + 31) / 32) * 4;
var image = BitmapSource.Create(width, height, 96d, 96d, format, null, buffer, stride);
return (CachedBitmap)image;
}