C#:从文件打印图像

本文关键字:打印 图像 文件 | 更新日期: 2023-09-27 18:26:46

我想从文件中打印一个图像,使其完全适合页面。

到目前为止,我所能编码的就是:

    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);
    }

这里的问题是,图像太大了,无法完全适应页面。我能做什么?我在谷歌上发现的所有线索和问题都没有那么有希望。

有什么想法吗?

提前感谢

Marco Frost

C#:从文件打印图像

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);
}