如何将网格作为位图保存在Windows 8的本地存储中

时间:2014-07-10 03:46:09

标签: windows-8 writeablebitmap writeablebitmapex

我是Windows 8应用程序开发的新手。我正在开发一个应用程序,它需要将网格保存为本地存储中的位图图像。 我尝试使用可写位图,但我没有得到解决方案。

然后我搜索了样本,但我得到的答案是“不可能”#39;但在某些答案中,我发现使用' 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;
     }

1 个答案:

答案 0 :(得分:0)

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

http://kodierer.blogspot.de/2013/12/easy-render-writeablebitmapex-with.html http://kodierer.blogspot.de/2013/12/how-to-encode-and-save-writeablebitmap.html

这是一个代码段:

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