7

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

这个对吗 ?

4

3 回答 3

6

这是一个完全的发明,但它似乎确实有效......

// 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);
于 2013-02-06T06:00:59.247 回答
4

正如 Filip 所说,这是将图像读取到 WriteableBitmap 的方式:

StorageFile imageFile = ...

WriteableBitmap writeableBitmap = null;
using (IRandomAccessStream imageStream = await imageFile.OpenReadAsync())
{
   BitmapDecoder bitmapDecoder = await BitmapDecoder.CreateAsync(
      imageStream);

   BitmapTransform dummyTransform = new BitmapTransform();
   PixelDataProvider pixelDataProvider =
      await bitmapDecoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8, 
      BitmapAlphaMode.Premultiplied, dummyTransform, 
      ExifOrientationMode.RespectExifOrientation,
      ColorManagementMode.ColorManageToSRgb);
   byte[] pixelData = pixelDataProvider.DetachPixelData();

   writeableBitmap = new WriteableBitmap(
      (int)bitmapDecoder.OrientedPixelWidth,
      (int)bitmapDecoder.OrientedPixelHeight);
   using (Stream pixelStream = writeableBitmap.PixelBuffer.AsStream())
   {
      await pixelStream.WriteAsync(pixelData, 0, pixelData.Length);
   }
}

请注意,我使用的是可写位图使用的像素格式和 alpha 模式,并且我通过了 .

于 2013-02-06T07:22:23.300 回答
3

WriteableBitmapFromBitmapImageExtension.FromBitmapImage() works by using the original Uri used to load BitmapImage and IIRC it only works with BitmapImages from the appx. In your case there isn't even a Uri since loading from Pictures folder can only be done by loading from stream, so your options from the fastest to slowest (I think) are:

  1. Open the image as WriteableBitmap from the get go, so that you don't need to reopen it or copy bits around.
  2. If you need to have two copies - open it as WriteableBitmap and then create a new WriteableBitmap of same size and copy the pixel buffer.
  3. If you need to have two copies - track the path used to open the first bitmap and then create a new WriteableBitmap by loading it from the same file as the original one.

I think option 2 might be faster than option 3 since you avoid decoding a compressed image twice.

于 2013-02-06T02:10:49.340 回答