Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i read a base64 image in WPF?

Tags:

c#

.net

wpf

I know how to do it in WinForms

byte[] binaryData = Convert.FromBase64String(bgImage64);
image = Image.FromStream(new MemoryStream(binaryData));

but how do i do the same thing in WPF?

like image 586
LDomagala Avatar asked Feb 27 '09 03:02

LDomagala


2 Answers

byte[] binaryData = Convert.FromBase64String(bgImage64);

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = new MemoryStream(binaryData);
bi.EndInit();

Image img = new Image();
img.Source = bi;
like image 63
joshperry Avatar answered Nov 09 '22 13:11

joshperry


Expanding on Josh's answer above - here's one that uses a ValueConverter so you can use binding instead of setting the image source in the code-behind.

Base64ImageConverter.cs

public class Base64ImageConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string s = value as string;

        if (s == null)
            return null;

        BitmapImage bi = new BitmapImage();

        bi.BeginInit();
        bi.StreamSource = new MemoryStream(System.Convert.FromBase64String(s));
        bi.EndInit();

        return bi;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

ImageData.cs (used as a data source)

public class ImageData
{
    public string Base64ImageData { get; set; }

    public ImageData()
    {
        this.Base64ImageData = "iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAgMSURBVHja7Jl7jFXFHcc/55y5j32C+wCVXV6KwAIrbUBQeUhiixQEiYrUGNC0Bq2CFqsNjSYUrQg+Uk1JbW0VW0I3BCGgFSlYQKBSqKK8W6lCgQ0s7sKye++eOefMTP+4d+/ee3eRpa3ipvySyZnc+5s5v+/vOb85ljGGjkw2HZwuArgI4P8dgAU4QCT57EikACmAwv3ff6SuqF8vSh74HiYa5Y1lf2PJkr+QSLECCFjx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WmBcBkaAVjsrSUNWj9G+fB2zx8X+H5AUOG9+IPtxZy+dM/4rcjA8aOrcDzFVq38R5MpgUSvm9lmeyrGY5jIxyH1Ss+5v1jitLhwzno5fLakg+xk16RvSbhQiYTgGVduMA1xlBQGKGmuIzRNcP4KL8nl5fmpLTcVmuecS9kzIUtYJZloZXm+Ze34boeuz46QjjstNnQt4BOS6PNm3wd8no0Gkq1nWcTKV1Wu9mEFxrAeVtM63QXSvhTQUG0w33gEM0XL2fO1HfEDzTKAroCg4DSDib8SWC3lWx6C5LPjkQSaLAufqm/COAigIsALij9ewBzvhamSg2pRgAAAABJRU5ErkJggg==";
    }
}

Window1.xaml

<Window x:Class="ImageFromData.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ImageFromData"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <local:Base64ImageConverter x:Key="Base64ImageConverter"/>
        <local:ImageData x:Key="ImageData"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource ImageData}">
        <Image Source="{Binding Base64ImageData, Converter={StaticResource Base64ImageConverter}}"/>
    </Grid>
</Window>

(root namespace for project is ImageFromData)

like image 37
geofftnz Avatar answered Nov 09 '22 13:11

geofftnz