A.R
A.R

Reputation: 173

Convert byte Array or File Storage to Bitmap Image

After I pick file to storage file , How can I convert this file to be שn image in order to display it like profile picture?
I converted the file to byte array but don't know what to do next or there is an other way?

Here is my code :

var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
openPicker.FileTypeFilter.Add(".png");
openPicker.FileTypeFilter.Add(".Jpeg");
openPicker.FileTypeFilter.Add(".Jpg");
StorageFile file = await openPicker.PickSingleFileAsync();

var stream = await file.OpenReadAsync();

using (var dataReader = new DataReader(stream))
  {
      var  bytes = new byte[stream.Size];
      await dataReader.LoadAsync((uint)stream.Size);
      dataReader.ReadBytes(bytes);
      var stream2 = new MemoryStream(bytes);
  }

Upvotes: 0

Views: 5394

Answers (1)

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

Below code converts bytes into BitmapImage

BitmapImage image1 = new BitmapImage();
InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream();
ms.WriteAsync(tBytes.AsBuffer());
ms.FlushAsync().AsTask().Wait();
ms.Seek(0);
image1.SetSource(ms);
image.Source = image1;


I got this from somewhere, Try this if it helps

FileOpenPicker openPicker = new FileOpenPicker();
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".cmp");
openPicker.FileTypeFilter.Add(".png");
openPicker.FileTypeFilter.Add(".tif");
openPicker.FileTypeFilter.Add(".gif");
openPicker.FileTypeFilter.Add(".bmp");
StorageFile file = await openPicker.PickSingleFileAsync();
IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
BitmapImage bmp = new BitmapImage();
bmp.SetSource(stream);
Image1.Source = bmp;

Upvotes: 4

Related Questions