I am trying to download images from a website, convert them to base-64 strings so they can be serialized to a file, deserialize, convert from base-64 string to System.Drawing.Image and then finally convert to System.Windows.Media.ImageSource so they can be data bound to my UI.
public static string ImageToString(Image image)
{
using (var ms = new MemoryStream())
{
if (image == null)
image = Properties.Resources.blank_image;
image.Save(ms, image.RawFormat);
return Convert.ToBase64String(ms.ToArray());
}
}
public static Image StringToImage(string imageString)
{
if (String.IsNullOrEmpty(imageString))
return Properties.Resources.blank_image;
var array = Convert.FromBase64String(imageString);
using (var ms = new MemoryStream(array))
{
return Image.FromStream(ms);
}
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return null;
var image = (System.Drawing.Image)value;
var bitmap = new System.Windows.Media.Imaging.BitmapImage();
bitmap.BeginInit();
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, image.RawFormat);
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
bitmap.StreamSource = memoryStream;
bitmap.EndInit();
return bitmap;
}
I get this rather unhelpful exception when image.Save(memoryStream, image.RawFormat)
in Convert()
is hit:
An exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll but was not handled in user code
Additional information: A generic error occurred in GDI+.
Converting my Properties.Resources.blank_image
Image to ImageSource works fine, but not the base-64 string converted to Image.