C# 通过导出dll从CUDA中的指针加载图像
我对这种东西不熟悉。我试图创建一个函数,它相当于图像的直方图函数。我使用windows窗体应用程序显示直方图(并加载图像),使用CUDA/c++制作直方图。我从一开始就提到,我没有使用openCV、glut、OpenGL或任何其他第三个库。继续。。。我试图把位图传递给一个非托管C++ DLL。这里的问题是,我现在不知道如何在C++代码中引用位图。(甚至如何从中获得RGB)。 代码片段: c#: Dll导入:C# 通过导出dll从CUDA中的指针加载图像,c#,c++,dll,dllimport,hbitmap,C#,C++,Dll,Dllimport,Hbitmap,我对这种东西不熟悉。我试图创建一个函数,它相当于图像的直方图函数。我使用windows窗体应用程序显示直方图(并加载图像),使用CUDA/c++制作直方图。我从一开始就提到,我没有使用openCV、glut、OpenGL或任何其他第三个库。继续。。。我试图把位图传递给一个非托管C++ DLL。这里的问题是,我现在不知道如何在C++代码中引用位图。(甚至如何从中获得RGB)。 代码片段: c#: Dll导入: using System; using System.Runtime.InteropSe
using System;
using System.Runtime.InteropServices;
namespace HistogramProcessingCs
{
class NativeMethods
{
[DllImport("HistogramProcessingCpp.dll", CallingConvention = CallingConvention.StdCall)]
public static extern unsafe int** GenerateHistogram(IntPtr bmp, int dimensionImage);
}
}
c++:
好的,我在谷歌搜索了两个小时,堆积如山和微软文档制作之后,终于想出了办法
因为你使用CUDA,我认为你只需要快速和好的解决方案,这就是为什么我试图找到一种方法,你可以修改数据而不复制它,因为C和C++连接很多次。 到目前为止我就是这么做的
有些事情很重要。 我使用了一个整数指针,这对当前代码来说有点混乱。 当然,您可以使用更合理的char指针(尚未测试)。 另一件事是系统.Drawing.Imaging.PixelFormat
。
知道你选择了哪一个真的很重要。在我的示例中,我选择了PixelFormat.Format32bppRgb
。
字节顺序(到目前为止我已经知道)就是名称的顺序
例子:
32bppRgb代表红色、绿色和蓝色,每个消耗8位。
但是,因为它是这种格式,而不是24bppRgb,所以它会消耗一个整数(不使用8位)。在这种情况下,不使用前8位(从左到右思考),因此要将像素设置为红色,其工作原理如下。(很抱歉,格式化没有按预期工作…)
|8 | 8 | 8 | 8 |消耗位
|空|红|绿|蓝|色
|00 | FF | 00 | 00 |红色色码
红色的代码是这样的
=>0x00 FF 00
小数点是16711680。这就是C++中的数字来自.< /p>
C++代码:
头文件“nativellibrary.h”:
Cpp文件“NativeLibrary.Cpp”:
这段代码将生成一个完全红色的位图。我正在深入研究这一点,希望我的答案能帮助人们:)!谢谢你的提问。现在我知道如何用C++做C++的东西了:多谢!你搞定了!!哈哈,非常感谢:D。如果这个问题还剩下什么,欢迎评论。我只是想改进一下
using System;
using System.Runtime.InteropServices;
namespace HistogramProcessingCs
{
class NativeMethods
{
[DllImport("HistogramProcessingCpp.dll", CallingConvention = CallingConvention.StdCall)]
public static extern unsafe int** GenerateHistogram(IntPtr bmp, int dimensionImage);
}
}
extern "C" __declspec(dllexport) int** __stdcall GenerateHistogram(unsigned char *bmp, int dimensionImage)
{
//How to refere the bitmap from the bmp pointer?
//dimensionImage is Width = Height
}
namespace NativeLibrary
{
extern "C" __declspec(dllexport) void __stdcall PassBitmap(int* number, int size);
}
#include <NativeLibrary.h>
void NativeLibrary::PassBitmap(int* number, int size) {
for (int i = 0; i < size; i++) {
number[i] = 16711680;
}
}
using System.Drawing;
using System.Runtime.InteropServices;
[DllImport("NativeLibrary.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void PassBitmap(IntPtr bmp, int size);
public System.Drawing.Bitmap bitmap = null;
public void GenerateAndModifyBitmap()
{
//The pixel format is essential!
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(100, 100, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
//Just which region we want to lock of the bitmap
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(new System.Drawing.Point(), bmp.Size);
//We want to read and write to the data, pixel format stays the same (anything else wouldn't make much sense)
System.Drawing.Imaging.BitmapData data = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
//This is a pointer to the data in memory. Can be manipulated directly!
IntPtr ptr = data.Scan0;
// This code is specific to a bitmap with 32 bits per pixels.
// Ignore current calculations. They are still work in progress xD
int size = bmp.Height;
size *= Math.Abs(data.Stride);
size /= 4;
//Call native function with our pointer to the data and of course how many ints we have
PassBitmap(ptr, size);
//Work is finished. Give our data back to the manager
bmp.UnlockBits(data);
bitmap = bmp;
}