C# 通过IntPtr循环?

C# 通过IntPtr循环?,c#,c++,intptr,C#,C++,Intptr,我的问题是:如何循环使用C#中IntPtr所指的内容? 我有C++代码调用C++代码。C++代码返回指向一个图像缓冲区的指针。C和C++之间的接口是在C语言中声明的一个ItpTR变量。 这是我的C代码: 这是我的C++代码: DllExport_ThorDiskIO ReadImage(char *selectedFileName, char* &outputBuffer) { TIFF* image; tsize_t stripSize; unsigned lo

我的问题是:如何循环使用C#中IntPtr所指的内容? 我有C++代码调用C++代码。C++代码返回指向一个图像缓冲区的指针。C和C++之间的接口是在C语言中声明的一个ItpTR变量。 这是我的C代码:

这是我的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;  
}
DllExport\u ThorDiskIO ReadImage(char*selectedFileName,char*&outputBuffer)
{
TIFF*图像;
t调整条带尺寸;
无符号长imageOffset,结果;
int stripMax,stripCount;
无符号长缓冲区大小;
wchar\u t*路径=(wchar\u t*)selectedFileName;
布尔状态;
//打开TIFF图像
if((image=tiffDll->TIFFOpenW(路径,“r”))==NULL){
//logDll->TLTraceEvent(VERBOSE_事件,1,L“无法打开传入映像”);
}
//读入可能的多个条带
stripSize=tiffDll->TIFFStripSize(图像);
stripMax=tiffDll->TIFFNumberOfStrips(图像);
imageOffset=0;
bufferSize=tiffDll->TIFFNumberOfStrips(图像)*stripSize;
对于(stripCount=0;stripCountTIFFReadEncodedStrip(图像、条带计数、输出缓冲区+图像偏移量、条带大小))=-1)
{
//logDll->TLTraceEvent(详细事件,1,L“输入条号读取错误”);
}
imageOffset+=结果;
}
//关闭TIFF图像
tiffDll->TIFFClose(图像);
如果(输出缓冲区>0)
{
//logDll->TLTraceEvent(详细事件,1,L“在输出缓冲区内:TRUE”);
状态=真;
}
其他的
{
//logDll->TLTraceEvent(详细事件,1,L“在输出缓冲区内:FALSE”);
状态=假;
}   
返回状态;
}
所以现在我认为我可以成功地获得IntPtr,但问题是:我如何使用它?如何循环图像缓冲区中的每个像素,例如(伪代码):

for(int y=0;y
这是如何使用IntPtr(指向本机内存的指针)在指向的图像中循环

//假设这实际上指向某个东西(不是零!!)
IntPtr pNative=IntPtr.Zero;
//假设这些是你的图像尺寸
int w=640//宽度
int h=480//高度
int ch=3//渠道
//图像循环
//使用不安全的
//这太快了!!
不安全的
{
对于(int r=0;r
IntPtr不是缓冲区,而是内存地址(在本例中,指向缓冲区的开头)。不能“循环”内存地址。但是,您可以使用不安全的代码通过不安全指针上的指针算法在缓冲区上循环。@Romoku:如果我有一块char*缓冲区怎么办?还是一个字节*?通常是8位数据结构?似乎IntPtr只有.ToInt32()和.ToInt64(),而且我似乎无法从缓冲区中正确检索我的值。@KonradRudolph:可能是一个例子?@KonradRudolph:我使用了不安全的代码,它可以工作。谢谢因此,如果你在2天内将你的评论作为答复,我将接受。谢谢
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;  
}
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....

    }
//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;
        }
    }
}