Figure in C # Bitmap with C ++

I have a third-party DLL (simple C ++) that draws some lines on the HDC. I want these lines to be on a bitmap or C # form.

I tried to give C ++ HBITMAP or HDC Graphics.FromImage (bitmap) but none of the above methods worked for me.

Everything works fine with MFC TestApp using the following code

HWND handle = pStatic->GetSafeHwnd();
CDC* dc = pStatic->GetDC();

Draw(dc);

My question is: What do I need to do / use to draw on a bitmap or form using the Draw (HDC) method?

I hope you help me. Thanks in advance,

Patrick

+5
source share
1 answer

For drawing in raster C # use this code:

        Graphics gr = Graphics.FromImage(MyBitmap);
        IntPtr hdc = gr.GetHdc();
        YourCPPDrawFunction(hdc);
        gr.ReleaseHdc(hdc);

YourCPPDrawFunction example :

    void YourCPPDrawFunction(HDC hDc)
    {
        SelectObject(hDc, GetStockObject(BLACK_PEN));
        Rectangle(hDc, 10, 10, 20, 20);
    }

To draw directly on the surface of the form, use this code:

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        IntPtr hdc = e.Graphics.GetHdc();
        YourCPPDrawFunction(hdc);
        e.Graphics.ReleaseHdc(hdc);
    }

Remember to call Graphics.ReleaseHdc () after you finish drawing, otherwise you will not see the results of your drawing.

+5
source

All Articles