我的图形对象确实存在以下问题。
编辑:
我确实有一个 picturebox_image (imageRxTx),它是来自相机的实时流。我在绘制事件中所做的就是在图像 imageRxTx 上绘制一些线条(下面的代码中未显示)。到目前为止,这没有问题。
现在我需要检查 imageRxTx 中的圆圈,因此我必须使用需要 Bitmap 作为参数的方法 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
对象是什么有一个(常见的)误解。
到目前为止,我可以毫无问题地绘制图形对象
不! “
Graphics
”对象不包含任何图形。它只是用于将图形绘制到相关位图
上的工具。因此,您根本不绘制Graphics
对象;你用它来绘制imageRxTx
,无论那是什么,可能是一些Control
或Form
的表面..这一行使用了一种经常令人困惑且无用的
位图
格式 constructor :
Bitmap bmp = new Bitmap(image_RxTx.Width, image_RxTx.Height, Draw);
最后一个参数几乎什么都不做;它的唯一功能是复制 Dpi
设置。特别是,它不会克隆或复制“Draw”中的任何内容,正如您现在所知,“Graphics”对象无论如何都没有“Draw”内容,也没有任何其他设置。所以是的,之后 bmp 位图
仍然是空的。
如果您想绘制到 bmp
中,您需要使用实际绑定(bind)到它的 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
转化为 位图
,您可以使用此方法 位于 (!) Paint
事件之外的地方:
Bitmap bmp = new Bitmap(image_RxTx.Width, image_RxTx.Height);
image_RxTx.DrawToBitmap(bmp, image_RxTx.ClientRectangle);
这将使用Paint
事件将控件绘制到位图
中。并不是说结果包括整个 PictureBox
:BackgroundImage
、Image
和表面绘图!
更新:要获取PictureBox
的组合内容,即它的Image
和您的内容绘制到表面上后,您应该在 Timer
的 Tick
事件中使用上面的代码(最后 2 行)或触发 Paint
事件的行。 (您没有向我们展示这是如何发生的。)您实际上不能将其放入 Paint
事件本身,因为它将使用 Paint
事件,因此会导致无限循环!
关于c# - 将图形对象转换为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29772266/