转换Graphics对象为位图 [英] Convert Graphics object to Bitmap

查看:161
本文介绍了转换Graphics对象为位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题,我的图形对象。

编辑:

我有一个picturebox_image (imageRxTx)这是来自摄像头的实时流。我在内线做的活动是借助图像imageRxTx(下code未显示)的顶部一些行。这工作到目前为止没有问题。

现在我需要检查在imageRxTx圈,所以我一定要使用,需要一个位图作为参数的方法 processImage来()。不幸的是我没有位图图像,而是把手的(HDC)以我imageRxTx。

:我怎样​​才能从我的图形对象得到imageRxTx和转换到一个位图图像,我需要在方法的 processImage来(位图位图)使用?这种方法需要在油漆事件continously所谓为了检查我的相机(imageRxTx)的实时数据流。

下面我code:

 私人无效imageRxTx_paint(对象发件人,PaintEventArgs的E)
    {
        VAR设置=新设置();        //创建图形的本地版本对象的图片框。
        图形绘制= e.Graphics;
        IntPtr的的hDC = Draw.GetHdc(); //获取一个句柄image_RxTx。        Draw.ReleaseHdc(HDC); //释放image_RxTx句柄。
        //这里我需要在picturebox_imageimage_RxTx发送到processImage来为位图
        AForge.Point中心= processImage来(......?);
    }
    //位图图像在检查圈
    私人AForge.Point processImage来(位图位图)
    {
        //在这一点上我应该读picturebox_imageimage_RxTx
        ...

视频图像在这里更新:

 私人无效timer1_Elapsed(对象发件人,EventArgs的发送)
    {
        //如果Live和捕捉到的图像发生了变化然后更新窗口
        如果(PIXCI_LIVE&放大器;&放大器;!LastCapturedField = pxd_capturedFieldCount(1))
        {
            LastCapturedField = pxd_capturedFieldCount(1);
            image_RxTx.Invalidate();
        }
    }


解决方案

正如标题所暗示的,您的主要问题是关于图形目标是什么(通用)的误解

到目前为止,我可以借鉴到我的图形对象没有问题


  • 没有! A'图形'对象确实的不可以包含的任何图形。只有在工具用于绘制的图形到相关 位图。因此,您不要画到图形对象在所有;你用它来绘制到 imageRxTx ,不管它是什么,有些可能是表面控制表格 ..


  • 这行是使用位图构造



  BMP位图=新位图(image_RxTx.Width,image_RxTx.Height,抽奖);

最后一个参数是干什么的旁边没有;它的唯一功能是复制 dpi的设置。尤其是,它并没有克隆或复制'画',正如你现在知道,一个图形对象没有任何反正内容,也没有其任何其他设置。所以,是的,在 BMP位图的仍然是空之后。

如果你想画成 BMP 您需要使用实际上是绑定到一个图形对象:

 使用(图形G = Graphics.FromImage(BMP)
{
   //现在绘制..
   //绘制图像IMG到使用位图
   G.DrawImage(IMG,...);
   与源和目标右PARAMS //!
}

本应在油漆事件可能发生!但是,所有的preparation code为不清楚,以你真正想做的事情。你应该解释一下究竟什么是图纸的来源,什么是目标!

相反,如果你想获得你画的 image_RxTx 的东西位图您可以使用此方法的 somwhere外油漆事件(!):

  BMP位图=新位图(image_RxTx.Width,image_RxTx.Height);
image_RxTx.DrawToBitmap(BMP,image_RxTx.ClientRectangle);

这将使用油漆事件绘制控件成位图。这并不是说该结果包含在全部 图片框:在的BackgroundImage 图片 的表面拉丝!

更新:要获得合并图片框的内容,这既是它图片和你画在表面上的东西,你应该使用勾选事件code以上(最后2行)的定时 右后触发油漆事件行。 (你没有告诉我们如何出现这种情况。)你不能实际上可以把它放在油漆事件本身,因为它会使用油漆事件,并因此导致无限循环!

I do have following issue with my graphics object.

EDIT:

I do have a picturebox_image (imageRxTx) which is a live stream from a camera. What I do in the paint event is to draw some lines on top of the image imageRxTx (not shown in the code below). This works so far without problem.

Now I need to check for circles in imageRxTx and therefore I have to use the method ProcessImage() which needs a Bitmap as parameter. Unfortunately I do not have the Bitmap image but rather the handle (hDC) to my imageRxTx.

Question: How can I get the imageRxTx from my graphics-object and "convert" it to a bitmap-image which I need to use in the method ProcessImage(Bitmap bitmap)? This method needs to be called continously in the paint-event in order to check the live-stream of my camera (imageRxTx).

Here my code:

    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'
        ...

The video image is updated here:

    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();
        }
    }

解决方案

As the title suggests, your main problem is a (common) misconception about what a Graphics object is.

So far I can draw to my graphics object without problem

  • No! A 'Graphics' object does not contain any graphics. It is only the tool used to draw graphics onto a related Bitmap. So you don't draw onto the Graphics object at all; you use it to draw onto imageRxTx, whatever that is, probably the surface of some Control or Form..

  • This line is using an often confusing rather useless format of the Bitmap constructor:


Bitmap bmp = new Bitmap(image_RxTx.Width, image_RxTx.Height, Draw); 

The last parameter is doing next to nothing; its only function is to copy the Dpi setting. In particular it does not clone or copy any content from 'Draw', which, as you know now, a Graphics object doesn't have anyway, nor any of its other settings. So yes, the bmp Bitmap is still empty after that.

If you want to draw into bmp you need to use a Graphics object that is actually bound to it:

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!
}

None of this should probably happen in the Paint event! But all the preparation code is unclear as to what you really want to do. You should explain just what is the source of the drawing and what is the target!

If instead you want to get the stuff you draw onto image_RxTx into a Bitmap you can use this method somwhere outside (!) the Paint event:

Bitmap bmp = new Bitmap(image_RxTx.Width, image_RxTx.Height);
image_RxTx.DrawToBitmap(bmp, image_RxTx.ClientRectangle);

This will use the Paint event to draw the control into a Bitmap. Not that the result includes the whole PictureBox: The BackgroundImage, the Image and the surface drawing!

Update: To get the combined content of the PictureBox, that is both its Image and what you have drawn onto the surface, you should use the code above (the last 2 lines) in the Tick event of a Timer or right after the line that triggers the Paint event. (You didn't show us how that happens.) You can't acutally put it in the Paint event itself, as it will use the Paint event and therefore would cause an infinite loop!

这篇关于转换Graphics对象为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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