我的图形对象确实存在以下问题。
编辑:
我确实有一个picturebox_image (imageRxTx),它是来自摄像机的实时流。在paint事件中,我要做的是在图像imageRxTx上方绘制一些线(未在下面的代码中显示)。到目前为止,这没有问题。
现在,我需要检查imageRxTx中的圆圈,因此必须使用需要使用位图作为参数的ProcessImage()方法。不幸的是,我没有位图图像,而是有imageRxTx的句柄(hDC)。
问题:如何从图形对象获取imageRxTx并将其“转换”为需要在ProcessImage(Bitmap bitmap)方法中使用的位图图像?为了检查我的相机的实时流(imageRxTx),需要在绘画事件中连续调用此方法。
这是我的代码:
private void imageRxTx_paint(object sender, PaintEventArgs e)
{
var settings = new Settings();
// Create a local version of the graphics object for the PictureBox.
Graphics Draw = e.Graphics;
IntPtr hDC = Draw.GetHdc(); // Get a handle to image_RxTx.
Draw.ReleaseHdc(hDC); // Release image_RxTx handle.
//Here I need to send the picturebox_image 'image_RxTx' to ProcessImage as Bitmap
AForge.Point center = ProcessImage( ?....? );
}
// Check for circles in the bitmap-image
private AForge.Point ProcessImage(Bitmap bitmap)
{
//at this point I should read the picturebox_image 'image_RxTx'
...
视频图像在这里更新:
private void timer1_Elapsed(object sender, EventArgs e)
{
// If Live and captured image has changed then update the window
if (PIXCI_LIVE && LastCapturedField != pxd_capturedFieldCount(1))
{
LastCapturedField = pxd_capturedFieldCount(1);
image_RxTx.Invalidate();
}
}
顾名思义,您的主要问题是对Graphics
对象是什么的(常见)误解。
到目前为止,我可以毫无问题地绘制图形对象
不!A“ Graphics
”对象不不包含任何图形。它只是用于将图形绘制到相关图形上的工具。因此,您根本不会使用该对象。您可以用它来绘制,可能是某些或..的表面。 Bitmap
Graphics
imageRxTx
Control
Form
这行使用的是Bitmap
构造函数的一种通常令人困惑且无用的格式:
Bitmap bmp = new Bitmap(image_RxTx.Width, image_RxTx.Height, Draw);
最后一个参数几乎无所作为; 它唯一的功能是复制Dpi
设置。特别是,它不会从“绘图”中克隆或复制任何内容,“绘图”现在Graphics
不知道对象是什么,也没有任何其他设置。是的,那bmp Bitmap
之后仍然是空的。
如果要画入bmp
,则需要使用Graphics
实际绑定到的对象:
using (Graphics G = Graphics.FromImage(bmp)
{
// draw now..
// to draw an Image img onto the Bitmap use
G.DrawImage(img, ...);
// with the right params for source and destination!
}
没有这种可能应该发生的Paint
事件!但是所有准备代码都不清楚您真正想要做什么。您应该说明图纸的源头和目标!
相反,如果你想获得你画的东西到 image_RxTx
进入一个Bitmap
你可以用这种方法somwhere外(!)的Paint
事件:
Bitmap bmp = new Bitmap(image_RxTx.Width, image_RxTx.Height);
image_RxTx.DrawToBitmap(bmp, image_RxTx.ClientRectangle);
这将使用该Paint
事件来绘制控件成Bitmap
。结果不包括整体 PictureBox
:BackgroundImage
,,Image
和表面图!
更新:要获取的组合内容PictureBox
,即包含的内容Image
以及在表面上绘制的内容,如果触发事件的行Tick
位于aTimer
或紧随其后,则应使用上面的代码(最后2行)Paint
。(您没有向我们展示这是如何发生的。)您不能将它严格地放入Paint
事件本身,因为它将使用该Paint
事件,因此会导致无限循环!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句