加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c#-4.0 – 如何从BitmapImage创建WriteableBitmap?

发布时间:2020-12-15 17:45:43 所属栏目:百科 来源:网络整理
导读:我可以从Assets中的图片创建WriteableBitmap. Uri imageUri1 = new Uri("ms-appx:///Assets/sample1.jpg");WriteableBitmap writeableBmp = await new WriteableBitmap(1,1).FromContent(imageUri1); 但是,我无法从图片目录创建WriteableBitmap,(我正在使用W
我可以从Assets中的图片创建WriteableBitmap.
Uri imageUri1 = new Uri("ms-appx:///Assets/sample1.jpg");
WriteableBitmap writeableBmp = await new WriteableBitmap(1,1).FromContent(imageUri1);

但是,我无法从图片目录创建WriteableBitmap,(我正在使用WinRT XAML Toolkit)

//open image
StorageFolder picturesFolder = KnownFolders.PicturesLibrary;
StorageFile file = await picturesFolder.GetFileAsync("sample2.jpg");
var stream = await file.OpenReadAsync();

//create bitmap
BitmapImage bitmap2 = new BitmapImage();
bitmap2.SetSource();
bitmap2.SetSource(stream);

//create WriteableBitmap,but cannot
WriteableBitmap writeableBmp3 = 
    await WriteableBitmapFromBitmapImageExtension.FromBitmapImage(bitmap2);

它是否正确 ?

解决方法

这是一个完全的设计,但似乎确实有效……
// load a jpeg,be sure to have the Pictures Library capability in your manifest
var folder = KnownFolders.PicturesLibrary;
var file = await folder.GetFileAsync("test.jpg");
var data = await FileIO.ReadBufferAsync(file);

// create a stream from the file
var ms = new InMemoryRandomAccessStream();
var dw = new Windows.Storage.Streams.DataWriter(ms);
dw.WriteBuffer(data);
await dw.StoreAsync();
ms.Seek(0);

// find out how big the image is,don't need this if you already know
var bm = new BitmapImage();
await bm.SetSourceAsync(ms);

// create a writable bitmap of the right size
var wb = new WriteableBitmap(bm.PixelWidth,bm.PixelHeight);
ms.Seek(0);

// load the writable bitpamp from the stream
await wb.SetSourceAsync(ms);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读