How to save a canvas having more than one image to the picture library in windows store app using c# and xaml

怎甘沉沦 提交于 2019-12-13 05:38:49

问题


I am creating an application for windows 8 store app using c# and xaml.I have used a canvas where more than one images are added now i want t save the canvas to the picture library .

Thanks :)


回答1:


You can use WinRT XAML Toolkit's Composition library. Build your UI as you would to display on screen, call Measure and Arrange on the root element to size it to your output image dimensions and use one of the extension methods - WriteableBitmapRenderExtensions.Render() or .RenderToPngStream() to render to a WriteableBitmap or MemoryStream respectively. You can then save the bitmap or stream to a file if you enable pictures library access capability in your app's manifest.




回答2:


I lost so much time trying to render UI elements using SharpDX and WinRt toolkit without any results, after which found that there exists a very nice class: RenderTargetBitmap, which can be used for such tasks. Here is a code snippet copeid from this web site http://mariusbancila.ro/blog/2013/11/05/render-the-screen-of-a-windows-store-app-to-a-bitmap-in-windows-8-1/comment-page-1

async Task<RenderTargetBitmap> CaptureToStreamAsync(FrameworkElement  uielement, IRandomAccessStream stream, Guid encoderId)
{
   try
   {
  var renderTargetBitmap = new RenderTargetBitmap();
  await renderTargetBitmap.RenderAsync(uielement);

  var pixels = await renderTargetBitmap.GetPixelsAsync();

  var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
  var encoder = await BitmapEncoder.CreateAsync(encoderId, stream);
  encoder.SetPixelData(
      BitmapPixelFormat.Bgra8,
      BitmapAlphaMode.Ignore,
      (uint)renderTargetBitmap.PixelWidth,
      (uint)renderTargetBitmap.PixelHeight,
      logicalDpi,
      logicalDpi,
      pixels.ToArray());

  await encoder.FlushAsync();

  return renderTargetBitmap;
}

catch (Exception ex)
{
   DisplayMessage(ex.Message);
}

return null;
}


来源:https://stackoverflow.com/questions/19223460/how-to-save-a-canvas-having-more-than-one-image-to-the-picture-library-in-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!