在Picturebox和突出显示的区域中打印图像

时间:2015-10-15 21:50:32

标签: c# .net image winforms picturebox

我正在使用WinForms。在我的表格中,我有一个包含图像的图片框。我提供了我的代码,通过右键单击并拖动鼠标来突出显示图像。

如何打印图像以及我在图片框中突出显示的内容?

    private Random _rnd = new Random();
    private Point _pt;
    private Point _pt2;

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        //set the upper left point of our selection rectangle
        if (e.Button == MouseButtons.Left)
            _pt = e.Location;
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        //display the selected part when mouse button gets released
        if (e.Button == MouseButtons.Left)
        {
            GenerateBmp();
        }
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        //if we have a valid rectangle, draw it
        if (_pt.X - _pt2.X != 0 && _pt.Y - _pt2.Y != 0)
        {
            //fill
            using (SolidBrush sb = new SolidBrush(Color.FromArgb(95, 255, 255, 0)))
                e.Graphics.FillRectangle(sb, new Rectangle(_pt.X, _pt.Y, _pt2.X - _pt.X, _pt2.Y - _pt.Y));
            //draw
            e.Graphics.DrawRectangle(Pens.Blue, new Rectangle(_pt.X, _pt.Y, _pt2.X - _pt.X, _pt2.Y - _pt.Y));

        }
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        //set the bottom right one
        if (e.Button == MouseButtons.Left)
        {
            _pt2 = e.Location;
            this.pictureBox1.Invalidate();
        }
    }

    private void GenerateBmp()
    {
        //check, if we have a valid rectangle
        if (_pt2.X - _pt.X > 0 && _pt2.Y - _pt.Y > 0)
        {
            //create that rectangle
            Rectangle r = new Rectangle(_pt.X, _pt.Y, _pt2.X - _pt.X, _pt2.Y - _pt.Y);

            //create a new Bitmap with the size of the selection-rectangle
            Bitmap bmp = new Bitmap(r.Width, r.Height);

            //draw the selectex part of the original image
            using (Graphics g = Graphics.FromImage(bmp))
                g.DrawImage(this.pictureBox1.Image, new Rectangle(0, 0, r.Width, r.Height), r, GraphicsUnit.Pixel);
        }
    }

    private Bitmap SetUpPictures(PictureBox pb)
    {
        //create a bitmap to display
        Bitmap bmp1 = new Bitmap(pb.ClientSize.Width, pb.ClientSize.Height);

        //get the graphics-context
        using (Graphics g = Graphics.FromImage(bmp1))
        {
            //get a random, opaque, color
            Color c = Color.FromArgb(255, _rnd.Next(256), _rnd.Next(256), _rnd.Next(256));
            g.Clear(c);

            //better smoothinmode for round shapes
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            //draw ten shapes to the bitmap
            for (int i = 0; i < 10; i++)
            {
                //loaction and size rectangle
                Rectangle r = new Rectangle(_rnd.Next(pb.ClientSize.Width / 2), _rnd.Next(pb.ClientSize.Height / 2),
                    _rnd.Next(pb.ClientSize.Width / 2), _rnd.Next(pb.ClientSize.Height / 2));

                //random color
                Color c2 = Color.FromArgb(_rnd.Next(256), _rnd.Next(256), _rnd.Next(256), _rnd.Next(256));

                //one color brush
                using (SolidBrush sb = new SolidBrush(c2))
                {
                    //check, if i is odd or even and decide on that to draw rectangles or ellipses
                    if ((i & 0x01) == 1)
                        g.FillEllipse(sb, r);
                    else
                        g.FillRectangle(sb, r);
                }
            }
        }

        //return our artwork
        return bmp1;
    }

    private void Btn_Print_Click(object sender, EventArgs e)
    {
        System.Drawing.Printing.PrintDocument myPrintDocument1 = new System.Drawing.Printing.PrintDocument();
        PrintDialog myPrinDialog1 = new PrintDialog();
        myPrintDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(DVPrintDocument_PrintPage);
        myPrinDialog1.Document = myPrintDocument1;

        if (myPrinDialog1.ShowDialog() == DialogResult.OK)
        {
            myPrintDocument1.Print();
        }
    }

    private void DVPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {

        e.Graphics.DrawImage(pictureBox1.Image, 25, 25, 800, 1050);
        var rc = new RectangleF(0, 0, pictureBox1.Image.Width / 100f, pictureBox1.Image.Height / 100f);
        e.Graphics.DrawImage(pictureBox1.Image, rc);
    }

enter image description here

1 个答案:

答案 0 :(得分:2)

您只需使用DrawToBitmap方法绘制图片框的内容:

例如:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawRectangle(Pens.Red, new Rectangle(10, 10, 100, 100));
}

private void button1_Click(object sender, EventArgs e)
{
    this.printDocument1.Print();
}

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    var bmp=new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
    this.pictureBox1.DrawToBitmap(bmp,this.pictureBox1.ClientRectangle);
    e.Graphics.DrawImageUnscaled(bmp, 0, 0);
}

以下是打印结果:

enter image description here