使用RenderTargetBitmap渲染画布会产生模糊的图像

时间:2019-05-04 07:08:53

标签: c# wpf graphics

我正在使用画布制作一个绘制程序并将其自身渲染,但是每次使用RenderTargetBitmap进行渲染时,我的图像都会变得模糊,并且在更大的画布上会发生更多的情况。

过去存在另一个问题,每当我渲染图像时,图像就会向右下角移动,但是由于stackoverflow的帮助,此问题已得到修复。

这是我过去的问题: RenderTargetBitmap Image Sliding

这是我的代码:

XAML

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition Height="50"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="25"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="150"/>
        </Grid.ColumnDefinitions>


        <ScrollViewer Grid.Row="2" Grid.Column="1"
               HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <Canvas x:Name="Pad" Focusable="True" FocusManager.IsFocusScope="True" Width="700" Height="500"
                MouseDown="Pad_MouseDown" MouseMove="Pad_MouseMove" MouseUp="Pad_MouseUp"
                SnapsToDevicePixels="True" Background="White" ClipToBounds="True">
            </Canvas>
        </ScrollViewer>
</Grid>

C#

    public partial class MainWindow : Window
    private bool IsDrawing = false;
    private Polyline polyline;

    public MainWindow()
        {
            InitializeComponent();
        }
    private void Pad_MouseDown(object sender, MouseButtonEventArgs e)
         if (e.ChangedButton == MouseButton.Left)
                {
                    IsDrawing = true;
                    Point p = e.GetPosition((Canvas)sender);
                    polyline = new Polyline();
                    polyline.Stroke = Brushes.Black;;
                    polyline.StrokeThickness = 2;
                    polyline.StrokeStartLineCap = PenLineCap.Round;
                    polyline.StrokeEndLineCap = PenLineCap.Round;
                    polyline.StrokeLineJoin = PenLineJoin.Round;
                    polyline.Points.Add(p);
                    polyline.Points.Add(p);
                    Pad.Children.Add(polyline);

                }
    private void Pad_MouseMove(object sender, MouseEventArgs e)
        {
            Point p = e.GetPosition((Canvas)sender);
            if (IsDrawing)
            {
                polyline.Points.Add(p);
            }
        }
    private void Pad_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left && IsDrawing)
            {
                    IsDrawing = false;
                    RenderCanvas();
            }
        }

   // Render the Canvas and set the rendered image as background of canvas

   public static void RenderCanvas()
        {
        ImageBrush brush = new ImageBrush();
        RenderTargetBitmap rtb;
        Rect bounds;
        DrawingVisual dv;


            bounds = VisualTreeHelper.GetDescendantBounds(Pad);
            rtb = new RenderTargetBitmap((int)(bounds.Width), (int)(bounds.Height), 96, 96, PixelFormats.Pbgra32);
            dv = new DrawingVisual();
            using (DrawingContext ctx = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush();
                vb.Stretch = Stretch.None;
                vb.AlignmentX = AlignmentX.Left;
                vb.AlignmentY = AlignmentY.Top;
                vb.Visual = Pad;
                ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
            }
            rtb.Render(dv);


            Pad.Children.Clear();
            brush.Stretch = Stretch.None;
            brush.AlignmentX = AlignmentX.Left;
            brush.AlignmentY = AlignmentY.Top;
            brush.ImageSource = rtb;
            Pad.Background = brush;
        }

某些分辨率为700、2000、3000、5000的画布的屏幕截图

Canvas with width and height of 700 Canvas with width and height of 2000 Canvas with width and height of 3000 Canvas with width and height of 5000

0 个答案:

没有答案