首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#表单将图像打印得尽可能大

C#表单将图像打印得尽可能大
EN

Stack Overflow用户
提问于 2018-09-18 00:37:01
回答 1查看 143关注 0票数 0

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

代码语言:javascript
复制
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

我不想破坏图像的纵横比

EN

回答 1

Stack Overflow用户

发布于 2018-09-18 01:00:01

试试这个:

代码语言:javascript
复制
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));
    }
}

这将始终从左上角绘制。如果您希望它居中,则需要进行额外的调整:

代码语言:javascript
复制
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));
    }
}

也就是说,乘以相同的数字。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52372267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档