自由形式选择C# [英] Free-form selection C#

查看:62
本文介绍了自由形式选择C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我正在研究这个项目,我需要从一个图片框中裁剪一个自由形式选择,然后保存新图像。



我有这个代码将我上传的图片保存到一个文件夹中,如果它不存在就创建它。



Hello I am working on this project which i need to crop from a picturebox a free form selection and then save the new image.

I have this code which saves my uploaded pictures into a folder which if it doesn't exist is creates it.

private void button1_Click(object sender, EventArgs e)
        {

            ++NumberOfClick;
            switch (NumberOfClick)
            {
                case 1:
                    OpenFileDialog f = new OpenFileDialog();
                    f.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";

                    if (f.ShowDialog() == DialogResult.OK)
                    {
                        File = Image.FromFile(f.FileName);
                        pictureBox1.Image = File;
                    }

                    pictureBox1.Image.Save(@"C:\Users\User\Desktop\Gallery\image1.png", ImageFormat.Png);

                    DialogResult result = MessageBox.Show("Do you want to crop this image?", "Confirmation", MessageBoxButtons.YesNo);

                    if(result == DialogResult.Yes)
                    {
                        
                        pictureBox5.Visible = true;
                        button1.Visible = false;
                        pictureBox5.Image = pictureBox1.Image;

                    }

                    

                    break;
                case 2:
                    OpenFileDialog f2 = new OpenFileDialog();
                    f2.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";

                    if (f2.ShowDialog() == DialogResult.OK)
                    {
                        File = Image.FromFile(f2.FileName);
                        pictureBox2.Image = pictureBox1.Image;
                        pictureBox1.Image = File;
                    }

                    pictureBox1.Image.Save(@"C:\Users\User\Desktop\Gallery\image2.png", ImageFormat.Png);

                    break;
                case 3:
                    OpenFileDialog f3 = new OpenFileDialog();
                    f3.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";

                    if (f3.ShowDialog() == DialogResult.OK)
                    {
                        File = Image.FromFile(f3.FileName);
                        pictureBox3.Image = pictureBox2.Image;
                        pictureBox2.Image = pictureBox1.Image;
                        pictureBox1.Image = File;
                    }

                    pictureBox1.Image.Save(@"C:\Users\User\Desktop\Gallery\image3.png", ImageFormat.Png);

                    break;
                case 4:
                    OpenFileDialog f4 = new OpenFileDialog();
                    f4.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";

                    if (f4.ShowDialog() == DialogResult.OK)
                    {
                        File = Image.FromFile(f4.FileName);
                        pictureBox4.Image = pictureBox3.Image;
                        pictureBox3.Image = pictureBox2.Image;
                        pictureBox2.Image = pictureBox1.Image;
                        pictureBox1.Image = File;
                    }

                    pictureBox1.Image.Save(@"C:\Users\User\Desktop\Gallery\image4.png", ImageFormat.Png);

                    break;
                default:

                    OpenFileDialog f5 = new OpenFileDialog();
                    f5.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";

                    if (f5.ShowDialog() == DialogResult.OK)
                    {
                        File = Image.FromFile(f5.FileName);

                        File.Save(@"C:\Users\User\Desktop\Gallery\image" + NumberOfClick.ToString() + ".png", ImageFormat.Png);
                        
                        pictureBox4.Image = pictureBox3.Image;
                        pictureBox3.Image = pictureBox2.Image;
                        pictureBox2.Image = pictureBox1.Image;
                        pictureBox1.Image = File;
                    }

                        break;
            }
        }





我的尝试:



我已经做了一个矩形裁剪但是我想要那个自由形式的选择..



这是我的矩形裁剪:< br $>






What I have tried:

I already did a rectangle crop but I want that free-form selection..

Here is my rectangle crop:


Image img;
	bool mouseClicked;
	Point startPoint = new Point();
	Point endPoint = new Point();

	Rectangle rectCropArea;

	private void Button1_Click(System.Object sender, System.EventArgs e)
	{
	}

	private void OnLoad(System.Object sender, System.EventArgs e)
	{
		loadPrimaryImage();
	}

	private void loadPrimaryImage()
	{
		img = Image.FromFile("..\\..\\images.jpg");
		PictureBox1.Image = img;
	}

	private void PicBox_MouseUp(System.Object sender, System.Windows.Forms.MouseEventArgs e)
	{
		mouseClicked = false;
		if ((endPoint.X != -1)) {
			Point currentPoint = new Point(e.X, e.Y);
			Y1.Text = e.X.ToString();
			Y2.Text = e.Y.ToString();
		}

		endPoint.X = -1;
		endPoint.Y = -1;
		startPoint.X = -1;
		startPoint.Y = -1;
	}

	private void PicBox_MouseDown(System.Object sender, System.Windows.Forms.MouseEventArgs e)
	{
		mouseClicked = true;
		startPoint.X = e.X;
		startPoint.Y = e.Y;
		//Display coordinates
		X1.Text = startPoint.X.ToString();
		Y1.Text = startPoint.Y.ToString();

		endPoint.X = -1;
		endPoint.Y = -1;

		rectCropArea = new Rectangle(new Point(e.X, e.Y), new Size());
	}

	private void PicBox_MouseMove(System.Object sender, System.Windows.Forms.MouseEventArgs e)
	{
		Point ptCurrent = new Point(e.X, e.Y);


		if ((mouseClicked)) {
			if ((endPoint.X != -1)) {
				//Display Coordinates
				X1.Text = startPoint.X.ToString();
				Y1.Text = startPoint.Y.ToString();
				X2.Text = e.X.ToString();
				Y2.Text = e.Y.ToString();
			}

			endPoint = ptCurrent;

			if ((e.X > startPoint.X & e.Y > startPoint.Y)) {
				rectCropArea.Width = e.X - startPoint.X;
				rectCropArea.Height = e.Y - startPoint.Y;


			} else if ((e.X < startPoint.X & e.Y > startPoint.Y)) {
				rectCropArea.Width = startPoint.X - e.X;
				rectCropArea.Height = e.Y - startPoint.Y;
				rectCropArea.X = e.X;
				rectCropArea.Y = startPoint.Y;

			} else if ((e.X > startPoint.X & e.Y < startPoint.Y)) {
				rectCropArea.Width = e.X - startPoint.X;
				rectCropArea.Height = startPoint.Y - e.Y;
				rectCropArea.X = startPoint.X;
				rectCropArea.Y = e.Y;

			} else {
				rectCropArea.Width = startPoint.X - e.X;
				rectCropArea.Height = startPoint.Y - e.Y;
				rectCropArea.X = e.X;
				rectCropArea.Y = e.Y;
			}

			PictureBox1.Refresh();

		}

	}

	private void PicBox_Paint(System.Object sender, System.Windows.Forms.PaintEventArgs e)
	{
		Pen drawLine = new Pen(Color.Red);
		drawLine.DashStyle = DashStyle.Dash;
		e.Graphics.DrawRectangle(drawLine, rectCropArea);
	}

	private void btnCrop_Click(System.Object sender, System.EventArgs e)
	{
		PictureBox2.Refresh();

		Bitmap sourceBitmap = new Bitmap(PictureBox1.Image, PictureBox1.Width, PictureBox1.Height);
		Graphics g = PictureBox2.CreateGraphics();

		if (!(CheckBox1.Checked)) {
			g.DrawImage(sourceBitmap, new Rectangle(0, 0, PictureBox2.Width, PictureBox2.Height), rectCropArea, GraphicsUnit.Pixel);
			sourceBitmap.Dispose();

		} else {
			int x1 = 0;
			int x2 = 0;
			int y1 = 0;
			int y2 = 0;
			try {
				x1 = Convert.ToInt32(CX1.Text);
				x2 = Convert.ToInt32(CX2.Text);
				y1 = Convert.ToInt32(CY1.Text);
				y2 = Convert.ToInt32(CY2.Text);
			} catch (Exception ex) {
				MessageBox.Show("Enter valid Coordinates (only Integer values)");
			}

			if (((x1 < x2 & y1 < y2))) {
				rectCropArea = new Rectangle(x1, y1, x2 - x1, y2 - y1);
			} else if ((x2 < x1 & y2 > y1)) {
				rectCropArea = new Rectangle(x2, y1, x1 - x2, y2 - y1);
			} else if ((x2 > x1 & y2 < y1)) {
				rectCropArea = new Rectangle(x1, y2, x2 - x1, y1 - y2);
			} else {
				rectCropArea = new Rectangle(x2, y2, x1 - x2, y1 - y2);
			}

			PictureBox1.Refresh();
			//This repositions the dashed box to new location as per coordinates entered.

			g.DrawImage(sourceBitmap, new Rectangle(0, 0, PictureBox2.Width, PictureBox2.Height), rectCropArea, GraphicsUnit.Pixel);
			sourceBitmap.Dispose();
		}
	}

	private void pictureBox1_MouseClick(System.Object sender, System.Windows.Forms.MouseEventArgs e)
	{
		PictureBox1.Refresh();
	}

	private void CheckBox1_CheckedChanged(System.Object sender, System.EventArgs e)
	{
		if ((CheckBox1.Checked)) {
			CX1.Visible = true;
			Label10.Visible = true;
			CY1.Visible = true;
			Label9.Visible = true;
			CX2.Visible = true;
			Label8.Visible = true;
			CY2.Visible = true;
			Label7.Visible = true;

			X1.Text = "0";
			X2.Text = "0";
			Y1.Text = "0";
			Y2.Text = "0";

		} else {
			CX1.Visible = false;
			Label10.Visible = false;
			CY1.Visible = false;
			Label9.Visible = false;
			CX2.Visible = false;
			Label8.Visible = false;
			CY2.Visible = false;
			Label7.Visible = false;
		}

	}
	public Form1()
	{
		Load += OnLoad;
	}
}

推荐答案

您无法通过发布问题来学习高级主题,例如图像编辑像这样的论坛。您需要学习教程和视频,或购买书籍。请参见图片编辑c - Google搜索 [ ^ ]。
You cannot learn an advanced subject, such as image editing, by posting questions on a forum like this. You need to study tutorials and videos, or buy a book. See image edit c - Google Search[^].


我可以告诉你的代码泄漏了资源。你没有处理你正在创建的资源,就像你每次刷新时创建的那样。



另外,你在整个地方使用PictureBoxes 。它可能是ToolBox中最无用且最受阻碍的控件,特别是如果它们设置为自动缩放图像。它会在你不知道的情况下做到这一点,因为它不报告它何时发生。



PictureBox适用于quick'n'dirty显示此图像你真的不关心其他事情的情况。对于图像选择和编辑,它实际上会妨碍。



在您的示例中,图像的所有选择代码应放在其自己的类中继承自另一个控件,如Panel。您可以自己以所需的比例绘制源图像,并保持可在选择代码中使用的所有缩放值,以获得更好的计算坐标的结果。
I can tell you that your code leaks resources. You're not disposing the resource you're creating, like that Pen you're creating on every refresh.

Also, you're using PictureBoxes all over the place. it's probably the most useless and hindering controls in the ToolBox, especially if they're setup to automatically scale your image. It'll do that without you knowing it because it doesn't report when it does so.

PictureBox is fine for quick'n'dirty "show this image" situations where you really don't care about much else. For image selection and editing it actually gets in the way.

In your example, all of your "selection" code for an image should be placed in its own class which inherits from another control, like Panel. You get the ability to paint the source image yourself, at the scale you need, and you maintain all of the scaling values you can use in your selection code to get better results for calculating coordinates.


这篇关于自由形式选择C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆