我可以将图像/位图转换为writeablebitmap吗?

我有两个位图图像,我想合并在一起。 我发现我可以用writeablebitmap做到这一点,但我如何首先将这些图像转换为writeablebitmaps?

更新:我找不到直接将位图转换为可写位图的方法,所以我所做的是将我的位图写入独立存储并在流对象中再次读取它。 之后,Xyroid下面给出的代码可用于合并图像并将合并的图像转换为位图。

在这里,我给你合并两个图像的代码。 WinRT的WriteableBitmap是不同的,它的构造函数将高度和宽度作为参数。 我已经将WriteableBitmapEx用于某些function。

XAML

           

C#

 protected async override void OnNavigatedTo(NavigationEventArgs e) { StorageFile destiFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("Merged.png", CreationCollisionOption.ReplaceExisting); WriteableBitmap wb; wb = await Render(); using (IRandomAccessStream stream = await destiFile.OpenAsync(FileAccessMode.ReadWrite)) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync( BitmapEncoder.PngEncoderId, stream); Stream pixelStream = wb.PixelBuffer.AsStream(); byte[] pixels = new byte[pixelStream.Length]; await pixelStream.ReadAsync(pixels, 0, pixels.Length); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)wb.PixelWidth, (uint)wb.PixelHeight, 96.0, 96.0, pixels); await encoder.FlushAsync(); } var bitmp = new BitmapImage(); using (var strm = await destiFile.OpenReadAsync()) { bitmp.SetSource(strm); imgTarget.Source = bitmp; } } private async Task Render() { var Assets = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets"); StorageFile file1 = await Assets.GetFileAsync("img1.png"); StorageFile file2 = await Assets.GetFileAsync("img2.png"); BitmapImage i1 = new BitmapImage(); BitmapImage i2 = new BitmapImage(); using (IRandomAccessStream strm = await file1.OpenReadAsync()) { i1.SetSource(strm); } using (IRandomAccessStream strm = await file2.OpenReadAsync()) { i2.SetSource(strm); } WriteableBitmap img1 = new WriteableBitmap(i1.PixelWidth, i1.PixelHeight); WriteableBitmap img2 = new WriteableBitmap(i2.PixelWidth, i2.PixelHeight); using (IRandomAccessStream strm = await file1.OpenReadAsync()) { img1.SetSource(strm); } using (IRandomAccessStream strm = await file2.OpenReadAsync()) { img2.SetSource(strm); } WriteableBitmap destination = new WriteableBitmap((int)(img1.PixelWidth > img2.PixelWidth ? img1.PixelWidth : img2.PixelWidth), (int)(img1.PixelHeight + img1.PixelHeight)); destination.Clear(Colors.White); destination.Blit(new Rect(0, 0, (int)img1.PixelWidth, (int)img1.PixelHeight),img1,new Rect(0, 0, (int)img1.PixelWidth, (int)img1.PixelHeight)); destination.Blit(new Rect(0, (int)img1.PixelHeight, (int)img2.PixelWidth, (int)img2.PixelHeight), img2, new Rect(0, 0, (int)img2.PixelWidth, (int)img2.PixelHeight)); return destination; } 

请注意,您必须添加System.Runtime.InteropServices.WindowsRuntime命名空间。

更新1

假设你已经有两个BitmapImage img1img2 ,那么就这样做

 protected async override void OnNavigatedTo(NavigationEventArgs e) { StorageFile destiFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("Merged.png", CreationCollisionOption.ReplaceExisting); WriteableBitmap wb; wb = await Render(); using (IRandomAccessStream stream = await destiFile.OpenAsync(FileAccessMode.ReadWrite)) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync( BitmapEncoder.PngEncoderId, stream); Stream pixelStream = wb.PixelBuffer.AsStream(); byte[] pixels = new byte[pixelStream.Length]; await pixelStream.ReadAsync(pixels, 0, pixels.Length); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)wb.PixelWidth, (uint)wb.PixelHeight, 96.0, 96.0, pixels); await encoder.FlushAsync(); } var bitmp = new BitmapImage(); using (var strm = await destiFile.OpenReadAsync()) { bitmp.SetSource(strm); imgTarget.Source = bitmp; } } private async Task Render() { WriteableBitmap destination = new WriteableBitmap((int)(img1.PixelWidth > img2.PixelWidth ? img1.PixelWidth : img2.PixelWidth), (int)(img1.PixelHeight + img1.PixelHeight)); destination.Clear(Colors.White); destination.Blit(new Rect(0, 0, (int)img1.PixelWidth, (int)img1.PixelHeight),img1,new Rect(0, 0, (int)img1.PixelWidth, (int)img1.PixelHeight)); destination.Blit(new Rect(0, (int)img1.PixelHeight, (int)img2.PixelWidth, (int)img2.PixelHeight), img2, new Rect(0, 0, (int)img2.PixelWidth, (int)img2.PixelHeight)); return destination; } 

我在Silverlight上做了很多工作,我相信商店应用程序在很多方面都类似。

考虑这个构造函数:

 WriteableBitmap(BitmapSource) - Initializes a new instance of the WriteableBitmap class using the provided BitmapSource. 

接下来的问题是,如何从图像中获取“BitmapSource”? 你可以这样做:

 (BitmapSource)MyImage.Source 

虽然这假设源(类型为’ImageSource’)实际上是’BitmapSource’实例。 也就是说,从Silverlight 5.0开始,Silverlight中从ImageSource派生的唯一类是BitmapSource,所以我怀疑这是一个问题。

所以这样的事情可能有用:

 WriteableBitmap((BitmapSource)MyImage.Source) 

最后,这里有一个开源项目可能会有所帮助: http : //writeablebitmapex.codeplex.com/