Native C++ 与 CLI/C++ 数组、字符串、Bitmap 转换工具函数

注: COM 返回的字符串类型 是Unicode 宽字符 wchar(2字节), 而我们用的是 Ascii 字符char_t(窄字符,一字节), 需要进行转化。 这在使用COM 字符串的时候 需要注意。

  1. // Returns a float array for a managed float array. // Caller must free the memory using the delete[] operator!
  2. inline float* GetUnmanagedArray(array<float>^ managedArray)
  3.  {
  4.       if (nullptr == managedArray)
  5.              return NULL;
  6.       if (managedArray->Length == 0)
  7.              return NULL;
  8.       pin_ptr<float> p = &managedArray[0];
  9.       float* pSource = p;
  10.       float* pDestination = new float[managedArray->Length];
  11.       ::memcpy( pDestination, pSource, managedArray->Length * sizeof(float) );
  12.       return pDestination;
  13.  }
  1. // Returns a int array for a managed int array.  
  2. // Caller must free the memory using the delete[] operator!
  3. inline int* GetUnmanagedArray(array<int>^ managedArray)
  4. {
  5.     if (nullptr == managedArray)
  6.         return NULL;
  7.     if (managedArray->Length == 0)
  8.         return NULL;
  9.     pin_ptr<int> p = &managedArray[0];
  10.     int* pSource = p;
  11.     int* pDestination = new int[managedArray->Length];
  12.     ::memcpy( pDestination, pSource, managedArray->Length * sizeof(int) );
  13.     return pDestination;
  14. }
  1. inline CString FromManaged(System::String^ other)
  2. {
  3.     System::IntPtr pointer = System::Runtime::InteropServices::Marshal::StringToHGlobalUni(other);
  4.     const wchar_t* internalBuffer = (const wchar_t*)pointer.ToPointer();
  5.     CString cstrResult( internalBuffer );
  6.     System::Runtime::InteropServices::Marshal::FreeHGlobal( pointer );
  7.     return cstrResult;
  8. }
  9.                 
  10. inline System::String^ FromUnmanaged(const CString& other)
  11. {
  12.     return gcnew System::String(other);
  13. }
  1. // This function use to convert managed string to wchar unmanaged string.
  2. // 将托管String 转换成 非托管 Unicode String
  3. wchar_t * ManagedStringToUnmanagedStringA(String^ strIn)
  4. {
  5.     IntPtr ip = Marshal::StringToHGlobalUni(strIn);
  6.     const wchar_t* pTemp = static_cast<const wchar_t*>(ip.ToPointer());
  7.     wchar_t *pOut = new wchar_t[wcslen(pTemp) + 1];
  8.     wcscpy_s(pOut, wcslen(pTemp), pTemp);
  9.     Marshal::FreeHGlobal(ip);
  10.     return pOut;
  11. }
  1. // This function use to convert managed Bitmap to native byte array.
  2. // 通过 MemoryStream 来取道 Bitmap 的所有2进制数据,包括BitmapHeadInfo 等信息。
  3. BYTE* BmpToBytes_MemoryStream (Bitmap^ bmp)
  4. {
  5.     System::IO::MemoryStream^ ms = gcnew System::IO::MemoryStream();
  6.     // Save to memory using the Jpeg format
  7.     bmp->Save (ms, System::Drawing::Imaging::ImageFormat::Bmp);
  8.     
  9.     // read to end
  10.     cli::array<BYTE>^ bmpBytes = ms->GetBuffer();
  11.     pin_ptr<BYTE> p = &bmpBytes[0];
  12.     BYTE* pSource = p;
  13.     BYTE* pDestination = new BYTE[bmpBytes->Length];
  14.     ::memcpy( pDestination, pSource, bmpBytes->Length * sizeof(BYTE) );
  15.     
  16.     ms->Close();
  17.     return pDestination;
  18. }
  1. // Convert BitmapData to bytes array.
  2. // 只是把BitmapData 转出,而不包含图片头的信息,一般Publish中 也只是需要这些。
  3. BYTE* BitmapDataToBytes (Bitmap^ bmp)
  4. {
  5.     System::Drawing::Point point;
  6.     System::Drawing::Rectangle rect(point, bmp->Size);
  7.     BitmapData^ bData = bmp->LockBits(rect,
  8.         ImageLockMode::ReadOnly, 
  9.         PixelFormat::Format24bppRgb);
  10.     // number of bytes in the bitmap
  11.     int byteCount = bData->Stride * bmp->Height;
  12.     array<BYTE>^ bmpBytes = gcnew array<BYTE>(byteCount);
  13.     // Copy the locked bytes from memory
  14.     Marshal::Copy (bData->Scan0, bmpBytes, 0, byteCount);
  15.     // don't forget to unlock the bitmap!!
  16.     bmp->UnlockBits (bData);
  17.     pin_ptr<BYTE> p = &bmpBytes[0];
  18.     BYTE* pSource = p;
  19.     BYTE* pDestination = new BYTE[bmpBytes->Length];
  20.     ::memcpy( pDestination, pSource, bmpBytes->Length * sizeof(BYTE) );
  21.     return pDestination;
  22. }


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值