Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Com从设备检索二进制图像(Tiff)数据_C#_.net_Com_Interop - Fatal编程技术网

C#Com从设备检索二进制图像(Tiff)数据

C#Com从设备检索二进制图像(Tiff)数据,c#,.net,com,interop,C#,.net,Com,Interop,我试图使用StringBuilder通过COM检索Tiff图像数据,但COM调用后缓冲区的长度仅为3。我正在将一个VB.NET版本转换为C#,它使用字符串而不是StringBuilder,工作正常。如果有人有任何建议或能给我指一些好的阅读材料,我将不胜感激 COM函数签名: ULONG MTMICRGetImage (char *pcDevName, char *pcImageID, char *pcBuffer, DWORD *pdwLength )) COM调用代码: ImageS

我试图使用StringBuilder通过COM检索Tiff图像数据,但COM调用后缓冲区的长度仅为3。我正在将一个VB.NET版本转换为C#,它使用字符串而不是StringBuilder,工作正常。如果有人有任何建议或能给我指一些好的阅读材料,我将不胜感激

COM函数签名:

ULONG MTMICRGetImage (char *pcDevName, char *pcImageID, char *pcBuffer, DWORD *pdwLength
))

COM调用代码:

    ImageSize = Convert.ToInt32(mtValue.ToString());
    TempImage = new StringBuilder(ImageSize);
    mtValueSize = 9216;

    RC = MTMICRGetIndexValue(mtDocInfo, "ImageInfo", "ImageURL", 2, mtValue, ref mtValueSize);

    // Allocate memory for image with size of ImageSize
    RC = MTMICRGetImage(ExcellaDeviceName, mtValue.ToString(), TempImage, ref ImageSize);

编辑:我相信这是由于二进制数据及其封送方式造成的,字符串中的字符4是空字符。根据Marshal.PtrToStringAuto()/Marshal.PtrToStringUni(),复制到第一个空字符之前的所有字符。

我找到了答案。该问题是由于编组时终止StringBuilder的空字符造成的。相反,我必须使用IntPtr并将字节直接从内存中读取到字节数组中。请参阅下面的解决方案

[DllImport("mtxmlmcr", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern Int32 MTMICRGetImages(string DeviceName, ref MagTekImage MagTekGImages, ref Int32 TotalImages);


// 
// Allocate memory for image with size of imageSize, because the image
// data has null characters (which marshalling doesn't like), we must
// get the memory location of the char* and read bytes directly from memory
//
IntPtr ptr = Marshal.AllocHGlobal(imageSize + 1);
RC = MTMICRGetImage(ExcellaDeviceName, mtValue.ToString(), ptr, ref imageSize);

// Copy the Image bytes from memory into a byte array for storing
byte[] imageBytes = new byte[imageSize];
Marshal.Copy(ptr, imageBytes, 0, imageSize);
Marshal.FreeHGlobal(ptr);

您确定ImageSize大于3吗?与中一样,ImageSize的值作为最后一个参数传递到MTMIRGETIMAGE。是的,图像大小可以。我将vb.net版本更改为使用StringBuilder,并且遇到了与C代码相同的问题。这3个字符是tiff文件头“ll*”的开头(在Ascii中,*=十进制表示42)。我会仔细检查大小以确保。图像大小很好,StringBuilder根据图像大小提供了最大容量,例如10754,但在COM调用之后,它的长度仅为3。灵感来自
[DllImport("mtxmlmcr", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern Int32 MTMICRGetImages(string DeviceName, ref MagTekImage MagTekGImages, ref Int32 TotalImages);


// 
// Allocate memory for image with size of imageSize, because the image
// data has null characters (which marshalling doesn't like), we must
// get the memory location of the char* and read bytes directly from memory
//
IntPtr ptr = Marshal.AllocHGlobal(imageSize + 1);
RC = MTMICRGetImage(ExcellaDeviceName, mtValue.ToString(), ptr, ref imageSize);

// Copy the Image bytes from memory into a byte array for storing
byte[] imageBytes = new byte[imageSize];
Marshal.Copy(ptr, imageBytes, 0, imageSize);
Marshal.FreeHGlobal(ptr);