WPF实际尺寸打印控件 [英] Real size WPF controls for printing
问题描述
我目前正在开发一个应用程序,用户可以在其中动态创建/移动画布上的TextBlocks。一旦他们将TextBlocks放置在所需的位置,就可以按下打印按钮,这将使ZPL打印机打印屏幕上当前显示的内容。
I am currently developing an application where users can create/move TextBlocks on a canvas dynamically. Once they have positioned the TextBlocks where they want them they can press a print button which will cause a ZPL printer to print what is currently displayed on screen.
ZPL命令是通过从每个TextBlock中获取以下值来构建:
The ZPL commands are built by taking the following values from each TextBlock:
- XPosition = Canvas.Left
- YPosition = Canvas.Right
- 文本=文本
但是我找不到一种获取方法打印输出类似于屏幕显示。我猜这是因为Canvas.Left和Canvas.Right的值与打印机DPI不匹配。
However I can't find a way of getting the printout to resemble the on screen display. I guess this is because the values of Canvas.Left and Canvas.Right do not match up with the printers DPI.
这是我当前正在使用的转换(因为我认为Canvas.Left = 1表示1/96英寸)(画布的左上角是0, 0)
Here is the conversion I am currently using (because I thought the Canvas.Left = 1 means 1/96th of an inch) (Top left of the canvas is 0,0)
public double GetZplXPosition(UIElement uiElement)
{
int dpiOfPrinter = 300;
double zplXPosition = (Canvas.GetLeft(uiElement) / 96.0) * dpiOfPrinter;
return zplXPosition;
}
我可以在那里显示实际大小的控件。使用的纸张将始终为A5(8.3英寸x 5.8英寸)。
Is there I can display controls in "real size". The paper being used will always be A5 (8.3 in x 5.8 in).
我考虑过在画布的宽度和高度设置为830 x的情况下使用视图框580(比对A5的比率正确),但这没有帮助。
I thought about using a viewbox around a Canvas which had its width and height set to 830 x 580 (ratio correct for A5) however this didn't help.
任何建议?
谢谢
推荐答案
而不是您在做什么,而是对整个画布进行截屏并进行打印。
Instead of what you're doing, take a "screenshot" of the entire canvas and print that.
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;
}
}
}
这篇关于WPF实际尺寸打印控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!