c# - 打印图片框

标签 c# winforms

当我尝试打印 PictureBox 时,抛出 ArgumentException(参数无效)。

这是打印函数:

void pdGroupBox_PrintPage(object sender, PrintPageEventArgs e)
{
    foreach(Control control in _GbFrm.Controls)
        DrawControls(control, e.Graphics);
}

private void DrawControls(Control control,Graphics g)
{
    var font = new Font("Arial", 10);
    var brush = new SolidBrush(Color.Black);
    var drawFormat = new StringFormat
    {
        FormatFlags = StringFormatFlags.DirectionRightToLeft
    };

    if (control is Label)
    {
        if ((string)control.Tag == "1") //Treated with 1 columns of fields.
        {
            g.DrawString(
                control.Text, font, brush,
                new Rectangle(control.Location.X - 160, control.Location.Y, control.Size.Width + 10, control.Size.Height),
                drawFormat);
        }
        if ((string)control.Tag == "2") //Treated with 2 columns of fields.
        {
            g.DrawString(
                control.Text, font, brush,
                new Rectangle(control.Location.X - 70, control.Location.Y, control.Size.Width + 10, control.Size.Height),
                drawFormat);
        }
        if ((string)control.Tag == "3") //Treated with 3 columns of fields.
        {
            g.DrawString(
                control.Text, font, brush,
                new Rectangle(control.Location.X, control.Location.Y, control.Size.Width + 10, control.Size.Height));
        }
    }
    else if (control is TextBox || control is ComboBox)
    {
        if ((string)control.Tag == "1") //Treated with 1 columns of fields.
        {
            g.DrawRectangle(
                new Pen(Color.Black, 1),
                new Rectangle(control.Location.X - 120, control.Location.Y, control.Size.Width - 40, control.Size.Height));
            g.DrawString(
                control.Text, font, brush,
                new Rectangle(control.Location.X - 120, control.Location.Y, control.Size.Width - 40, control.Size.Height), drawFormat);
        }
        if ((string)control.Tag == "2") //Treated with 2 columns of fields.
        {
            g.DrawRectangle(
                new Pen(Color.Black, 1),
                new Rectangle(control.Location.X - 30, control.Location.Y, control.Size.Width - 40, control.Size.Height));
            g.DrawString(
                control.Text, font, brush,
                new Rectangle(control.Location.X - 30, control.Location.Y, control.Size.Width - 40, control.Size.Height),
                drawFormat);
        }
        if ((string)control.Tag == "3") //Treated with 3 columns of fields.
        {
            g.DrawRectangle(
                new Pen(Color.Black, 1),
                new Rectangle(control.Location.X, control.Location.Y, control.Size.Width, control.Size.Height));
            g.DrawString(
                control.Text, font, brush,
                new Rectangle(control.Location.X, control.Location.Y, control.Size.Width, control.Size.Height), drawFormat);
        }
    }
    else if (control is PictureBox && control.Visible)
    {
        var img = ((PictureBox)control).Image;
        var bitmap = new Bitmap(img,new Size(img.Width,img.Height));
        g.DrawImage(
            bitmap,
            new Rectangle(control.Location.X, control.Location.Y, control.Size.Width, control.Size.Height));
    }
    else if (control is DateTimePicker)
    {
        g.DrawString(
            control.Text, font, brush,
            new Rectangle(control.Location.X, control.Location.Y, control.Size.Width + 10, control.Size.Height));
    }
}

所有的打印问题都解决了,但有时打印 PictureBox 会引发 Exception,那么我该如何解决这个问题?

最佳答案

试试这个:有点简单:

在按钮点击事件中添加如下代码:

PrintDocument printDocument1 = new PrintDocument();
printDocument1.PrintPage +=new PrintPageEventHandler(printDocument1_PrintPage);
printDocument1.Print();

然后是 printDocument1_PrintPage 事件:

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    e.Graphics.DrawImage(pic.Image, 0, 0);
}

其中pic是图片框..

这将打印图片框中的图像。

关于c# - 打印图片框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14039752/

相关文章:

c# - 当键包含美元($)(元数据)时将 JSON 反序列化为 c# 对象

c# - 按下一个键然后执行另一个键

c# - 表结构更改后更新 xsd

c# - TextBox 可以显示的最大字符数

c# - 将 CMD 输出复制到剪贴板

c# - List< > Dictionary< K,List< >> 内部 (C#)

c# - 如何避免在调试时触发属性?

c# - 如何在从组合框中选择任何值时连接到数据库

c# - 如何在 C# 中用它打开特定文件?

c# - MEF 加载和卸载特定插件?