Hi forum!
My name is Eduardo and I´m from Portugal. I´m a newbie to C++ and I hope to learn a lot with you
I´m facing a doubt that I couldn´t solve yet.
(I will also post this thread at c++ area, hope you don´t mind )

My doubt lies in the exchange of data between two different languages (C++ and C#). The doubt aroused from the request of build a cool interface! C++ doesn´t allow me to do a nice interface but c# does!
So the problem is that I´m sharing data between the two projects (the interface is a c# project and the image processing routines are in a c++ project. Both are in the same solution) using .dll export and import!
Basically I get the webcam image in c#, I process it using opencv and I return it to c# to show it in a picturebox.

However as the program goes long and complex I´m really afraid about the performance...
I know that QT is suitable to build an interface in c++ however it will not be so cool as a c# one....

Do you know a better way to do this sharing of data??
Thanks in advance for your time!

Eduardo

Dani AI

Generated

Short expert summary linked to the thread: wants a C# UI and an OpenCV/C++ processing backend with minimal overhead; ’s advice about not cross‑posting is sensible — keep the question and its updates in one forum. The core tradeoff is developer convenience versus frame‑rate overhead: aim to avoid per‑frame copies and expensive marshalling. (learn.microsoft.com)

Preferred, high‑performance pattern (same process)

  • Use a thin interop layer (C++/CLI or a native DLL called from C++/CLI) so managed code can pass a pointer directly into native routines; native code can wrap that pointer with cv::Mat without copying. This is the lowest‑overhead approach for realtime frames.
  • Example (concept):
// C# side: lock bitmap and get pointer
var bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
IntPtr scan0 = bmpData.Scan0;
// pass scan0 to C++/CLI or P/Invoke (as IntPtr)
bmp.UnlockBits(bmpData);
// C++ side: wrap external data as Mat (no copy)
cv::Mat frame(height, width, CV_8UC3, externalPtr, stride);

See C++/CLI guidance and Mat constructors that accept external data. (learn.microsoft.com)

When that isn’t possible

  • P/Invoke works but introduces marshalling and copies for complex buffers; it’s fine for infrequent calls but not ideal for per‑frame pipelines. (learn.microsoft.com)
  • For separate processes, use MemoryMappedFile (shared memory) for zero‑copy IPC and synchronize access with lightweight primitives. (learn.microsoft.com)
  • If the goal is faster development rather than writing interop, use a maintained .NET wrapper (Emgu CV or OpenCvSharp) so C# can call OpenCV directly. (emgu.com)

Cautions and gotchas

  • Be careful with pixel formats, stride, and channel order (OpenCV is BGR by default); conversion costs matter. Pinning/locking memory (GCHandle/LockBits) avoids copies but must be used briefly to avoid GC/fragmentation issues. (docs.opencv.org)

Bottom line: for best realtime performance in a single process, a small C++/CLI glue layer that wraps a locked/pinned C# buffer into cv::Mat is the most efficient; for quicker development, prefer OpenCvSharp/Emgu. (learn.microsoft.com)

>>(I will also post this thread at c++ area, hope you don´t mind )
Yes we do so don't do that. Pick a forum and stick with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.