在运行时在图片框中绘制一个框并将这些像素发送到函数 [英] drawing a box in a picture box in run time and sending those pixels to a function
本文介绍了在运行时在图片框中绘制一个框并将这些像素发送到函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个图片框,它从摄像头获取视频流。我有一个返回直方图的类,该部分工作正常。我希望能够在运行时使用我的鼠标并在
的图片框中绘制一个框,并获得一个统治的直方图。现在我有这个:
int height = pictureBox1.Image.Height;
int width = pictureBox1.Image.Width;
Bitmap bmp = new Bitmap (pictureBox1.Image);
// Luminance
ImageStatisticsHSL hslStatistics = new ImageStatisticsHSL(bmp);
int[] luminanceValues = hslStatistics.Luminance.Values;
// RGB
ImageStatistics rgbStatistics = new ImageStatistics(bmp);
int[] redValues = rgbStatistics.Red.Values;
int[] greenValues = rgbStatistics.Green.Values;
int[] blueValues = rgbStatistics.Blue.Values;
如何从框中绘制区域并仅将其发送到我的ImageStatisticsHSL类?
推荐答案
处理源图片框的鼠标事件和绘图事件。在paint事件处理程序中绘制rect。
handle the mouse events and the paint event of the source pictureBox. Draw the rect in the paint event handler.
在检查rect的大小和位置后,通过克隆获取orig bmp的一部分。
Get a section of the orig bmp by cloning after checking the rect size and location.
public partial class Form1 : Form
{
PictureBox p1 = new PictureBox();
PictureBox p2 = new PictureBox();
Point _st = new Point();
Point _end = new Point();
public Form1()
{
InitializeComponent();
p1.Size = new Size(200, 200);
p2.Size = new Size(200, 200);
p1.Location = new Point(12, 12);
p2.Location = new Point(224, 12);
p2.SizeMode = PictureBoxSizeMode.Zoom;
p1.MouseDown += P1_MouseDown;
p1.MouseMove += P1_MouseMove;
p1.MouseUp += P1_MouseUp;
p1.Paint += P1_Paint;
this.Controls.Add(p1);
this.Controls.Add(p2);
this.FormClosing += Form1_FormClosing;
p1.Image = GetBitmap();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (p1.Image != null)
p1.Image.Dispose();
if (p2.Image != null)
p2.Image.Dispose();
}
private Bitmap GetBitmap()
{
Bitmap bmp = new Bitmap(200, 200);
using (Graphics g = Graphics.FromImage(bmp))
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.Clear(Color.Green);
g.FillEllipse(Brushes.Yellow, new RectangleF(0, 0, 200, 200));
}
return bmp;
}
private void P1_Paint(object sender, PaintEventArgs e)
{
if (_end.X - _st.X > 0 && _end.Y - _st.Y > 0)
e.Graphics.DrawRectangle(Pens.Aqua, new Rectangle(_st.X, _st.Y, _end.X - _st.X, _end.Y - _st.Y));
}
private void P1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_end = e.Location;
if (_end.X > p1.Image.Width - 1)
_end = new Point(p1.Image.Width - 1, _end.Y);
if (_end.Y > p1.Image.Height - 1)
_end = new Point(_end.X, p1.Image.Height - 1);
if (_st.X >= 0 && _st.Y >= 0 && _end.X < p1.Image.Width && _end.Y < p1.Image.Height)
{
if (_end.X - _st.X > 0 && _end.Y - _st.Y > 0)
{
Bitmap bmp = (Bitmap)p1.Image;
Image iOld = p2.Image;
p2.Image = bmp.Clone(new Rectangle(_st.X, _st.Y, _end.X - _st.X, _end.Y - _st.Y), bmp.PixelFormat);
if (iOld != null)
{
iOld.Dispose();
iOld = null;
}
}
}
}
}
private void P1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_end = e.Location;
this.p1.Invalidate();
}
}
private void P1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_st = e.Location;
}
}
}
问候,
Thorsten
Thorsten
这篇关于在运行时在图片框中绘制一个框并将这些像素发送到函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文