【问题标题】:Set image position to page center将图像位置设置为页面中心
【发布时间】:2018-04-11 15:40:41
【问题描述】:

我正在尝试在页面中心打印图像,但我想不出任何想法。

System.Windows.Point printLocation = new System.Windows.Point(50,50);
printLocation.X = pageWidth - 50 / 2; 50 is the margin
imageViewer = ImagePrintAdapter.CreateImageFromBitmapImage(img,printLocation);
printerDialog.PrintVisual(imageViewer, "Identification");

这是CreateImageFromBitmapImage方法

public static System.Windows.Controls.Image CreateImageFromBitmapImage(BitmapImage imgSource, System.Windows.Point imgLocation)
 {
   System.Windows.Controls.Image imageViewer = new System.Windows.Controls.Image();
   imageViewer.BeginInit();
   imageViewer.Source = imgSource;

   imageViewer.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
   imageViewer.Arrange(new System.Windows.Rect(imgLocation, imageViewer.DesiredSize));

   imageViewer.EndInit();
   imageViewer.UpdateLayout();

   return imageViewer;
}

如果我将 printLocation.X 设置为 pageWidth 的一半,它不应该从中心开始吗?

【问题讨论】:

    标签: wpf printdialog


    【解决方案1】:

    您可以简单地将图像绘制到 DrawingVisual 中并打印出来。以下简化示例假设位图大小小于可打印区域大小:

    ImageSource image = ...
    
    var rect = new Rect(
        (printDialog.PrintableAreaWidth - image.Width) / 2,
        (printDialog.PrintableAreaHeight - image.Height) / 2,
        image.Width, image.Height);
    
    var visual = new DrawingVisual();
    using (var dc = visual.RenderOpen())
    {
        dc.DrawImage(bitmap, rect);
    }
    
    printDialog.PrintVisual(visual, "");
    

    请注意,您也可以为矩形使用任何其他尺寸,即相应地缩放打印的图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 1970-01-01
      • 2012-06-23
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多