Windows 8 如何在windows 8的本地存储中将网格另存为位图图像

Windows 8 如何在windows 8的本地存储中将网格另存为位图图像,windows-8,writeablebitmap,writeablebitmapex,Windows 8,Writeablebitmap,Writeablebitmapex,我对Windows8应用程序开发非常陌生。我正在开发一个应用程序,需要将网格保存为本地存储中的位图图像。 我尝试过使用可写位图,但没有得到解决方案 然后我搜索了样本,但我得到的答案是“不可能”。但在一些答案中,我发现通过使用“WriteableBitmapEx”,我们可以做到这一点。但我不知道如何使用这个库来实现这一点。如果有人知道,请回复我 提前谢谢 编辑 <Canvas Background="Cyan" Name="panelcanvas" Margin="47.5,57,327.5

我对Windows8应用程序开发非常陌生。我正在开发一个应用程序,需要将网格保存为本地存储中的位图图像。 我尝试过使用可写位图,但没有得到解决方案

然后我搜索了样本,但我得到的答案是“不可能”。但在一些答案中,我发现通过使用“WriteableBitmapEx”,我们可以做到这一点。但我不知道如何使用这个库来实现这一点。如果有人知道,请回复我

提前谢谢

编辑

<Canvas Background="Cyan" Name="panelcanvas" Margin="47.5,57,327.5,153"  
     Width="200"  
     Height="400">
        <Image Name="maskimg"  
               Height="100" Width="220"/>
        <ScrollViewer ZoomMode="Enabled" Name="scroll">  
            <Image  Name="img" Stretch="Fill" Canvas.Top="15"
              Height="400" Width="200" Source="mask_image.png">
                <Image.RenderTransform>
                    <CompositeTransform x:Name="Composite_Transform"></CompositeTransform>
                </Image.RenderTransform>
            </Image>
        </ScrollViewer>

    </Canvas>

    <Image Name="maskimg2" HorizontalAlignment="left" Width="200"  
     Height="400"/>
    <Image Name="maskimg3" HorizontalAlignment="Right"  Width="200"  
     Height="400"/>
C代码

  var destBmp = BitmapFactory.New((int)panelcanvas.ActualWidth, (int)panelcanvas.ActualHeight);        
  foreach (var child in panelcanvas.Children)
        {                
            var img = child as Image;
            if (img != null)
            {
                   var sourceMask = ((BitmapImage)img.Source);
                   var sourceUriMask = sourceMask.UriSource;
                   Uri imageUri = new Uri(strMask.ToString());                       
                    var srcBmp = await new WriteableBitmap(1, 1).FromContent(imageUri);
                    if (srcBmp != null)
                    {
                        var x = Canvas.GetLeft(img);                            
                        var y = Canvas.GetTop(img);                           
                        destBmp.Blit(new Rect(x, y, srcBmp.PixelWidth, srcBmp.PixelHeight),                             srcBmp, new Rect(0, 0, srcBmp.PixelWidth, srcBmp.PixelHeight));
                    }
            }
            await WriteableBitmapToStorageFile(destBmp, FileFormat.Jpeg);

    private async Task<StorageFile> WriteableBitmapToStorageFile(WriteableBitmap WB, FileFormat   fileFormat)
    {
        string FileName = "MyFile.jpeg";
        StorageFolder localFolder = ApplicationData.Current.LocalFolder;
        var file = await localFolder.CreateFileAsync(FileName, CreationCollisionOption.ReplaceExisting);

        using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
         {
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, 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,
                                200,
                                200,
                                0,
                                0,
                                pixels);
            await encoder.FlushAsync();
        }
        return file;
     }

这篇博客文章指出了使用WriteableBitmapEx库需要做什么。请注意,Windows 8.1引入了所需的RenderTargetBitmap!对于Windows 8.0,这根本不可能

下面是一段代码片段:

// Render some UI to a RenderTargetBitmap
var renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(MyGrid);

// Get the pixel buffer and copy it into a WriteableBitmap
var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
var width = renderTargetBitmap.PixelWidth;
var height = renderTargetBitmap.PixelHeight;
WriteableBitmap wbmp = await new WriteableBitmap(1, 1).FromPixelBuffer(pixelBuffer, width, height);

SaveWriteableBitmapAsJpeg(wbmp, "mygrid.jpg");

private static async Task SaveWriteableBitmapAsJpeg(WriteableBitmap bmp, string fileName)
{
   // Create file in Pictures library and write jpeg to it
   var outputFile = await KnownFolders.PicturesLibrary.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
   using (var writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
   {
      await EncodeWriteableBitmap(bmp, writeStream, BitmapEncoder.JpegEncoderId);
   }
   return outputFile;
}

private static async Task EncodeWriteableBitmap(WriteableBitmap bmp, IRandomAccessStream writeStream, Guid encoderId)
{         
   // Copy buffer to pixels
   byte[] pixels;
   using (var stream = bmp.PixelBuffer.AsStream())
   {
      pixels = new byte[(uint) stream.Length];
      await stream.ReadAsync(pixels, 0, pixels.Length);
   }

   // Encode pixels into stream
   var encoder = await BitmapEncoder.CreateAsync(encoderId, writeStream);
   encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied,
      (uint) bmp.PixelWidth, (uint) bmp.PixelHeight,
      96, 96, pixels);
   await encoder.FlushAsync();
}

本地存储是指用于存储键/值对的本地存储,还是指一般的本地计算机存储?您能否确认这是XAML/WinRT Windows应用商店应用程序还是桌面WPF应用程序?Windows phone中类似StorageFolder的隔离存储。这是XAMl/WinRt windows应用商店应用程序。我正在为windows tablet开发此应用程序。WriteableBitmapEx创建一个包含许多扩展方法的位图图像。所以,只要使用这个库制作可写位图并探索它的方法,你就会得到一些东西。谢谢。但是我的应用程序应该与Windows8兼容。我的目标基本上是Windows8平板电脑。我通过看电影找到了一种解决办法。通过使用它,我可以将画布作为图像保存到本地存储。但我的画布子对象之一是scrollviewer,其中包含图像。在这里,我无法存储带有该图像的画布。我已经编辑了我的代码,请检查并回复我。提前准备好。