在图像上添加和删除圆/矩形 [英] add and delete circle/rectangle on an image

查看:55
本文介绍了在图像上添加和删除圆/矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要通过单击鼠标在图像上添加圆形或矩形.稍后,当我按Delete按钮并单击任何圆形或矩形时,仅应删除该特定的圆形/矩形.
我正在使用pictureBox和Windows Form.

Hi,

I need to add either circle or rectangle on an image with a mouse click. later when i press delete button and click on any circle or rectangle, only that particular circle/rectangle should be deleted.
Am using pictureBox and windows Form.
Kindly help.

推荐答案

在绘制函数的作用域之后,所有这些图形对象的圆矩形都将被销毁.现在,它只是位图像素的一部分.它不会为您提供点击事件.

但是WPF这些图形对象仍然存在并且具有click事件.但是,在Windows窗体上并不是不可能的.您可以使用简单的控制面板.在面板上绘制图形,使背景透明.将面板放在图像上.现在,它可以响应单击,单击时您可以自行销毁面板.一个有用的例子.

图片框中的可点击区域 [
The circle rectangle all those graphics objects will be destroyed after the scope of the function you are drawing it. Now it is only the part of the bitmap pixels. It won''t offer you a click event.

But WPF these graphics objects live and have a click event. However it is not impossible on a windows forms. You can use the simple control panel. Draw your graphics on a panel, make the background transparent. Place the panel on the image. Now it responds to a click and when click you can self destroy the panel. A helpful example here.

Clickable areas in a picturebox[^]

This use the graphic transparent, but you can draw a visible graphic on the panel.

Good luck


这是我的代码...我在删除按钮(button2)上尝试过类似的操作...,用黑色绘制矩形.我知道它是不正确的,只是尝试尝试一下.请帮助.非常紧急.
请frds ...帮助我.

使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
命名空间addRect
{
公共局部类Form1:Form
{
图片newImage = null;
bool btn1 = false;
bool btn2 = false;
矩形rect1;
公共Form1()
{
InitializeComponent();
}
私有void Form1_Load(对象发送者,EventArgs e)
{
newImage = Image.FromFile("C:\\ Documents and Settings \\ ing12675 \\ Desktop \\ images \\ ectopic-ruptured-1a.jpg");
pictureBox1.Image = newImage;
}
私有空pictureBox1_MouseClick(对象发送者,MouseEventArgs e)
{
//int x = e.X;
//int y = e.Y;
如果(btn1)
{
使用(Graphics g = Graphics.FromImage(pictureBox1.Image))
{
rect1 =新的Rectangle(e.X,e.Y,20,20);
g.DrawRectangle(Pens.Red,rect1);
btn1 = false;
}
}
否则,如果(btn2)
{
使用(Graphics g = Graphics.FromImage(pictureBox1.Image))
{
if(rect1.Contains(new Point(e.X,e.Y)))
{
g.DrawRectangle(Pens.Black,rect1);
btn2 = false;
}
}
}
pictureBox1.Invalidate();
}
private void button1_Click(对象发送者,EventArgs e)
{
btn1 = true;
pictureBox1.MouseClick + =新的MouseEventHandler(pictureBox1_MouseClick);
}
private void button2_Click(对象发送者,EventArgs e)
{
btn2 = true;
pictureBox1.MouseClick + =新的MouseEventHandler(pictureBox1_MouseClick);
}
private void button3_Click(对象发送者,EventArgs e)
{
pictureBox1.Image.Save(@"D:\\ test.jpg");
}

}
}
Here is my Code... i tried something like this... on delete button(button2), drawing the rectangle in black color. I know its not correct, but just trying something.please help. its very urgent.
please frds... help me.

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;
namespace addRect
{
public partial class Form1 : Form
{
Image newImage = null;
bool btn1 = false;
bool btn2 = false;
Rectangle rect1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
newImage = Image.FromFile("C:\\Documents and Settings\\ing12675\\Desktop\\images\\ectopic-ruptured-1a.jpg");
pictureBox1.Image = newImage;
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
//int x = e.X;
//int y = e.Y;
if (btn1)
{
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
rect1 = new Rectangle(e.X, e.Y, 20, 20);
g.DrawRectangle(Pens.Red, rect1);
btn1 = false;
}
}
else if (btn2)
{
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
if (rect1.Contains(new Point(e.X, e.Y)))
{
g.DrawRectangle(Pens.Black, rect1);
btn2 = false;
}
}
}
pictureBox1.Invalidate();
}
private void button1_Click(object sender, EventArgs e)
{
btn1 = true;
pictureBox1.MouseClick += new MouseEventHandler(pictureBox1_MouseClick);
}
private void button2_Click(object sender, EventArgs e)
{
btn2 = true;
pictureBox1.MouseClick += new MouseEventHandler(pictureBox1_MouseClick);
}
private void button3_Click(object sender, EventArgs e)
{
pictureBox1.Image.Save(@"D:\\test.jpg");
}

}
}


这篇关于在图像上添加和删除圆/矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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