c# - Windows 窗体 C# 中的打印面板

标签 c# winforms visual-studio-2010

<分区>

我这里有个情况。我需要在 Visual Studio 2010 中使用 C# 在 Windows 窗体中创建一个员工卡结构。该结构可能包含带有白色背景的标签和图片框。我创建它没有问题,但我还在这个表单上提供了一个名为“打印”的按钮,以便用户可以打印该卡。我用谷歌搜索但没有找到具体的东西。 请帮帮我。

namespace Employee_Card_Manager
{
public partial class Card : Form
{
    public Card(String a, String b, String c, String d, String e, String f, String g, String h, String i)
    {
        InitializeComponent();
        this.label2.Text = a;
        this.label9.Text = b;
        this.label10.Text = c;
        this.label11.Text = d;
        this.label12.Text = e;
        this.label13.Text = f;
        this.label14.Text = g;
        this.label16.Text = h;
        this.pictureBox1.Image = Image.FromFile(i);
        Image myimg = Code128Rendering.MakeBarcodeImage(h, 2, true);
        this.pictureBox2.Image = myimg;
    }
    private void button1_Click(object sender, EventArgs e)
    {
          //Print Card Code
    }
  }
}

卡片模板如下:

Employee Card Structure

我已将所有卡片结构放在面板控件上并将面板背景设置为白色。你能填写打印这张卡片的代码吗? 谢谢

最佳答案

我发现以下代码运行良好!!

    //declare event handler for printing in constructor
        printdoc1.PrintPage += new PrintPageEventHandler(printdoc1_PrintPage);

    //Rest of the code
    Bitmap MemoryImage;
    public void GetPrintArea(Panel pnl)
    {
        MemoryImage = new Bitmap(pnl.Width, pnl.Height);
        pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height));
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        if (MemoryImage != null)
        {
            e.Graphics.DrawImage(MemoryImage, 0, 0);
            base.OnPaint(e);
        }
    }
    void printdoc1_PrintPage(object sender, PrintPageEventArgs e)
    {
        Rectangle pagearea = e.PageBounds;
        e.Graphics.DrawImage(MemoryImage, (pagearea.Width / 2) - (this.panel1.Width / 2), this.panel1.Location.Y);
    }
    public void Print(Panel pnl)
    {
        pannel = pnl;
        GetPrintArea(pnl);
        previewdlg.Document = printdoc1;
        previewdlg.ShowDialog();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Print(this.panel1);
    }

关于c# - Windows 窗体 C# 中的打印面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10605840/

相关文章:

c# - 简单的加载形式/线程

c# - 使用 Windows 窗体控制操作系统鼠标事件 - C#

c++ - 如何通过友元函数调用私有(private)函数?

c# - 在 DSL 中的 DragOver 上更新隔间形状轮廓颜色

c# - 如何强调工具提示中显示的 xml-doc 中的单词?

c# - 从 C++ 迁移到 C# 的惊喜

c# - 在 MVVM 中使用 Rx 跟踪集合更改

c# - 将文本添加到 RichTextBox Async#C/WPF

c# - 如何重新排序 DataGridView 中的列?

c# - 在 C# 中测试类库