Xaml
Файл MainWindow.xaml
<Window x:Class="WpfAppImageBase64.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAppImageBase64"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Image x:Name="MyControlImg"/>
</Grid>
</Window>
C#
Файл MainWindow.xaml.cs
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
namespace WpfAppImageBase64
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
string imageBase64 = "Qk04JAAAAAAAADYAAAAoAAAAYAAAACAAAAABABgAAAAAAAIk......."; // image in base 64
// Note! Please download full example code to see correct imageBase64 value
try
{
var image = Base64StringToImage(imageBase64);
MyControlImg.Source = ConvertImageToBitmapImage(image);
}
catch (Exception)
{
}
}
public static Image Base64StringToImage(string base64String)
{
byte[] byteBuffer = Convert.FromBase64String(base64String);
MemoryStream memoryStream = new MemoryStream(byteBuffer);
memoryStream.Position = 0;
return Image.FromStream(memoryStream);
}
public static BitmapImage ConvertImageToBitmapImage(Image image)
{
using (var memory = new MemoryStream())
{
image.Save(memory, ImageFormat.Bmp);
memory.Position = 0;
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
return bitmapImage;
}
}
}
}