我正在用C ++编写一个DLL来搜索Bitmap中的子图片。
执行C ++部分时,HBITMAP
无效。
这是我的代码:
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;
}
嗯,直到现在,C ++ Library函数还没有做任何事情。那是因为HBITMAP
无效。在C#中,System.Drawing.Bitmap
有效。
当我输入“mIng。”时,没有自动完成功能。
我错过了什么吗?
答案 0 :(得分:0)
获取位图并不意味着您可以像数据字段一样访问它。您必须先将位图数据锁定在内存中。 另请参阅this thread中的答案。
代码如下:
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;
答案 1 :(得分:0)
好的,我有解决方案。
HBITMAP只是一个句柄&#34; void *&#34;
首先必须通过函数GetObject
代码:
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;
}