如何导出具有适当尺寸的wpf InkCanvas?

时间:2018-09-30 23:28:21

标签: wpf scaling inkcanvas

我正在尝试创建一个simple sketching application

但是我遇到了一个奇怪的问题。根据{{​​3}},我正在使用Surface Pro 3,其DPI为144。

some system settings

当我使用96作为dpi从我的应用程序中保存图像时,它会产生稍小的图像。哪个很奇怪。

有没有一种方法可以a)缩放要保存的画布/笔画,或告诉RenderTargetBitmap正确缩放?如果我仅在其中粘贴144,就可以得到适当的笔划比例,但是我的画布尺寸太小了。

我的画布保存代码如下:

public void saveCanvas(object sender, RoutedEventArgs e){
    this.save_filename = this.save_filename ?? this.getSaveFilename();

    if (save_filename != null){
        var cantwo = new InkCanvas();
        cantwo.Strokes.Clear();
        cantwo.Strokes.Add(this.canvas.Strokes);
        cantwo.Background = this.canvas.Background;
        var size = new Size(this.canvas.ActualWidth, this.canvas.ActualHeight);
        cantwo.Height = size.Height;
        cantwo.Width = size.Width;
        cantwo.Measure(size);
        cantwo.Arrange(new Rect(size));
        var transform = this.canvas.LayoutTransform;
        var rtb = new RenderTargetBitmap((int)this.canvas.ActualWidth, (int)this.canvas.ActualHeight, dpiX, dpiY, PixelFormats.Default);
        rtb.Render(cantwo);
        try {
            using(var fs = File.Open(this.save_filename, FileMode.Create)){
                var encoder = new JpegBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(rtb));
                encoder.Save(fs);
            }
        }
        catch(IOException){
            MessageBox.Show("Failed to save image", "ERROR: Save Failed");
        }
        // Restore transformation if there was one.
        this.canvas.LayoutTransform = transform;
    }
}

如何以与画布相同的尺寸/分辨率/ dpi保存图像? (或者以与保存图像相同的dpi /比例在画布上绘画)?

1 个答案:

答案 0 :(得分:0)

不是创建第二个InkCanvas,而是将用VisualBrush填充的Rectangle绘制到DrawingVisual中:

var rect = new Rect(canvas.RenderSize);
var visual = new DrawingVisual();

using (var dc = visual.RenderOpen())
{
    dc.DrawRectangle(new VisualBrush(canvas), null, rect);
}

var rtb = new RenderTargetBitmap(
    (int)rect.Width, (int)rect.Height, 96, 96, PixelFormats.Default);
rtb.Render(visual);