C#中等价的C++内存复制
C++ Memory Copy Equivalent in C#
我正在编写一个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#位图类能够为您处理这个问题。
您是否尝试过简单地保存位图?
相关文章:
- 将字符串存储在c++中的稳定内存中
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- Win32编译器选项和内存分配
- 当vector是tje全局变量时,c++中vector的内存管理
- 带内存和隔离功能的SQLite
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 迭代时从向量和内存中删除对象
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 为什么示例代码访问IUnknown中已删除的内存
- 如何在C++类内存结构中创建"spacer"?
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 类型总是使用其大小存储在内存中吗
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存