English 中文(简体)
通过C#至C++的比图 未管理的守则
原标题:Passing Bitmap from C# to C++ Unmanaged code

我在试图在C#中写成法典,以便把图带给一个未管理的C++ DLL,并返回一个POINT结构。

我在互联网上做了大量研究,但没有发现“Gotcha”条款或法典的一部分帮助我解决我的问题。

迄今为止,我能够讨论的最佳途径是管理不畅的++ DLL,由我C#的表号所呼吁,用一个管理得起的++路程。 在测试时,我可以穿透简单的类型,如ger,并送回无问题的愤怒。 现在,就我所持有的问题而言,这份蓝图。

我曾尝试过通过《经济、社会、文化权利国际公约》(利用我方位标的格特·贝图(GetHBitmap)(c#)的功能),但在汇编“从系统看不到参数1:由于保护水平,“IntPtr”到《经济、社会、文化权利国际公约》”和“Class1.FindImage(撤销*,无效)”时,我发现错误。

我的一些法典是:

主要应用:

class Program
{
    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int X;
        public int Y;

        public POINT(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }

        public static implicit operator System.Drawing.Point(POINT p)
        {
            return new System.Drawing.Point(p.X, p.Y);
        }

        public static implicit operator POINT(System.Drawing.Point p)
        {
            return new POINT(p.X, p.Y);
        }
    }

    static void Main(string[] args)
    {
        Image src = Image.FromFile("Pin.png");
        Image tgt = Image.FromFile("screen.png");

        Bitmap bsrc = new Bitmap(src);
        Bitmap btgt = new Bitmap(tgt);

        IntPtr bs = bsrc.GetHbitmap();
        IntPtr bt = btgt.GetHbitmap();

        POINT p = Class1.FindImage(bs, bt);
    }
}

ImportWrap.h:

namespace ImportWrap {

public ref class Class1
{
public:
    static POINT FindImage(IntPtr source, IntPtr target);
};
}

ImportWrap.cpp:

static POINT FindImage(IntPtr source, IntPtr target)
{
return ImgFuncs::MyImgFuncs::FindImage(source, target);
}

ImgFuncs.h

typedef long LONG;
typedef void *PVOID;
typedef PVOID HANDLE;
typedef HANDLE HBITMAP;

typedef struct tagPOINT {
  LONG x;
  LONG y;
} POINT, *PPOINT;

namespace ImgFuncs
{

    class MyImgFuncs
    {
    public:
        static __declspec(dllexport) POINT FindImage(HBITMAP source, HBITMAP target);
    };
}

ImgFuncs.cpp

namespace ImgFuncs
{
POINT MyImgFuncs::FindImage(HBITMAP source, HBITMAP target)
{
    POINT p;

    p.x = 1;
    p.y = 2;
    return p;
}
}

What am I doing wrong, or am I going completely off the map with my approach? I would GLADLY entertain any suggestions regarding the correct way to code what I am trying to do. I have some C++ code that is used to find an image within another image that works quite fast. Unfortunately, even using lockbits in c#, it s not fast enough. So I would like to make use of the c++ code for the image search.

我相信,我会走到更多的天头,但如果我能够接过这一 st脚石,我可以处理。 如你所知,我的C++知识有限。

最佳回答

我们不能把土质类作为回报,C#可以处理。 从IntPtr到HBITMAP,需要翻一番。 与此类似:

static System::Drawing::Point FindImage(IntPtr source, IntPtr target)
{
    POINT retval = ImgFuncs::MyImgFuncs::FindImage((HBITMAP)(void*)source, (HBITMAP)(void*)target);
    return System::Drawing::Point(retval.X, retval.Y);
}

添加关于系统的内容。 引文。 那么,你可能还要考虑通过一项比特马普特,而不是让它更容易使用。

问题回答

也许你可以完全从C#通过P/Invoke,把管理得起的C++包裹从C#中提掉。 您的C#等类

[DllImport("YourUnamangedDll.dll")]
private static extern POINT FindImage(IntPtr source, IntPtr target);

我没有对此进行测试,因此,你可能需要根据需要加以 t击,但这符合一般的想法。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签