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

标签 c#-4.0 windows-8 windows-runtime writeablebitmap

我可以从资源中的图片创建 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);

关于c#-4.0 - 如何从 BitmapImage 创建 WriteableBitmap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14718855/

相关文章:

c# - 如何在winrt中解压SharpZipLib压缩的字符串?

c# - 如何使用 C# WebAPI 2 拥有两条使用相同方法的路由

c# - 立即取消所有正在运行的C# Parallel.Foreach 线程

asp.net-mvc-3 - 设置默认依赖项而不使用第三方依赖项解析器

c++ - 如何在 C++/CX Windows 8 应用程序中流式传输原始合成 PCM 音频?

Windows Store App 使用私钥解密

angularjs - 从 Angular js 到 Controller 的 webapi 调用不起作用

windows-8 - 如何开始将 Windows 8 应用程序移植到 Windows Phone 8?

windows-8 - 使用代码歌唱将桌面应用程序提交到 Windows 8 商店

c# - 具有自定义类的 Windows 8 应用程序漫游存储