c# - 通过 IntPtr 循环?

标签 c# c++ intptr

这是我的问题:如何在 C# 中循环遍历 IntPtr 指向的内容? 我有 C# 代码调用 C++ 代码。 C++ 代码返回一个指向一 block 图像缓冲区的指针。 C#和C++的接口(interface)是在C#中声明的一个IntPtr变量 所以这是我的 C# 代码:

private IntPtr _maskData;

public void LoadMask(string maskName)
{            
     _maskData = Marshal.AllocHGlobal(_imgWidth * _imgHeight * 1);
     ReadImage(maskName, ref _maskData);
}

[DllImport(@"D:\Projects\ImageStatistics\ImageStatisticsEllipse\Debug\DiskIO.dll", EntryPoint = "ReadImage")]
        private static extern int ReadImage([MarshalAs(UnmanagedType.LPWStr)]string path, ref IntPtr outputBuffer);

这是我的 C++ 代码:

DllExport_ThorDiskIO ReadImage(char *selectedFileName, char* &outputBuffer)
{
    TIFF* image;
    tsize_t stripSize;
    unsigned long imageOffset, result;
    int stripMax, stripCount;
    unsigned long bufferSize;
    wchar_t * path = (wchar_t*)selectedFileName;
    bool status;

    // Open the TIFF image
    if((image = tiffDll->TIFFOpenW(path, "r")) == NULL){
        //      logDll->TLTraceEvent(VERBOSE_EVENT,1,L"Could not open incoming image");

    }

    // Read in the possibly multiple strips
    stripSize = tiffDll->TIFFStripSize(image);
    stripMax = tiffDll->TIFFNumberOfStrips (image);
    imageOffset = 0;

    bufferSize = tiffDll->TIFFNumberOfStrips (image) * stripSize;

    for (stripCount = 0; stripCount < stripMax; stripCount++)
    {
        if((result = tiffDll->TIFFReadEncodedStrip (image, stripCount, outputBuffer + imageOffset, stripSize)) == -1)
        {
            //logDll->TLTraceEvent(VERBOSE_EVENT,1,L"Read error on input strip number");
        }
        imageOffset += result;
    }

    // Close the TIFF image
    tiffDll->TIFFClose(image);

    if(outputBuffer > 0)
    {
        //logDll->TLTraceEvent(VERBOSE_EVENT,1,L"inside output buffer: TRUE");
        status = TRUE;
    }
    else
    {
        //logDll->TLTraceEvent(VERBOSE_EVENT,1,L"inside output buffer: FALSE");
        status = FALSE;
    }   
    return status;  
}

所以现在我认为我可以成功获得 IntPtr,但真正的问题是:我如何使用它?我如何遍历图像缓冲区中的每个像素,例如(伪代码):

for (int y = 0; y < imgHeight; y++)
    for (int x = 0; x < imgWidth; x++)
    {
        int pixVal = IntPtr[y * imgWidth + x ];

        // do something to process the pixel value here....

    }

最佳答案

这是如何循环使用 IntPtr(指向 native 内存的指针)指向的图像

//assume this actually points to something (not zero!!)
IntPtr pNative = IntPtr.Zero;

//assume these are you image dimensions
int w=640; //width
int h=480; //height
int ch =3; //channels

//image loop
//use unsafe
//this is very fast!! 
unsafe
{
    for (int r = 0; r < h; r++)
    {
        byte* pI = (byte*)pNative.ToPointer() + r*w*ch; //pointer to start of row
        for (int c = 0; c < w; c++)
        {
            pI[c * ch]      = 0; //red
            pI[c * ch+1]    = 0; //green
            pI[c * ch+2]    = 0; //blue

//also equivalent to *(pI + c*ch)  = 0 - i.e. using pointer arythmetic;
        }
    }
}

关于c# - 通过 IntPtr 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15550821/

相关文章:

c# - 在 C# 中使用 WhatsappAPI 发送 whatsapp 消息

c# - 是否有处理 cookie 的 C# Rest 客户端?

c# - 在 Unity 中序列化多个变量的更简洁方法

c# - 将 blittable 结构复制到非托管内存位置的最快方法 (IntPtr)

c# - 如何使用 RDLC 报告的文本框属性将超链接 url 设置为在新窗口中打开?

c++ - 是否可以将其重写为一个类轮?

c++ - 我可以在 Windows Xp Pro SP3 系统上配置漫游用户配置文件吗?

c++ - 四舍五入到 C 中的次奇数或偶数整数

C# 我可以检查 IntPtr 是否为空吗?

c# - 在 C# 中将 IntPtr 转换为 int*?