1

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

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

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

    }

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

有任何想法吗?

提前致谢

马可弗罗斯特

4

1 回答 1

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);
}
于 2013-11-14T21:56:52.567 回答