【问题标题】:How to save a canvas having more than one image to the picture library in windows store app using c# and xaml如何使用 c# 和 xaml 将具有多个图像的画布保存到 Windows 商店应用程序中的图片库
【发布时间】:2013-10-07 11:18:16
【问题描述】:

我正在使用 c# 和 xaml 为 Windows 8 商店应用程序创建一个应用程序。我使用了一个画布,其中添加了多个图像,现在我想将画布保存到图片库中。

谢谢:)

【问题讨论】:

  • 以上链接适用于 Windows 8.1。我需要它用于 Windows 8
  • 可能是某个第三方。需要搜索一下。
  • 转换到 8.1 会更好

标签: xaml canvas windows-store-apps


【解决方案1】:

您可以使用 WinRT XAML 工具包的组合库。像在屏幕上显示一样构建您的 UI,在根元素上调用 MeasureArrange 以将其调整为输出图像尺寸,并使用扩展方法之一 - WriteableBitmapRenderExtensions.Render().RenderToPngStream() 渲染到WriteableBitmapMemoryStream 分别。然后,如果您在应用的清单中启用图片库访问功能,则可以将位图或流保存到文件中。

【讨论】:

  • Filip 我正在尝试使用 RenderToPngStream 作为波纹管 var canvas = cvPicFrame as FrameworkElement; MemoryStream t1 = 等待 WriteableBitmapRenderExtensions.RenderToPngStream(canvas);字节[] st = t1.ToArray();但我收到以下错误
  • mscorlib.dll 中出现“SharpDX.SharpDXException”类型的未处理异常附加信息:HRESULT:[0x88990015],模块:[SharpDX.Direct2D1],ApiCode:[D2DERR_WRONG_RESOURCE_DOMAIN/WrongResourceDomain],消息:资源在错误的渲染目标上实现。
  • Filip 如果您能对此做出回应,将会非常有帮助
  • 有这个错误的解决方案吗?我的代码中有同样的错误。看起来问题出在 ImageRenderer 类上: var bitmap = await image.Source.ToSharpDX(renderTarget); renderTarget.DrawBitmap(位图,矩形,(浮动)图像。不透明度,D2D.BitmapInterpolationMode.Linear);但我不知道如何解决这个问题..
【解决方案2】:

我花了很多时间尝试使用 SharpDX 和 WinRt 工具包渲染 UI 元素而没有任何结果,之后发现有一个非常好的类:RenderTargetBitmap,可以用于此类任务。 这是来自该网站的代码 sn-p copid 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多