【问题标题】:Converitng Canvas to .png Image. Malfunctioning in WPF app将画布转换为 .png 图像。 WPF 应用程序出现故障
【发布时间】:2012-10-29 19:51:56
【问题描述】:

我有一些方法可以将 System.Windows.Controls.Canvas 控件转换为 .png 图像并保存在目录中。我为某些 List 调用此函数,并在某个目录中获取所有图像。它工作正常,但有时此函数会随机生成一些空白图像,再次调用此函数也会完美生成所有图像。这种故障背后的原因可能是什么? 下面是函数

  public bool GenerateICards(Canvas surface)
        {
            bool retVal = false;
            try
            {
                // Save current canvas transform
                Transform transform = surface.LayoutTransform;
                // reset current transform (in case it is scaled or rotated)
                surface.LayoutTransform = null;

                // Get the size of canvas
                Size size = new Size(surface.Width, surface.Height);

                // Measure and arrange the surface
                // VERY IMPORTANT
                surface.Measure(size);
                surface.Arrange(new Rect(size));

                // Create a render bitmap and push the surface to it
                RenderTargetBitmap renderBitmap =
                  new RenderTargetBitmap(
                    (int)size.Width,
                    (int)size.Height,
                    96d,
                    96d,
                    PixelFormats.Pbgra32);
                renderBitmap.Render(surface);

                // Create a file stream for saving image
                using (FileStream outStream = new FileStream(SessionHelper.Path.PrintImage, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    // Use png encoder for our data
                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    // push the rendered bitmap to it
                    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                    // save the data to the stream
                    encoder.Save(outStream);
                }
                // Restore previously saved layout
                surface.LayoutTransform = transform;
                // i++;
                retVal = true;
            }
            //}
            catch (Exception)
            {
                ErrorLbl = "Error occured while generating temporary images.";
            }
            return retVal;
        }

【问题讨论】:

标签: c# wpf wpf-controls


【解决方案1】:

刚刚更换

renderBitmap.Render(surface);

函数中的行(上述问题中的函数)与

 Rect bounds = VisualTreeHelper.GetDescendantBounds(surface);
                DrawingVisual dv = new DrawingVisual();
                using (DrawingContext ctx = dv.RenderOpen())
                {
                    VisualBrush vb = new VisualBrush(surface);
                    ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
                }

                renderBitmap.Render(dv);

这解决了我的问题。详情请查看:http://blogs.msdn.com/b/jaimer/archive/2009/07/03/rendertargetbitmap-tips.aspx

此链接由 colinsmith(在问题的评论部分)提供。

【讨论】:

    猜你喜欢
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 2021-03-12
    • 2020-04-10
    相关资源
    最近更新 更多