c#


Convert byte[] to Windows.UI.Xaml.Media.Imaging.BitmapImage


Working on a Universal Windows App and trying to bind an image that is a byte array in the xaml. I'm a little lost. In the xaml I have something like:
<Image Source="{Binding SelectedImageSource}"
Stretch="UniformToFill"
Grid.Row="1"/>
And in my view model I have
private byte[] _selectedImageSource;
public byte[] SelectedImageSource
{
get { return _selectedImageSource; }
set
{
_selectedImageSource = value;
OnPropertyChanged(nameof(SelectedImageSource));
}
}
But I can't see the image here. What I think I need to do is to convert the byte[] to a Windows.UI.Xaml.Media.Imaging.BitmapImage. I am not 100% sure though how to do that and if thats even the right thing to do.
Try this:
public static async Task<BitmapImage> GetBitmapAsync(byte[] data)
{
var bitmapImage = new BitmapImage();
using (var stream = new InMemoryRandomAccessStream())
{
using (var writer = new DataWriter(stream))
{
writer.WriteBytes(data);
await writer.StoreAsync();
await writer.FlushAsync();
writer.DetachStream();
}
stream.Seek(0);
await bitmapImage.SetSourceAsync(stream);
}
return bitmapImage;
}
You can also try simpler version described here, but I think I had some problems with it, that's why I use code from above now.
#Andrey Ashikhmin your way is so good.
But I also have another way that is use WriteableBitmap. You can set image by URI and File.
UWP can convert WriteableBitmap and byte[]
Change the property as
private WriteableBitmap _selectedImageSource;
public WriteableBitmap SelectedImageSource
{
get { return _selectedImageSource; }
set
{
_selectedImageSource = value;
OnPropertyChanged(nameof(SelectedImageSource));
}
}
And set byte[] to WriteableBitmap
private async Task<ImageSource> FromBase64(byte[] bytes)
{
var image = bytes.AsBuffer().AsStream().AsRandomAccessStream();
// decode image
var decoder = await BitmapDecoder.CreateAsync(image);
image.Seek(0);
// create bitmap
var output = new WriteableBitmap((int)decoder.PixelHeight, (int)decoder.PixelWidth);
await output.SetSourceAsync(image);
return output;
}
If you want to read from file,use
private static async Task<WriteableBitmap> OpenWriteableBitmapFile(StorageFile file)
{
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
WriteableBitmap image = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
image.SetSource(stream);
return image;
}
}
If you want to convert WriteableBitmap to byte[] ,see : http://lindexi.oschina.io/lindexi/post/win10-uwp-%E8%AF%BB%E5%8F%96%E4%BF%9D%E5%AD%98WriteableBitmap-BitmapImage/
Thx https://codepaste.net/ijx28i give the code.

Related Links

Convert string to enum, but when enum type is only known at runtime
how can I read each filed of json
c# chart axis not displaying when form is loaded
Visual Studio Xamarin signing packages archive failed could not read as zip
Cannot implicitly convert type 'String' to 'Int' error on visual [duplicate]
Async Await Persistant Warning
Pasting complex Word objects from ClipBoard
Wcf Service and SignalR FormsAuthentication
transform formula given as a string into expression tree and apply to array
Xamarin Forms Embeded resource image as background for ContentPage in XAML
How the trick is to take a picture in the database and then bring dipicturebox [duplicate]
visual studio: cannot get full list of properties of a build configuration
Sub-sub-Treeview (WPF)
Randomly Choose A Windows Form Control
Calling Twilio API from C#/.NET through a Proxy Server
Progress Bar in Theme

Categories

HOME
qlikview
max
msbuild
intellij-idea
packet
mysqli
watson-iot
sms
opengl-es
mediarecorder
ms-access-2010
install
doctrine2
cracking
theory
google-search-appliance
unreal-engine4
logstash-configuration
loss
gomobile
utc
key-bindings
queryover
origami
keychain
g77
history
wink
gige-sdk
jenkins-docker
openpgp.js
group
huawei
sharing
xcode7.1
geokit
syslog4j
normalisation
hotkeys
stdlist
nugetgallery
bootstrap-tabs
borland-c++
eaaccessory
dcg
cakephp-2.6
beagleboard
static-files
cordova-3
information-hiding
report-builder2.0
apache-pivot
formbuilder
agent-based-modeling
selenium-grid2
sat4j
disparity-mapping
smartxls
opencobol
new-operator
power-management
nvcc
node-amqp
complete
lastinsertid
ironworker
xrandr
jython-2.5
coinbase-php
idoc
quit
configurationsection
systrace
eeprom
cherokee
recess
windows-xp-embedded
zipstream
norton
llblgen
html-help-workshop
ironpython-studio

Resources

Encrypt Message