C#中的照片打印

时间:2011-03-15 15:54:34

标签: c# printing

我有以下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");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

检查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。