关于图像的裁剪问题
由于,我编程方面的知识刚刚接触不到半年,能力有限,不知各位朋友是否可以给予相关的一点提示。谢谢!在图像的裁剪功能上遇到了些问题,我不知道该如何解决了。具体问题为是,当我对图像进行裁剪时:
问题1:出现了无数的矩形框;
问题2:当裁剪完毕时,只出现了以个灰色的矩形面,但是没有出现裁剪图片。
具体代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace caijiantest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private System.Drawing.Point DownPoint = System.Drawing.Point.Empty;//记录鼠标按下坐标,用来确定绘图起点
private bool CatchStart = false;//表示截图开始
private Bitmap originBmp;//用来保存原始图像
private Rectangle CatchRect;//用来保存截图的矩形
private bool CatchFinished = false;//用来表示是否截图完成
private void button2_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == null)
{
MessageBox.Show("请用户打开要处理的图像!");
}
else
{
Clipboard.SetDataObject(pictureBox1.Image);
IDataObject iData = Clipboard.GetDataObject();
DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Bitmap);
if (iData.GetDataPresent(DataFormats.Bitmap))
{
pictureBox2.Image = (Bitmap)iData.GetData(DataFormats.Bitmap);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog myPicture = new OpenFileDialog();
myPicture.Title = "打开文件";
myPicture.Filter = "BMP文件(*.bmp)|*.bmp|所有文件(*.*)|*.*";
if (myPicture.ShowDialog() == DialogResult.OK)
{
Bitmap image = new Bitmap(myPicture.FileName);
pictureBox1.Image = image;
pictureBox1.InitialImage = image;
}
}
//鼠标左键按下时动作
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (!CatchStart)//如果捕捉没有开始
{
CatchStart = true;
DownPoint = new Point(e.X, e.Y);//保存鼠标按下坐标
}
}
}
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
if (CatchStart)//如果捕捉开始
{
Point newPoint = new Point(DownPoint.X, DownPoint.Y);//获取鼠标的坐标
Graphics g = pictureBox2.CreateGraphics();//调用pictureBox2的CreateGraphics方法创建CreateGraphics对象
Pen p = new Pen(Color.Red, 1);
int Wide = Math.Abs(e.X - DownPoint.X), Height = Math.Abs(e.Y - DownPoint.Y);//获取矩形的长和宽
if (e.X < DownPoint.X)
{
newPoint.X = e.X;
}
if (e.Y < DownPoint.Y)
{
newPoint.Y = e.Y;
}
CatchRect = new Rectangle(newPoint, new Size(Wide, Height));//保存矩形
g.DrawRectangle(p, CatchRect);//将矩形画在这个画板上
g.Dispose();//释放目前的这个画板
p.Dispose();
pictureBox2.Image.Dispose();//要及时释放,不然内存将会被大量消耗
}
}
private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (CatchStart)
{
CatchStart = false;
CatchFinished = true;
}
}
}
//鼠标双击事件,如果鼠标位于矩形内,则将矩形内的图片保存到剪贴板中
private void pictureBox2_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && CatchFinished)
{
if (CatchRect.Contains(new Point(e.X, e.Y)))
{
Bitmap CatchedBmp = new Bitmap(CatchRect.Width, CatchRect.Height);//新建一个于矩形等大的空白图片
Graphics g = Graphics.FromImage(CatchedBmp);
g.DrawImage(CatchedBmp, new Rectangle(0, 0, CatchRect.Width, CatchRect.Height), CatchRect, GraphicsUnit.Pixel);
Clipboard.SetImage(CatchedBmp);//将图片保存到剪贴板
g.Dispose();
CatchFinished = false;
pictureBox2.Image = originBmp;
CatchedBmp.Dispose();
IDataObject iData = Clipboard.GetDataObject();
pictureBox2.Image = (Bitmap)iData.GetData(DataFormats.Bitmap);
}
}
}
private void pictureBox2_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
}
}
--------------------编程问答-------------------- http://topic.csdn.net/u/20090420/00/4042e404-e802-45f7-8b25-c7fbc5a81c76.html --------------------编程问答-------------------- 非常感谢!
补充:.NET技术 , C#