【问题标题】:C# Forms Print Image As Big As Possible [duplicate]C#表单尽可能大地打印图像[重复]
【发布时间】:2019-02-21 15:29:30
【问题描述】:

我想在 Windows 窗体上打印图像,但我想在不破坏其纵横比的情况下在页面上打印尽可能大的图像。就像我想在 A4 纸上打印一个简单的正方形时,它会用这个脚本变成矩形:

private void Page(object sender, PrintPageEventArgs e)
{
    Bitmap i = image;
    if(e.PageBounds.Height > e.PageBounds.Width)
    {
        if (i.Width > i.Height)
            i.RotateFlip(RotateFlipType.Rotate90FlipNone);
    }
    else if(e.PageBounds.Width > e.PageBounds.Height)
    {
        if (i.Height > i.Width)
            i.RotateFlip(RotateFlipType.Rotate90FlipNone);
    }
    e.Graphics.DrawImage(i, new Rectangle(0, 0, e.PageBounds.Width, e.PageBounds.Height));
    i.Dispose();
}

原文: 500x500 square

我得到了什么(pdf格式): A rectangle on the A4 paper

另一个例子:

原文:Original image(313x234)

打印:Print image

我不想破坏图片的纵横比

【问题讨论】:

  • 我认为您是指纵横比,而不是分辨率或“解决方案”?
  • @NetMage 感谢您提供信息!我通过用纵横比替换(重新)解决方案来编辑​​我的帖子。再次感谢您的帮助,让我的帖子更干净
  • 现在如果你在保留纵横比的同时搜索调整大小,你应该会找到this answer

标签: c# winforms printing


【解决方案1】:

试试这个:

private void Page(object sender, PrintPageEventArgs e)
{
    using (Bitmap i = image)  //are you *really sure* you want to dispose this after printing? 
    {
        var pageRatio = e.PageBounds.Width / (double)e.PageBounds.Height;
        var imageRatio = i.Width / (double)i.Height;

        //do we need to rotate?
        if ( (pageRatio < 1 && imageRatio > 1) || (pageRatio < 1 && imageRatio > 1))
        {
            i.RotateFlip(RotateFlipType.Rotate90FlipNone);
            imageRatio = i.Width / (double)i.Height; //ratio will have changed after rotation
        }         

        var scale = 1.0D;
        //do we need to scale?
        if (pageRatio > imageRatio)
        { //the page is wider than the image, so scale to the height
             scale = e.PageBounds.Height / (double)i.Height;
        }
        else if (pageRatio < imageRatio)
        { //the page is narrower than the image, so scale to width
            scale = e.PageBounds.Width / (double)i.Width;
        }

        var W = (int)(scale * i.Width);
        var H = (int)(scale * i.Height);

        e.Graphics.DrawImage(i, new Rectangle(0, 0, W, H));
    }
}

这将始终从左上角绘制。如果你想让它居中,你需要做额外的调整:

private void Page(object sender, PrintPageEventArgs e)
{
    using (Bitmap i = image)  //are you *really sure* you want to dispose this after printing? 
    {
        var pageRatio = e.PageBounds.Width / (double)e.PageBounds.Height;
        var imageRatio = i.Width / (double)i.Height;

        //do we need to rotate?
        if ( (pageRatio < 1 && imageRatio > 1) || (pageRatio < 1 && imageRatio > 1))
        {
            i.RotateFlip(RotateFlipType.Rotate90FlipNone);
            imageRatio = i.Width / (double)i.Height; //ratio will have changed after rotation
        }         

        int T = 0, L = 0; //top, left
        var scale = 1.0D;
        int W = i.Width, H = i.Height;

        //do we need to scale?
        if (pageRatio > imageRatio)
        { //the page is wider than the image, so scale to the height
             scale = e.PageBounds.Height / (double)i.Height;
             W = (int)(scale * i.Width); 
             H = (int)(scale * i.Height);
             L = (e.PageBounds.Width - W)/2;
        }
        else if (pageRatio < imageRatio)
        { //the page is narrower than the image, so scale to width
            scale = e.PageBounds.Width / (double)i.Width;
            W = (int)(scale * i.Width); 
            H = (int)(scale * i.Height);
            T = (e.PageBounds.Height - H)/2;
        }

        e.Graphics.DrawImage(i, new Rectangle(L, T, W+L, H+T));
    }
}

重要的是宽度和高度都调整到相同的比例...即乘以相同的数字。

【讨论】:

  • 首先感谢您的代码,但很抱歉,它没有用。它仍然打破了纵横比。 NetMage的回答帮我解决了
  • 我的第二个样本中有一个错误,现在应该是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-16
  • 2016-07-27
相关资源
最近更新 更多