C# picturebox中的图片根据坐标截取

.net2005 c#
picturebox中的图片上拖动鼠标得到4个坐标x1,y1,x2,y2,想把这个范围内的图像另存出来,代码应该怎么写?谢谢。
还有在鼠标按下后,怎样能显示一个框,来显示拖动过程中所包括的范围,就像QQ截图一样有个框。求详细代码,谢谢。

PS:控件picturebox1 ,通过事件mouse_down,mouse_up获得了左上和右下坐标
谢谢你的回答,但我要的最后效果是在picturebox1中显示截取出的那部分图片,和你的 Form1_MouseClick事件有些出入。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace qiequtupian
{
public partial class mainform : Form
{
bool isDrag = false;
Rectangle theRectangle = new Rectangle(new Point(0, 0), new Size(0, 0));
Point startPoint, oldPoint;
private Graphics ig;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*.jpg,*.jpeg,*.bmp,*.gif,*.ico,*.png,*.tif,*.wmf|*.jpg;*.jpeg;*.bmp;*.gif;*.ico;*.png;*.tif;*.wmf";
openFileDialog1.ShowDialog();
Image myImage = System.Drawing.Image.FromFile(openFileDialog1.FileName);
pictureBox1.Image = myImage;
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//如果开始绘制,则开始记录鼠标位置
if ((isDrag = !isDrag) == true)
{
startPoint = new Point(e.X, e.Y);
oldPoint = new Point(e.X, e.Y);
}
}
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isDrag = false;
ig = pictureBox1.CreateGraphics();
ig.DrawRectangle(new Pen(Color.Black, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
theRectangle = new Rectangle(startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
Graphics g = this.CreateGraphics();
if (isDrag)
{
g.DrawRectangle(new Pen(Color.Black, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
}
}

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
try
{
Graphics graphics = this.CreateGraphics();
Bitmap bitmap = new Bitmap(pictureBox1.Image);
Bitmap cloneBitmap = bitmap.Clone(theRectangle, PixelFormat.DontCare);
graphics.DrawImage(cloneBitmap, e.X, e.Y);
Graphics g = pictureBox1.CreateGraphics();
SolidBrush myBrush = new SolidBrush(Color.White);
g.FillRectangle(myBrush, theRectangle);
}
catch
{ }
}
}
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答