转换字节数组或文件存储位图图像 [英] Convert byte Array or File Storage to Bitmap Image

查看:81
本文介绍了转换字节数组或文件存储位图图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我选择文件后,存储文件,我怎么能转换这个文件是שN个图像,以类似资料图片显示呢?结果
我将该文件转换为字节数组,但不知道是什么下一步或有一个其他的方式?



下面是我的代码:

  VAR openPicker =新的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文件=等待openPicker.PickSingleFileAsync();

VAR流=等待file.OpenReadAsync();

使用(VAR的DataReader =新的DataReader(流))
{
变种字节=新的字节[stream.Size]
等待dataReader.LoadAsync((UINT)stream.Size);
dataReader.ReadBytes(字节);
变种流2 =新的MemoryStream(字节);
}


解决方案

下面的代码转换字节成的BitmapImage

 的BitmapImage此搜索=新的BitmapImage(); 
InMemoryRandomAccessStream毫秒​​=新InMemoryRandomAccessStream();
ms.WriteAsync(tBytes.AsBuffer());
ms.FlushAsync()AsTask()等待()。
ms.Seek(0);
image1.SetSource(毫秒);
image.Source = image1的;



结果
我得到这个从什么地方,试试这个,如果有帮助。



  FileOpenPicker openPicker =新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文件=等待openPicker.PickSingleFileAsync();
IRandomAccessStream流=等待file.OpenAsync(FileAccessMode.Read);
的BitmapImage BMP =新的BitmapImage();
bmp.SetSource(流);
Image1.Source = BMP;


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);
  }

解决方案

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;

这篇关于转换字节数组或文件存储位图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆