使用PrintDocument打印PDF或图像

时间:2012-03-19 08:06:03

标签: c# asp.net

我编写了使用打印文档打印GIF图像的代码,它工作正常但面临高度和宽度相关的问题。

我的代码:

PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.PrintPage);
pd.Print();

private void PrintPage(object sender, PrintPageEventArgs ev)
      {         
            FileStream fs = File.Open(@"D:\\Test2.gif", FileMode.Open, FileAccess.Read);
            var image = System.Drawing.Image.FromStream(fs);
            try
            {
                ev.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                ev.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                ev.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                ev.Graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                ev.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

                double WidthRatio = ev.MarginBounds.Width / (double)image.Width;
                double HeightRatio = ev.MarginBounds.Height / (double)image.Height;
                if (WidthRatio > HeightRatio)
                {
                    ev.Graphics.DrawImage(image, 0, 0, Convert.ToInt32(HeightRatio * ev.MarginBounds.Width), ev.MarginBounds.Height);
                }
                else
                {
                    ev.Graphics.DrawImage(image, 0, 0, ev.MarginBounds.Width, Convert.ToInt32(WidthRatio * ev.MarginBounds.Height));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                fs.Close();
                image.Dispose();
            }
        }

正确打开图像但是在高度宽度方面遇到问题但未正确显示。 还想用打印文档打印pdf。

请帮忙。

0 个答案:

没有答案