简体   繁体   English

C#无法打印正确的图像尺寸

[英]C# Can't print the right image size

I'm trying to make a program that will print an image file to the printer onto an A4 sized paper. 我正在尝试制作一个程序,将打印机上的图像文件打印到A4大小的纸张上。 (The image is scaled to A4) This is currently the code I'm using. (图像缩放为A4)这是我正在使用的代码。

Function.cs        

    Image tmp;
    public void PrintImage(string FileName)
    {
        tmp = Image.FromFile(FileName);
        PrintDocument printDoc = new PrintDocument();
        printDoc.DocumentName = "My Print Document";
        printDoc.PrintPage += new PrintPageEventHandler(OnPrintPage);
        // Send print message
        Console.WriteLine("Sending Print Message...");
        try
        {
            printDoc.Print();
        }
        catch(Exception)
        {
            Console.WriteLine("Error: No Printer Installed");
        }
        //
        // Event Handler
        //
    }
    private void OnPrintPage(object sender, PrintPageEventArgs ppea)
    {
    Console.WriteLine("Printing...");
    Graphics g = ppea.Graphics;
    g.DrawImage(tmp,0,0);
    }



Main.cs

    string ReceiptFilePath = "C:\\Receipt.jpg"; // The Image I want to print.

    private void button1_Click(object sender, EventArgs e)
    {
        functions.PrintImage(ReceiptFilePath);
    }

With the above code, it can be printed, however it is not in A4 size, rather it zoomed in almost 400%. 使用上面的代码,它可以打印,但它不是A4尺寸,而是放大了近400%。 How should I modify the code? 我该如何修改代码?

The image is scaled to A4 图像缩放为A4

That's the problem statement. 这是问题陈述。 PrintDocument already scales. PrintDocument已经扩展。 It initializes the Graphics instance you get from e.Graphics in the PrintPage event handler to PageUnit = GraphicsUnit.Display. 它将您从PrintPage事件处理程序中的e.Graphics获取的Graphics实例初始化为PageUnit = GraphicsUnit.Display。 Which is a precooked scaling mode that makes one pixel 0.01 inches on paper. 这是一种预煮的缩放模式,可在纸上制作一个0.01英寸的像素。 Or in other words, an image that's 100 x 100 pixels becomes 1.00 x 1.00 inch on paper. 换句话说,100 x 100像素的图像在纸上变为1.00 x 1.00英寸。

This is a convenient scaling mode, chosen to make anything you draw to the screen about the same size on paper. 这是一种方便的缩放模式,选择使您在屏幕上绘制的任何内容大小相同。 Video adapters tend to operate at 96 dots-per-inch resolution. 视频适配器往往以每英寸96点的分辨率运行。 Although that's changing, later Windows versions make it really easy to increase this. 虽然这种情况正在发生变化,但后来的Windows版本让它变得非常容易。

So by prescaling the image, you'll end up making the image large. 因此,通过预先对图像进行预定,您最终会使图像变大。 Which then makes it too large on paper by the additional scaling done by the printer's Graphics object. 然后通过打印机的Graphics对象进行的额外缩放使其在纸上太大。

Giving better advice isn't that easy since you didn't describe how you prescaled the image. 提供更好的建议并不容易,因为您没有描述如何对图像进行预分类。 Simply not prescaling probably already solves your problem. 简单地说,预定标可能已经解决了你的问题。 Using the Graphics.DrawImage(Image, Rectangle) overload can be a better bet, it gives you direct control over the size of the image on paper. 使用Graphics.DrawImage(Image,Rectangle)重载可能是一个更好的选择,它可以让您直接控制纸张上的图像大小。 Also giving you an opportunity to deal with different paper sizes and still making the image fit. 同时让您有机会处理不同的纸张尺寸并使图像更合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM