【问题标题】:Photo printing in C#C#中的照片打印
【发布时间】:2011-03-16 00:14:53
【问题描述】:

我有以下C# 代码可以在桌面应用程序中打印8x6 照片。

它适用于使用普通信纸尺寸纸张的普通打印机。
但是我的客户正在使用带有8x6 纸张的柯达打印机,照片正在打印,但它们的尺寸不同,它们没有完全打印8x6 尺寸,我做错了。

有人可以指导我正确的方向吗?

public void Print(List ListToBePrinted)
{
    PrintDialog SelectedPrinter = new PrintDialog();
    if (SelectedPrinter.ShowDialog() == true)
    {
        PrintCapabilities printerCapabilities = SelectedPrinter.PrintQueue.GetPrintCapabilities();
        Size PageSize = new Size(printerCapabilities.PageImageableArea.ExtentWidth, printerCapabilities.PageImageableArea.ExtentHeight);
        Size PrintableImageSize = new Size();
        foreach (Uri aUri in ListToBePrinted)
        {
            DrawingVisual drawVisual = new DrawingVisual();
            ImageBrush imageBrush = new ImageBrush();
            imageBrush.ImageSource = new BitmapImage(aUri);
            imageBrush.Stretch = Stretch.Fill;
            imageBrush.TileMode = TileMode.None;
            imageBrush.AlignmentX = AlignmentX.Center;
            imageBrush.AlignmentY = AlignmentY.Center;
            if (imageBrush.ImageSource.Width > imageBrush.ImageSource.Height)
                PrintableImageSize = new Size(768, 576); //8x6
            else PrintableImageSize = new Size(576, 768); //6x8 
            double xcor = 0; double ycor = 0;
            if (imageBrush.ImageSource.Width > imageBrush.ImageSource.Height)
            {
                if ((PageSize.Width - PrintableImageSize.Height) > 0)
                    xcor = (PageSize.Width - PrintableImageSize.Height) / 2;
                if ((PageSize.Height - PrintableImageSize.Width) > 0)
                    ycor = (PageSize.Height - PrintableImageSize.Width) / 2;
            }
            else
            {
                if ((PageSize.Width - PrintableImageSize.Width) > 0)
                    xcor = (PageSize.Width - PrintableImageSize.Width) / 2;
                if ((PageSize.Height - PrintableImageSize.Height) > 0)
                    ycor = (PageSize.Height - PrintableImageSize.Height) / 2;
            }
            using (DrawingContext drawingContext = drawVisual.RenderOpen())
            {
                if (imageBrush.ImageSource.Width > imageBrush.ImageSource.Height)
                {
                    drawingContext.PushTransform(new RotateTransform(90, PrintableImageSize.Width / 2, PrintableImageSize.Height / 2));
                }
                drawingContext.DrawRectangle(imageBrush, null, new Rect(xcor, ycor, PrintableImageSize.Width, PrintableImageSize.Height));
            }
            SelectedPrinter.PrintVisual(drawVisual, "Print");
        }
    }
}

【问题讨论】:

  • @digEmAll:编辑速度很快!
  • @Nick:上帝保佑 Visual Studio 自动格式化功能 ;-)

标签: c# printing


【解决方案1】:

检查 HardMarginX 和 HardMarginY 值,如果可以的话。

我的一个应用中有此代码(其中 e 是 PrintPageEventArgs)

    e.Graphics.DrawImage(nextImage, e.PageSettings.PrintableArea.X - e.PageSettings.HardMarginX, e.PageSettings.PrintableArea.Y - e.PageSettings.HardMarginY, e.PageSettings.Landscape ? e.PageSettings.PrintableArea.Height : e.PageSettings.PrintableArea.Width, e.PageSettings.Landscape ? e.PageSettings.PrintableArea.Width : e.PageSettings.PrintableArea.Height);

我的打印功能有点原始,但也许你可以根据硬边距调整你的 xcor 和 ycor。

【讨论】:

  • 如果我想打印整页,我可以为 xcor 和 ycor 使用 0,0 吗?我试过了,但它似乎不起作用
  • 我为 xcor 和 ycor 尝试了 0,0,并且所有边距都为 0。即使那样,照片周围的边距也很小。如何获得无边距打印?
猜你喜欢
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 2022-10-04
  • 1970-01-01
  • 2015-10-30
相关资源
最近更新 更多