如何在c#中打印全尺寸图像 [英] How to print a full size image in c#

查看:77
本文介绍了如何在c#中打印全尺寸图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 C# 打印图像.它是由 Adob​​e Acrobat 从 PDF 创建的完整 8.5x11 大小的 tiff.当我使用下面的代码用 C# 打印它时,它在垂直方向打印正确,但不是水平方向,它被推到大约半英寸.我将图像的原点设置为 0,0.我错过了什么吗?

I am trying to print an image in C#. It is a full 8.5x11 size tiff created by Adobe Acrobat from a PDF. When I print it with C# using the code below, it prints correct vertically, but not horizontally, where it is pushed over about a half inch. I set the origin of the image to 0,0. Am I missing something?

private FileInfo _sourceFile;
    public void Print(FileInfo doc, string printer, int tray)
    {
        _sourceFile = doc;
        PrintDocument pd = new PrintDocument();
        pd.PrinterSettings.PrinterName = printer;
        pd.DocumentName = _sourceFile.FullName;
        using (Image img = Image.FromFile(_sourceFile.FullName)) {
            if (img.Width > img.Height) {
                pd.DefaultPageSettings.Landscape = true;
            }
        }
        pd.PrintPage += PrintPage;
        foreach (PaperSource ps in pd.PrinterSettings.PaperSources) {
            if (ps.RawKind == tray) {
                pd.DefaultPageSettings.PaperSource = ps;
            }
        }
        pd.Print();
    }

    private void PrintPage(object o, PrintPageEventArgs e)
    {
        using (System.Drawing.Image img = System.Drawing.Image.FromFile(_sourceFile.FullName)) {
            Point loc = new Point(0, 0);
            e.Graphics.DrawImage(img, loc);
        }
    }

推荐答案

看看下面的代码作为一个很好的例子 这个代码来自下面的这个链接在 C# 中打印图像

look at the code from this below for a good example this code is from this link below Print Image in C#

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

            pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
            pd.OriginAtMargins = false;
            pd.DefaultPageSettings.Landscape = true;

            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);


            printPreviewDialog1.Document = pd;
            printPreviewDialog1.ShowDialog();

            //pd.Print();
        }

        void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            double cmToUnits = 100 / 2.54;
            e.Graphics.DrawImage(bmIm, 100, 1000, (float)(15 * cmToUnits), (float)(10 * cmToUnits));
        }

        private void button5_Click(object sender, EventArgs e)
        {
            PrintImage();
        }

这篇关于如何在c#中打印全尺寸图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆