0

C# では、winforms を使用した .NET 3.5 で、印刷プレビュー コントロールに取り組んでいます。うまくいきそうですが、スキャンしたA4ページをA3ページに等倍で印刷すると、縮尺が約3%ずれます...

次のコードを使用して、ユーザーが選択した設定に基づいてページの実際の印刷可能領域を計算していますが、それらの計算が間違っているのではないかと思いますか?

public static RectangleF GetPrintArea(PageSettings PageSettings)
    {
        float[] margins;
        RectangleF printArea;

        // Get the actual page bounds
        printArea = PageSettings.Bounds;

        // Calculate the hard margins taking into account page orientation
        margins = new float[4];
        // Left
        margins[0] = !PageSettings.Landscape ? PageSettings.HardMarginX : PageSettings.HardMarginY;
        // Top
        margins[1] = !PageSettings.Landscape ? PageSettings.HardMarginY : PageSettings.HardMarginX;
        // Right
        margins[2] = margins[0];
        // Bottom
        margins[3] = margins[1];

        // Calculate the real print margins taking into account teh hard and soft margins
        // left
        margins[0] = Math.Max(margins[0], PageSettings.Margins.Left);
        // Top
        margins[1] = Math.Max(margins[1], PageSettings.Margins.Top);
        // Right
        margins[2] = Math.Max(margins[2], PageSettings.Margins.Right);
        // Bottom
        margins[3] = Math.Max(margins[3], PageSettings.Margins.Bottom);

        return new RectangleF(
            new PointF(margins[0], margins[1]),
            new SizeF(printArea.Width - (margins[0] + margins[2]), printArea.Height - (margins[1] + margins[3]))
         );
    }

これは、印刷時のページの実際の領域を示す長方形を返す必要があります。この四角形を使用して、プレビューと印刷物の両方を生成します。

印刷用のコードは次のとおりです。

    /// <summary>
    /// Draws the image on the printing surface
    /// </summary>
    /// <param name="Graphics">The graohics object with which to draw</param>
    protected virtual void PrintImage(Graphics Graphics)
    {
        RectangleF imageBoundingBox;
        RectangleF visibleImageBoundingBox;
        RectangleF visibleImage;

        // Offset the visible bounding box location by the position of the print area so as to print right within the margins
        Graphics.TranslateTransform(-this.Page.PrintableArea.Left, -this.Page.PrintableArea.Top);

        // Calculate the bounding box of the scaled image
        imageBoundingBox = new RectangleF(this.Page.PrintAreaOrigin.Add(this.ImagePrintLocation), this.Image.Size.Multiply(this.ImagePrintScale));

        // Calculate the position and size of the portion of the image bounding box visible in the viewport
        visibleImageBoundingBox = RectangleF.Intersect(imageBoundingBox, this.Page.PrintArea);

        // Calculate the portion of the image which corresponds to the visible bounding box
        visibleImage = new RectangleF(
            new PointF(
                imageBoundingBox.X < this.Page.PrintArea.Left ? Math.Min(this.Page.PrintArea.Left - imageBoundingBox.X, imageBoundingBox.Width) : 0,
                imageBoundingBox.Y < this.Page.PrintArea.Top ? Math.Min(this.Page.PrintArea.Top - imageBoundingBox.Y, imageBoundingBox.Height) : 0
            ).Divide(this.ImagePrintScale),
            visibleImageBoundingBox.Size.Divide(this.ImagePrintScale)
        );

        // Draw the image
        Graphics.DrawImage(this.Image, visibleImageBoundingBox, visibleImage, GraphicsUnit.Pixel);
    }

は、ページ境界 ( )、印刷可能領域、および実際の印刷領域 ( )Pageを含むクラスです。実際の印刷領域は、前の長方形で指定された領域です。PageAreaPrintableAreaPrintArea

このアプローチには何か問題があるに違いありませんが、私の人生ではそれが何であるかを理解することはできません. 誰かが何が間違っているのかを特定できれば、私は非常に感謝しています...

4

0 に答える 0