Drawimage(Image,Rectangle)裁剪原始图像

时间:2018-12-22 15:32:13

标签: c# printing graphics drawimage printdocument

我正在尝试从文件中打印图像,但是当我尝试打印图像时,Graphics.DrawImage会裁剪图像。 例: 当我尝试打印Mona Lisa时,输出为cropped

这似乎是相同的问题,但解决方案对我不起作用: DrawImage scales original image

我的代码:

private void print()
    {
        PrintDocument pd = new PrintDocument();

        pd.PrintPage += PrintPage;
        pd.PrinterSettings.PrinterName = "Microsoft Print to PDF";
        pd.Print();
    }

    private void PrintPage(object o, PrintPageEventArgs e)
    {
        Image img = Image.FromFile(@"C:\Users\Leres75\Desktop\MonaLisa.jpg");
        Rectangle rect = new Rectangle(0, 0, img.Width, img.Height);
        img.Save(@"C:\Users\Leres75\Desktop\TestOutput.jpg"); //not Cropped
        e.Graphics.DrawImage(img, rect);
    }

我尝试了DrawImage方法的不同变体,并尝试将屏幕设置弄乱,以更改DPI,但输出不会更改。

如何打印整个图像?

1 个答案:

答案 0 :(得分:-1)

好的,我有办法解决: 我正在调整图像的大小,以使其适合页面。因为我的图像仍然是A4格式,所以这对我有用。 我正在使用它来调整图像的大小:How to resize an Image C#

我的代码现在是:

private void PrintPage(object o, PrintPageEventArgs e)
    {
        Image img = Image.FromFile(@"C:\Users\pavel\OneDrive - OSZ IMT\Desktop\MonaLisa.jpg");
        Image resizedImage = ResizeImage(img, e.PageSettings.PaperSize.Width, e.PageSettings.PaperSize.Height);
        e.Graphics.DrawImage(resizedImage, 0, 0);
    }
相关问题