Convert Graphics object to Bitmap

2020-03-30 03:03发布

问题:

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

回答1:

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!



回答2:

The method Graphics.CopyFromScreen is probably what you're looking for.

var rect = myControl.DisplayRectangle;
var destBitmap = new Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
using (var gr = Graphics.FromImage(destBitmap))
{
    gr.CopyFromScreen(myControl.PointToScreen(new Point(0, 0)), new Point(0, 0), rect.Size);
}