2013-02-05 28 views
6

Tôi có thể tạo WriteableBitmap từ ảnh trong Nội dung.Làm thế nào để tạo WriteableBitmap từ BitmapImage?

Uri imageUri1 = new Uri("ms-appx:///Assets/sample1.jpg"); 
WriteableBitmap writeableBmp = await new WriteableBitmap(1, 1).FromContent(imageUri1); 

nhưng, tôi không thể tạo WriteableBitmap từ Hình Directory, (Tôi đang sử dụng 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); 

Đây có phải là đúng?

Trả lời

5

Đây là một tổng bày ra, nhưng nó dường như để làm việc ...

// 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); 
3

WriteableBitmapFromBitmapImageExtension.FromBitmapImage() hoạt động bằng cách sử dụng Uri gốc được sử dụng để tải BitmapImage và IIRC nó chỉ hoạt động với BitmapImage s từ appx. Trong trường hợp của bạn thậm chí không có một Uri kể từ khi nạp từ thư mục Hình chỉ có thể được thực hiện bằng cách tải từ suối, vì vậy lựa chọn của bạn từ nhanh nhất để chậm nhất (tôi nghĩ) là:

  1. Mở hình ảnh như WriteableBitmap từ có được đi, do đó bạn không cần phải mở lại hoặc sao chép bit xung quanh.
  2. Nếu bạn cần có hai bản sao - hãy mở nó làm WriteableBitmap và sau đó tạo WriteableBitmap mới có cùng kích thước và sao chép bộ đệm pixel.
  3. Nếu bạn cần có hai bản sao - hãy theo dõi đường dẫn được sử dụng để mở bitmap đầu tiên và sau đó tạo một WriteableBitmap mới bằng cách tải nó từ cùng một tệp như bản gốc.

Tôi nghĩ tùy chọn 2 có thể nhanh hơn tùy chọn 3 vì bạn tránh giải mã ảnh nén hai lần.

+0

Cảm ơn bạn đã trả lời, nhưng làm thế nào để mở hình ảnh dưới dạng writeablebitmap từ get go ,? Tôi nghĩ rằng [this] (http://stackoverflow.com/questions/4254225/opening-an-image-file-into-writablebitmap) có thể hữu ích, nhưng tôi không thể không vượt qua một bitmap trực tiếp vào một writablebitmap. – a2hiro

+0

Không có liên kết nào dành cho Silverlight. Bạn cần có một StorageFile cho hình ảnh của bạn, ví dụ: bằng cách sử dụng bộ chọn tệp, sau đó chờ OpenReadAsync, sau đó tạo WriteableBitmap 1x1 và chờ SetSourceAsync. Kiểm tra điều này cho mẫu/tiện ích mở rộng: http://winrtxamltoolkit.codeplex.com/SourceControl/changeset/view/4d568d4e4c6a#WinRTXamlToolkit/Imaging/WriteableBitmapLoadExtensions.cs –

4

Đây là cách đọc một hình ảnh vào một WriteableBitmap hoạt động như Filip lưu ý:

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

Lưu ý rằng tôi đang sử dụng định dạng pixel và chế độ alpha Writeable Bitmap sử dụng và tôi vượt qua.

Các vấn đề liên quan