internal byte[] GetEncodedImageData(ImageSource image, string preferredFormat)
{
try
{
byte[] result = null;
BitmapEncoder encoder = null;
switch (preferredFormat.ToLower())
{
case ".jpg":
case ".jpeg":
encoder = new JpegBitmapEncoder();
break;
case ".bmp":
encoder = new BmpBitmapEncoder();
break;
case ".png":
encoder = new PngBitmapEncoder();
break;
case ".tif":
case ".tiff":
encoder = new TiffBitmapEncoder();
break;
case ".gif":
encoder = new GifBitmapEncoder();
break;
case ".wmp":
encoder = new WmpBitmapEncoder();
break;
}
if (image is BitmapSource)
{
MemoryStream stream = new MemoryStream();
encoder.Frames.Add(BitmapFrame.Create(image as BitmapSource));
encoder.Save(stream);
stream.Seek(0, SeekOrigin.Begin);
result = new byte[stream.Length];
BinaryReader br = new BinaryReader(stream);
br.Read(result, 0, (int)stream.Length);
br.Close();
stream.Close();
}
return result;
}
catch (System.Exception ex1)
{
TaskDialog.Show("Error!", "exception: " + ex1);
}
return null;
}