C#中等价的C++内存复制

C++ Memory Copy Equivalent in C#

本文关键字:C++ 内存 复制      更新时间:2023-10-16

我正在编写一个Winform应用程序来从相机捕获图像。需要将从相机捕获的原始数据转换为DNG格式。制造商提供了一个描述相同情况的C++示例。

我在C#中使用了相同的逻辑,但是C#代码生成的DNG图像不正确。

我认为在C#代码中,"memcpy_s"等价项没有正确执行,因此它失败了。

由于我对C++不是很透彻,我可能把C#中的逻辑搞砸了。

任何意见/建议/有价值的指导都将非常有用。非常感谢您花时间仔细阅读此代码。

C++代码如下所示。

HRESULT hr = Nncam_PullStillImageV2(m_hcam, NULL, 24, &info);
if (SUCCEEDED(hr))
{
void* pRawData = malloc(info.width * info.height);   // allocating memory based on width and Height of the image.
hr = Nncam_PullStillImage(m_hcam, pRawData, 24, NULL, NULL);
if (SUCCEEDED(hr))
{
unsigned nFourCC = 0, bitsperpixel = 0;
Nncam_get_RawFormat(m_hcam, &nFourCC, &bitsperpixel);
BITMAPINFOHEADER bmpHeader = { 0 };
bmpHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpHeader.biPlanes = 1;
bmpHeader.biBitCount = bitsperpixel;
bmpHeader.biWidth = info.width;
bmpHeader.biHeight = info.height;
if (bitsperpixel > 8)
{
bmpHeader.biSizeImage = info.width * info.height * 2;
}
else
{
bmpHeader.biSizeImage = info.width * info.height;
}
bmpHeader.biCompression = nFourCC;
// core logic for DNG conversion adding headers and rawimage data to Ptr
void* pDIB = malloc(sizeof(BITMAPINFOHEADER) + bmpHeader.biSizeImage);
memcpy_s(pDIB, sizeof(BITMAPINFOHEADER), &bmpHeader, sizeof(BITMAPINFOHEADER));
memcpy_s((char*)pDIB + sizeof(BITMAPINFOHEADER), bmpHeader.biSizeImage,
pRawData, bmpHeader.biSizeImage);
XIMAGEINFO imageInfo = { 0 };
imageInfo.iType = IMAGEFORMAT_DNG;
imageInfo.iCodec = 1;
BOOL res = ImageLib_Save(_T("image.dng"), pDIB, &imageInfo);
free(pDIB);
}
free(pRawData);
}

C#代码

if (cam_.PullStillImageV2(IntPtr.Zero, 24, out info))   /* peek the width and height */
{
uint pnWidth, pnHeight;
var sbmp = new Bitmap((int)info.width, (int)info.height, PixelFormat.Format24bppRgb);
var bmpdata = sbmp.LockBits(new Rectangle(0, 0, sbmp.Width, sbmp.Height), ImageLockMode.WriteOnly, sbmp.PixelFormat);
if (cam_.PullStillImage(bmpdata.Scan0, 24, out pnWidth, out pnHeight))
{
uint nFourCC = 0, bitsperpixel = 0;
cam_.get_RawFormat(out nFourCC, out bitsperpixel);
BITMAPINFOHEADER bmpHeader = new BITMAPINFOHEADER();
bmpHeader.Init();
bmpHeader.biPlanes = 1;
bmpHeader.biBitCount = bitsperpixel;
bmpHeader.biWidth = info.width;
bmpHeader.biHeight = info.height;
if (bitsperpixel > 8)
{
bmpHeader.biSizeImage = info.width * info.height * 2;
}
else
{
bmpHeader.biSizeImage = info.width * info.height;
}
bmpHeader.biCompression = nFourCC;
// adding header and rawdata to PDIB might not be correct way as shown in C++
IntPtr pDIB = Marshal.AllocHGlobal((int)(Marshal.SizeOf(typeof(BITMAPINFOHEADER)) + bmpHeader.biSizeImage));
Marshal.StructureToPtr(bmpHeader, pDIB, true);
CopyMemory(pDIB + Marshal.SizeOf(typeof(BITMAPINFOHEADER)), bmpdata.Scan0, bmpHeader.biSizeImage);

XIMAGEINFO imageInfo = new XIMAGEINFO();
imageInfo.iType = 17;
imageInfo.iCodec = 1;
ImageLib_Save("image.dng", pDIB, ref imageInfo);
}
}

与C#相比,C++更适合使用,我的印象是,在C++中添加标头的工作只是能够在保存后将malloc的内存识别为图像。

我希望C#位图类能够为您处理这个问题。

您是否尝试过简单地保存位图?