用于打印的实际尺寸WPF控件

Nic*_*ams 2 c# printing wpf dpi zpl

我目前正在开发一个应用程序,用户可以在画布上动态创建/移动TextBlocks.一旦他们将TextBlocks定位在他们想要的位置,他们就可以按下打印按钮,这将导致ZPL打印机打印当前显示在屏幕上的内容.

通过从每个TextBlock获取以下值来构建ZPL命令:

  • XPosition = Canvas.Left
  • YPosition = Canvas.Right
  • 文字=文字

但是我找不到让打印输出类似于屏幕显示的方法.我想这是因为Canvas.Left和Canvas.Right的值与打印机DPI不匹配.

这是我目前正在使用的转换(因为我认为Canvas.Left = 1表示1/96英寸)(画布的左上角是0,0)

public double GetZplXPosition(UIElement uiElement)
{
    int dpiOfPrinter = 300;
    double zplXPosition = (Canvas.GetLeft(uiElement) / 96.0) * dpiOfPrinter;
    return zplXPosition;
}
Run Code Online (Sandbox Code Playgroud)

我可以在"实际尺寸"中显示控件.使用的纸张将始终为A5(8.3英寸x 5.8英寸).

我想在Canvas周围使用一个视图框,其宽度和高度设置为830 x 580(A5的比例正确)但是这没有帮助.

有什么建议??

谢谢

Min*_*gar 6

而不是你正在做什么,采取整个画布的"截图"并打印出来.

using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ImageProcessing
{
    public class ImageProc
    {
        public RenderTargetBitmap GetImage(UIElement source)
        {
            double actualHeight = source.RenderSize.Height;
            double actualWidth = source.RenderSize.Width;

            if (actualHeight > 0 && actualWidth > 0)
            {
                RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)actualWidth, (int)actualHeight, 96, 96, PixelFormats.Pbgra32);
                VisualBrush sourceBrush = new VisualBrush(source);

                DrawingVisual drawingVisual = new DrawingVisual();
                DrawingContext drawingContext = drawingVisual.RenderOpen();
                drawingContext.DrawRectangle(sourceBrush, null, new Rect(0, 0, actualWidth, actualHeight));
                drawingContext.Close();

                renderTarget.Render(drawingVisual);
                return renderTarget;
            }
            else
                return null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)