Unable to pass System.Drawing.Bitmap to C++ DLL

587 views Asked by At

I'm writing a DLL in C++ to search a subpicture in a Bitmap. When the C++ Part is executed, the HBITMAP is not valid.

Here is my code:

C#:

[System.Runtime.InteropServices.DllImport("FindSubPicture.dll", EntryPoint = "FindSubPictures", CallingConvention = CallingConvention.Cdecl)]
    private static extern System.IntPtr FindSubPicturesFct(System.IntPtr mImg, System.IntPtr sImg, int* nMatches);
    public static System.Collections.Generic.List<TPoint> FindSubPictures(System.Drawing.Bitmap mImg, System.Drawing.Bitmap sImg)
    {
        TPoint* PStack = null;
        int nMatches = 0;
        System.Collections.Generic.List<TPoint> MyList = new System.Collections.Generic.List<TPoint>();
        MyList.Clear();

        PStack = (TPoint*)FindSubPicturesFct(mImg.GetHbitmap(), sImg.GetHbitmap(), &nMatches);
        if(PStack == null) { return MyList;}

        for (int i = 0; i < nMatches; i++) { MyList.Add(new TPoint(PStack[i].x[0], PStack[i].x[1])); }
        try
        {
            System.Runtime.InteropServices.Marshal.FreeHGlobal((System.IntPtr)PStack);
        }catch(System.Exception ex) { System.Console.WriteLine(ex.Message); }

        return MyList;
    }

C++:

#include "FindSubPictures.h"
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <omp.h>

struct TPoint
{
    int x[2];

    TPoint(int fx, int fy) {
        x[0] = fx;
        x[1] = fy;
    }

    void Reset() { x[0] = 0; x[1] = 0; }
    void Set(int fx, int fy) { x[0] = fx; x[1] = fy; }
};

extern "C" __declspec(dllexport) TPoint* FindSubPictures(HBITMAP mImg, HBITMAP sImg, int* nMatches) {
    int mImgWidth = -1, mImgHeight = -1, sImgWidth = -1, sImgHeight = -1;
    TPoint* MyList = nullptr;

    if (mImg == nullptr || sImg == nullptr || nMatches == nullptr) { return nullptr; }


    return MyList;
}

Well, the C++ Library function is not doing anything until now. That's because the HBITMAP is not valid. In C#, the System.Drawing.Bitmap is valid.

At a breakpoint

When I enter "mIng.", no auto-completion is available.

Am I missing anything?

2

There are 2 answers

1
yacc On

Obtaining a bitmap does not mean you can access it like a data field. You'll have to lock the bitmap data in memory first. See also the answers in this thread here.

Bitmap.LockBits

Code would look as follows:

Rectangle rect = new Rectangle(0, 0, mImg.Width, mImg.Height);
System.Drawing.Imaging.BitmapData bmpData =
    mImg.LockBits(rect, System.Drawing.Imaging.ImageLockMode.Read,
    mImg.PixelFormat);

// Get the address of the first line.
IntPtr ptr2mImg = bmpData.Scan0;
0
Jan021981 On

Ok, I have the solution.

HBITMAP is only a handle "void *"

You first have to get the complete object by the function GetObject

Code:

extern "C" __declspec(dllexport) TPoint* FindSubPictures(HBITMAP mImg, HBITMAP sImg, int* nMatches) {
    int mImgWidth = -1, mImgHeight = -1, sImgWidth = -1, sImgHeight = -1;
    TPoint* MyList = nullptr;

    if (mImg== nullptr || sImg== nullptr || nMatches == nullptr) { return nullptr; }

    BITMAP mBMP, sBMP;
    GetObject(mImg, sizeof(BITMAP), &mBMP);
    GetObject(sImg, sizeof(BITMAP), &sBMP);
    /*Now, BITMAP mBMP and sBMP are valid*/

    return MyList;
}