blob: 27a05c39da2578caf71821f46b011fd0b80809a9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
public static class ByteArrayExtensions
{
/// <summary>
/// Creates a new BitmapImage from the byte array.
/// </summary>
/// <param name="data">The image data.</param>
/// <returns></returns>
private static BitmapImage ToBitmapImage(this byte[] data)
{
if (data == null || data.Length == 0) return null;
var image = new BitmapImage();
using (var mem = new MemoryStream(data))
{
mem.Position = 0;
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = null;
image.StreamSource = mem;
image.EndInit();
}
image.Freeze();
return image;
}
}
|