【问题标题】:C#: Print image from fileC#:从文件中打印图像
【发布时间】:2013-08-07 11:21:39
【问题描述】:

我想打印文件中的图像以完美适合页面。

到目前为止,我设法编写的代码是这样的:

    private void button_print_Click(object sender, EventArgs e)
    {
        if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            printDocument1.PrinterSettings = printDialog1.PrinterSettings;
            printDocument1.PrintPage += PrintPage;
            printDocument1.Print();          
        }
    }

    private void PrintPage(object o, PrintPageEventArgs e)
    {
        System.Drawing.Image img = imgOriginal;
        Point loc = new Point(0, 24);
        e.Graphics.DrawImage(img, loc);

    }

这里的问题是,图像太大而无法完全适合页面。我能做些什么?我在谷歌上找到的所有主题和问题都没有那么有希望。

有什么想法吗?

提前致谢

马可弗罗斯特

【问题讨论】:

  • 所以您想将图像调整为默认打印机中当前所选纸张的大小?你想打破纵横比吗?
  • 是的。我想调整它的大小。但我想保持纵横比。
  • 你可以试试这样的:img.Width = pageSetupDialog.Document.DefaultPageSettings.PaperSize.Width;
  • @EtienneArthur 不幸的是没有。 img.Width/.Height 是只读的。此外,它不会保持纵横比。

标签: c# .net windows image printing


【解决方案1】:
private void PrintPage(object o, PrintPageEventArgs e)
{
  string filepath = "D:\\patient images\\" + txtPatCode.Text + "\\" + lstImages.SelectedItems[0].Text;
  System.Drawing.Image img = Image.FromFile(filepath);
  ResizeImage(img, 200);
  Point loc = new Point(200, 200);
  e.Graphics.DrawImage(img, loc);           
}

public static Image ResizeImage(Image img, int minsize)
{
  var size = img.Size;
  if (size.Width >= size.Height)
  {
    // Could be: if (size.Height < minsize) size.Height = minsize;
    size.Height = minsize;
    size.Width = (size.Height * img.Width + img.Height - 1) / img.Height;
  }
  else
  {
    size.Width = minsize;
    size.Height = (size.Width * img.Height + img.Width - 1) / img.Width;
  }
  return new Bitmap(img, size);
}

【讨论】:

  • 您应该将此答案归因于Resizing images in C#,因为您的 ResizeImage 函数似乎是从中复制而来的。
猜你喜欢
  • 2021-04-23
  • 1970-01-01
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
相关资源
最近更新 更多