快照产生的低质量图片。 WPF

问题描述 投票:0回答:1

下面的程序创建一个快照,其中包含应用程序本身主窗口的内容。然而,所产生的图像的质量不等于窗口10的打印屏幕程序,这产生了期望的结果。

这是运行程序的快照,使用Windows 10的打印屏幕程序进行放大,放大:

https://ibb.co/wz4pb4d

这是以下程序生成的快照,放大:

https://ibb.co/DLsNb8X

有什么我们可以尝试提高该程序产生的快照的质量?

我尝试了Bitmap Encoder,但结果相同,只是没有透明度,(不需要透明度)也试过其他Pixel格式但我得到错误,只有Pbgra32似乎可以像程序一样工作。

        if (e.Key == Key.P)
        {
            //Set scrollviewer's Content property as UI element to capture full content
            UIElement element = mainwindow.Content as UIElement;
            Uri path = new Uri(@"C:\Users\4gry\Desktop\screenshot.png");
            CaptureScreen(element, path);
        }

    }
        public void CaptureScreen(UIElement source, Uri destination)
        {
            try
            {
                Double Height, renderHeight, Width, renderWidth;

                Height = renderHeight = source.RenderSize.Height;
                Width = renderWidth = source.RenderSize.Width;

                //Specification for target bitmap like width/height pixel etc.
                RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96, PixelFormats.Pbgra32);
                //creates Visual Brush of UIElement
                VisualBrush visualBrush = new VisualBrush(source);

                DrawingVisual drawingVisual = new DrawingVisual();
                using (DrawingContext drawingContext = drawingVisual.RenderOpen())
                {
                    //draws image of element
                    drawingContext.DrawRectangle(visualBrush, null, new Rect(new Point(0, 0), new Point(Width, Height)));
                }
                //renders image
                renderTarget.Render(drawingVisual);

                //PNG encoder for creating PNG file
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(renderTarget));
                using (FileStream stream = new FileStream(destination.LocalPath, FileMode.Create, FileAccess.Write))
                {
                    encoder.Save(stream);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }



}

}

c# wpf
1个回答
0
投票

不确定这是否正是您想要实现的,但您可以通过将源元素的RenderOptions.EdgeMode属性设置为EdgeMode.Aliased来避免消除锯齿效果。

另请注意,您还可以更简单地编写CaptureScreen方法:

public void CaptureScreen(UIElement source, string destination)
{
    RenderOptions.SetEdgeMode(source, EdgeMode.Aliased); // here

    var drawingVisual = new DrawingVisual();

    using (DrawingContext drawingContext = drawingVisual.RenderOpen())
    {
        drawingContext.DrawRectangle(
            new VisualBrush(source),
            null,
            new Rect(source.RenderSize));
    }

    var bitmap = new RenderTargetBitmap(
        (int)Math.Round(source.RenderSize.Width),
        (int)Math.Round(source.RenderSize.Height),
        96, 96, PixelFormats.Default);

    bitmap.Render(drawingVisual);

    var encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(bitmap));

    using (var stream = new FileStream(destination, FileMode.Create))
    {
        encoder.Save(stream);
    }
}

要创建具有更高分辨率的图像,请使用DPI参数而不是默认值96,如下所示:

public void CaptureScreen(UIElement source, double dpi, string destination)
{
    RenderOptions.SetEdgeMode(source, EdgeMode.Aliased);

    var drawingVisual = new DrawingVisual();

    using (DrawingContext drawingContext = drawingVisual.RenderOpen())
    {
        drawingContext.DrawRectangle(
            new VisualBrush(source),
            null,
            new Rect(source.RenderSize));
    }

    var bitmap = new RenderTargetBitmap(
        (int)Math.Round(source.RenderSize.Width * dpi / 96),
        (int)Math.Round(source.RenderSize.Height * dpi / 96),
        dpi, dpi, PixelFormats.Default);

    bitmap.Render(drawingVisual);

    var encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(bitmap));

    using (var stream = new FileStream(destination, FileMode.Create))
    {
        encoder.Save(stream);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.