2

我的项目终于可以正常工作了,但打印时遇到了问题。我只想打印 Splitcontainer.panel1 的内容。每当我这样做时,它都会向我显示屏幕上可见的内容。如果可能,我想获取整个面板并删除滚动条。

这是我的代码:(感谢任何帮助)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LOTO_Tag_Creator
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        PlaceLabels();
        printDoc.PrintPage += new PrintPageEventHandler(PrintDoc_PrintPage);
    }

    //Ensures labels get placed correctly
    private void PlaceLabels()
    {
        Point lblNamePt = new Point(70, 369);
        Point lblDepartmentPt = new Point(70, 393);

        this.labelDepartment1.Parent = this.pictureBox2;
        this.labelDepartment2.Parent = this.pictureBox3;
        this.labelDepartment3.Parent = this.pictureBox4;
        this.labelDepartment5.Parent = this.pictureBox5;

        this.labelName1.Parent = this.pictureBox2;
        this.labelName2.Parent = this.pictureBox3;
        this.labelName3.Parent = this.pictureBox4;
        this.labelName4.Parent = this.pictureBox5;

        this.labelName1.Location = lblNamePt;
        this.labelName2.Location = lblNamePt;
        this.labelName3.Location = lblNamePt;
        this.labelName4.Location = lblNamePt;

        this.labelDepartment1.Location = lblDepartmentPt;
        this.labelDepartment2.Location = lblDepartmentPt;
        this.labelDepartment3.Location = lblDepartmentPt;
        this.labelDepartment5.Location = lblDepartmentPt;
    }

    private void buttonCreate_Click(object sender, EventArgs e)
    {
        InsertName();
        InsertDepartment();
        InsertPicture();
    }

    private void InsertName()
    {
        if (textBoxName.Text.Length > 0)
        {
            this.labelName1.Text = this.textBoxName.Text;
            this.labelName2.Text = this.textBoxName.Text;
            this.labelName3.Text = this.textBoxName.Text;
            this.labelName4.Text = this.textBoxName.Text;
        }
        else
        {
            MessageBox.Show("Please add a name", "No Name", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void InsertDepartment()
    {
        if (this.textBoxDepartment.Text.Length > 0)
        {
            this.labelDepartment1.Text = this.textBoxDepartment.Text;
            this.labelDepartment2.Text = this.textBoxDepartment.Text;
            this.labelDepartment3.Text = this.textBoxDepartment.Text;
            this.labelDepartment5.Text = this.textBoxDepartment.Text;
        }
        else
        {
            MessageBox.Show("Please add a department", "No Department", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void InsertPicture()
    {
        if (this.pictureBox1.Image != null)
        {
            this.pictureBox6.Image = this.pictureBox1.Image;
            this.pictureBox7.Image = this.pictureBox1.Image;
            this.pictureBox8.Image = this.pictureBox1.Image;
            this.pictureBox9.Image = this.pictureBox1.Image;
        }
        else
        {
            MessageBox.Show("Please add a picture", "No Picture", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void buttonSelectImage_Click(object sender, EventArgs e)
    {
        OpenFileDialog OFD = new OpenFileDialog();
        OFD.Filter = "Image Files |*.jpg; *.jpeg; *.png; *.bmp;";
        OFD.InitialDirectory = Path.GetPathRoot(Application.StartupPath);
        OFD.RestoreDirectory = true;

        if (OFD.ShowDialog() == DialogResult.OK)
        {
            using (ImageEditor IE = new ImageEditor(OFD.FileName))
            {
                if (IE.ShowDialog() == DialogResult.OK)
                {
                    this.pictureBox1.Image = IE.useimg;
                    IE.Close();
                }



                // do what ever with result...
            }
            //ImageEditor IE = new ImageEditor(OFD.FileName);
            //IE.ShowDialog();
        }
    }

    PrintDocument printDoc = new PrintDocument();
    PrintPreviewDialog printDialog = new PrintPreviewDialog();
    Panel panel = null;
    Bitmap MemoryImage;

    public void GetPrintArea(Panel pnl)
    {
        MemoryImage = new Bitmap(pnl.Width, pnl.Height);
        Rectangle rect = new Rectangle(0, 0, 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 PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
    {
        Rectangle pageArea = e.PageBounds;
        e.Graphics.DrawImage(MemoryImage, (pageArea.Width / 2) - (panel.Width / 2), panel.Location.Y);
    }

    public void Print(Panel pnl)
    {
        panel = pnl;
        GetPrintArea(pnl);
        printDialog.Document = printDoc;
        printDialog.ShowDialog();
    }

    private void buttonPrint_Click(object sender, EventArgs e)
    {
        Print(this.splitContainer1.Panel1);
    }
}
}
4

0 回答 0