C# 在Picturebox和突出显示区域打印图像
我正在使用WinForms。在我的表格中,我有一个包含图像的图片盒。我提供了通过右键单击并拖动鼠标来突出显示图像的代码 如何打印图像以及在picturebox中突出显示的内容C# 在Picturebox和突出显示区域打印图像,c#,.net,image,winforms,picturebox,C#,.net,Image,Winforms,Picturebox,我正在使用WinForms。在我的表格中,我有一个包含图像的图片盒。我提供了通过右键单击并拖动鼠标来突出显示图像的代码 如何打印图像以及在picturebox中突出显示的内容 private Random _rnd = new Random(); private Point _pt; private Point _pt2; private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
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);
}
private Random\u rnd=new Random();
私人点(pt);
专用点2;
私有void pictureBox1\u MouseDown(对象发送方,MouseEventArgs e)
{
//设置选择矩形的左上角点
if(e.Button==MouseButtons.Left)
_pt=e.位置;
}
私有无效图片box1u MouseUp(对象发送器,MouseEventArgs e)
{
//释放鼠标按钮时显示所选零件
if(e.Button==MouseButtons.Left)
{
GenerateBmp();
}
}
私有void pictureBox1_Paint(对象发送方,PaintEventArgs e)
{
//如果我们有一个有效的矩形,画它
如果(_pt.X-_pt2.X!=0&&_pt.Y-_pt2.Y!=0)
{
//填满
使用(SolidBrush sb=新的SolidBrush(Color.FromArgb(95255,255,0)))
e、 图形填充矩形(sb,新矩形(_pt.X,_pt.Y,_pt2.X-_pt.X,_pt2.Y-_pt.Y));
//画
e、 Graphics.DrawRectangle(Pens.Blue,新矩形(_pt.X,_pt.Y,_pt2.X-_pt.X,_pt2.Y-_pt.Y));
}
}
私有void pictureBox1\u MouseMove(对象发送方,MouseEventArgs e)
{
//把最下面的一个放在右边
if(e.Button==MouseButtons.Left)
{
_pt2=e.位置;
这个.pictureBox1.Invalidate();
}
}
私有void GenerateBmp()
{
//检查,如果我们有一个有效的矩形
如果(_pt2.X-_pt.X>0&&_pt2.Y-_pt.Y>0)
{
//创建那个矩形
矩形r=新矩形(_pt.X,_pt.Y,_pt2.X-_pt.X,_pt2.Y-_pt.Y);
//使用选择矩形的大小创建新位图
位图bmp=新位图(r.宽度,r.高度);
//绘制原始图像的selectex部分
使用(Graphics g=Graphics.FromImage(bmp))
g、 DrawImage(this.pictureBox1.Image,新矩形(0,0,r.宽度,r.高度),r,GraphicsUnit.像素);
}
}
私人位图设置图片(PictureBox pb)
{
//创建要显示的位图
位图bmp1=新位图(pb.ClientSize.Width,pb.ClientSize.Height);
//获取图形上下文
使用(Graphics g=Graphics.FromImage(bmp1))
{
//获得随机的、不透明的颜色
Color c=Color.FromArgb(255,下一个(256),下一个(256),下一个(256));
g、 明确(c);
//圆形形状的更好平滑模式
g、 SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//在位图中绘制十个形状
对于(int i=0;i<10;i++)
{
//移动和大小矩形
矩形r=新矩形(_rnd.Next(pb.ClientSize.Width/2),_rnd.Next(pb.ClientSize.Height/2),
_rnd.Next(pb.ClientSize.Width/2),_rnd.Next(pb.ClientSize.Height/2));
//随机颜色
Color c2=Color.FromArgb(_rnd.Next(256),_rnd.Next(256),_rnd.Next(256),_rnd.Next(256));
//单色刷
使用(SolidBrush sb=新的SolidBrush(c2))
{
//检查,如果我是奇数或偶数,然后决定画矩形或椭圆
如果((i&0x01)==1)
g、 (sb,r);
其他的
g、 (sb,r);
}
}
}
//归还我们的艺术品
返回bmp1;
}
私有无效Btn\u打印\u单击(对象发送者,事件参数e)
{
System.Drawing.Printing.PrintDocument myPrintDocument1=新的System.Drawing.Printing.PrintDocument();
PrintDialog myPrinDialog1=新建PrintDialog();
myPrintDocument1.PrintPage+=新系统.Drawing.Printing.PrintPageEventHandler(DVPrintDocument\u PrintPage);
myPrinDialog1.Document=myPrintDocument1;
if(myPrinDialog1.ShowDialog()==DialogResult.OK)
{
myPrintDocument1.Print();
}
}
私有void DVPrintDocument_PrintPage(对象发送者,System.Drawing.Printing.PrintPageEventArgs e)
{
e、 Graphics.DrawImage(pictureBox1.Image,25,25,800,1050);
var rc=新矩形f(0,0,pictureBox1.Image.Width/100f,pictureBox1.Image.Height/100f);
e、 Graphics.DrawImage(pictureBox1.Image,rc);
}
您只需使用以下方法即可绘制picturebox的内容: 例如:
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);
}
下面是打印的结果:
我有点想(绘制、拖动某些区域)矩形并手动高亮显示这些区域,但它会打印:)这个想法也会奏效,但我认为这样更简单。