我可以从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);