C#:从文件打印图像

时间:2013-08-07 11:21:39

标签: c# .net windows image printing

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

直到现在,我设法编码所有这一切:

    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

1 个答案:

答案 0 :(得分: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);
}