// Property Data-> Object Data Change BOOL CGXGridCellData::SetMultiCaptionBlobDataChange ( CString strPropertyData ) { DeleteContents(); if ( strPropertyData.IsEmpty() ) { return FALSE; } BYTE* pvData = NULL; pvData = StringToBinaryEx( strPropertyData ); ASSERT( pvData != NULL ); if( pvData != NULL ) { DWORD dwDataLen; dwDataLen = *(long*)pvData; CMemFile memFile; memFile.Attach ( (BYTE*)pvData+sizeof(DWORD), dwDataLen - sizeof(DWORD) ); CArchive arMem(&memFile, CArchive::load ); Serialize ( arMem ); free ( pvData ); return TRUE; } return FALSE; }
///////////////////////////////////////////////////////////////////////////// // serialization void CGXGridCellData::SerializeEx(CArchive& ar, CString& strProperty ) { ASSERT_VALID(this); DWORD dwDataLen; if (ar.IsStoring()) // Write { CMemFile memFile; CArchive arMem(&memFile, CArchive::store ); Serialize ( arMem ); arMem.Flush(); dwDataLen = memFile.GetLength(); if ( dwDataLen > 0 ) { BYTE *pTemp = new BYTE [dwDataLen+1]; memFile.Seek(0, CFile::begin); memFile.Read(pTemp, dwDataLen ); ar.Write(&dwDataLen, sizeof(DWORD)); ar.Write(pTemp, dwDataLen); // Property Save CString strProperty = BinaryToStringEx ( pTemp, dwDataLen ); if ( pTemp ) delete []pTemp; } } else // Read { ar >> dwDataLen; if ( dwDataLen > 0 ) { BYTE *pTemp = new BYTE[dwDataLen+1]; ar.Read(pTemp, dwDataLen ); CMemFile memFile; memFile.Attach ( (BYTE*)pTemp, dwDataLen ); CArchive arMem(&memFile, CArchive::load ); Serialize ( arMem ); // Property Save CString DWORD dwTotLen = dwDataLen + sizeof(DWORD); BYTE *pvData = new BYTE[dwTotLen]; *(DWORD*)pvData = dwTotLen; memcpy ( pvData + sizeof(DWORD), pTemp, dwDataLen ); if ( pTemp ) delete[] pTemp; strProperty = BinaryToStringEx ( pvData, dwTotLen ); if ( pvData ) delete[] pvData; } } }
BOOL CSampleGrabberCallback2::ImageConvert() { BYTE *pBuffer = m_CusBuffer.pBuffer0; long lwidth = m_CusBuffer.sizeBuf0.cx; long lheight = m_CusBuffer.sizeBuf0.cy; ULONG BufferLen = m_CusBuffer.ulBufLen0; BYTE *pImage = NULL; long lBitCount = 0; long lsize = 0; if(MEDIASUBTYPE_YUY2 == m_mtStillMediaType.subtype || MEDIASUBTYPE_I420 == m_mtStillMediaType.subtype || MEDIASUBTYPE_RGB24 == m_mtStillMediaType.subtype || MEDIASUBTYPE_RGB32 == m_mtStillMediaType.subtype) { if (MEDIASUBTYPE_YUY2 == m_mtStillMediaType.subtype) { lsize = lwidth * lheight * 3; pImage = new BYTE[lsize]; if(NULL == pImage) { return FALSE; } CFormatConvertArithmetic::YUY2ToRGB24(pBuffer, pImage, lwidth, lheight); lBitCount = 24; } else if (MEDIASUBTYPE_I420 == m_mtStillMediaType.subtype) { lsize = lwidth * lheight * 3; pImage = new BYTE[lsize]; if(NULL == pImage) return FALSE; CFormatConvertArithmetic::I420ToRGB24(pBuffer, pImage, lwidth, lheight); lBitCount = 24; } // MEDIASUBTYPE_RGB24 or MEDIASUBTYPE_RGB32 else { lsize = BufferLen; pImage = new BYTE[lsize]; memcpy(pImage, pBuffer, lsize); if(MEDIASUBTYPE_RGB24 == m_mtStillMediaType.subtype) lBitCount = 24; else lBitCount = 32; } BITMAPFILEHEADER hdr; hdr.bfType = MAKEWORD('B', 'M'); // always is "BM" hdr.bfSize = lsize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; hdr.bfOffBits = (DWORD) (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)); BITMAPINFOHEADER bih; ZeroMemory(&bih, sizeof(bih)); bih.biSize = sizeof(BITMAPINFOHEADER); bih.biWidth = lwidth; bih.biHeight = lheight; bih.biBitCount = (unsigned short)lBitCount; bih.biPlanes = 1; bih.biCompression = BI_RGB; bih.biSizeImage = 0; // 初始化Bitmap g_lpBits = pImage; g_fhdr = hdr; g_ihdr = bih; } else if (MEDIASUBTYPE_MJPG == m_mtStillMediaType.subtype) { CPicConver PicConver; CMemFile bmpFile; CMemFile jpgFile; long lBmpBufferLen = 0; jpgFile.Attach(pBuffer, BufferLen); PicConver.MBmpToMImage(jpgFile, bmpFile, _T("bmp")); jpgFile.Detach(); // JPG转换后的BMP图像文件的长度(此长度包含了BITMAPINFOHEADER以及图像数据) lBmpBufferLen = (long)bmpFile.GetLength(); BYTE *pbyteBmp = NULL; pbyteBmp = new BYTE[lBmpBufferLen]; if (NULL == pbyteBmp) { return FALSE; } BYTE *pbyteBuffer = bmpFile.Detach(); memcpy(pbyteBmp, pbyteBuffer, lBmpBufferLen); // MSDN中CMemFile自动分配的Buffer最后在析构函数中会被自动释放, // 在实际测试中发现, CMemFile自动分配的Buffer, 但是如果调用了 // Detach方法, 那么CMemFile不会自动释放原先分配的Buffer, 必需 // 手动释放, 否则会出现内存泄露 SAFEDELETEARRAY(pbyteBuffer); // 获得BITMAPFILEHEADER g_fhdr = *((BITMAPFILEHEADER *)pbyteBmp); // 获得BITMAPINFOHEADER g_ihdr = *((BITMAPINFOHEADER *)(pbyteBmp+sizeof(BITMAPFILEHEADER))); // 获得除BITMAPFILEHEADER, BITMAPINFOHEADER之外的图像数据长度 long lDestLen = lBmpBufferLen - sizeof(BITMAPFILEHEADER) - sizeof(BITMAPINFOHEADER); pImage = NULL; pImage = new BYTE[lDestLen]; if (NULL == pImage) { SAFEDELETEARRAY(pbyteBmp); return FALSE; } // 获得图像数据 memcpy(pImage, pbyteBmp+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER), lDestLen); g_lpBits = pImage; // 释放临时转换Buffer SAFEDELETEARRAY(pbyteBmp); } // RAW else { lsize = lwidth * lheight * 3; pImage = new BYTE[lsize]; if (NULL == pImage) { return FALSE; } m_FmtConvAtc.RAW8ToRGB24(pBuffer, pImage, lwidth, lheight); lBitCount = 24; BITMAPFILEHEADER hdr; hdr.bfType = MAKEWORD('B', 'M'); // always is "BM" hdr.bfSize = lsize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; hdr.bfOffBits = (DWORD) (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)); BITMAPINFOHEADER bih; ZeroMemory(&bih, sizeof(bih)); bih.biSize = sizeof(BITMAPINFOHEADER); bih.biWidth = lwidth; bih.biHeight = lheight; bih.biBitCount = (unsigned short)lBitCount; bih.biPlanes = 1; bih.biCompression = BI_RGB; bih.biSizeImage = 0; // 初始化Bitmap g_lpBits = pImage; g_fhdr = hdr; g_ihdr = bih; } }
HRESULT CTCPropBagOnRegKey::_WriteVariant(CRegKey& key, const _bstr_t& strValueName, VARIANT* pVar) { // Check for an array if (V_ISARRAY(pVar)) return WriteSafeArray(key, strValueName, pVar); // Write the value to the registry based on the VARIANT type switch (V_VT(pVar)) { // Empty variant is written as nothing case VT_EMPTY: key.DeleteValue(strValueName); key.RecurseDeleteKey(strValueName); break; // Integer types are written as a REG_DWORD value case VT_I1: case VT_I2: case VT_I4: case VT_UI1: case VT_UI2: case VT_ERROR: { // Coerce the value to a VT_UI4 VariantChangeTypeEx(pVar, pVar, GetThreadLocale(), m_wChangeTypeFlags, VT_UI4); // Fall thru to next case } case VT_UI4: { // Write the REG_DWORD value to the registry key.RecurseDeleteKey(strValueName); key.WriteDWord(strValueName, V_UI4(pVar)); break; } // BOOL's, float types and strings are written as a REG_SZ value case VT_R4: case VT_R8: case VT_CY: case VT_DATE: case VT_DECIMAL: case VT_BOOL: { // Coerce the value to a VT_BSTR VariantChangeTypeEx(pVar, pVar, GetThreadLocale(), m_wChangeTypeFlags, VT_BSTR); // Fall thru to next case } case VT_BSTR: { // Write the REG_SZ value to the registry key.RecurseDeleteKey(strValueName); key.WriteString(strValueName, V_BSTR(pVar)); break; } // Objects written as REG_BINARY, if they don't support IPersistPropertyBag case VT_UNKNOWN: case VT_DISPATCH: { // Attempt first to save the object property using IPersistPropertyBag key.DeleteValue(strValueName); key.RecurseDeleteKey(strValueName); CComObjectStack<CTCPropBagOnRegKey> bag; bag.Init(key, _bstr_t(), this); HRESULT hr = bag.SaveObject(strValueName, V_UNKNOWN(pVar), FALSE, TRUE); if (FAILED(hr)) { TRACE1("CTCPropBagOnRegKey::_WriteVariant: Saving object property \"%s\" as a binary value\n", strValueName); // Create a CArchive on a CMemFile CMemFile file; CArchive ar(&file, CArchive::store, 0); // Archive the variant to the CArchive and close it CComVariant v(pVar); ar << v; ar.Close(); // Write the REG_BINARY value to the registry int cbData = file.GetLength(); BYTE* pData = file.Detach(); key.RecurseDeleteKey(strValueName); key.WriteBinary(strValueName, pData, cbData); file.Attach(pData, cbData); } break; } default: TRACE1("CTCPropBagOnRegKey::_WriteVariant(\"%ls\"): ", strValueName); TRACE2("Unsupported variant type 0x%02X (%d)\n", UINT(V_VT(pVar)), UINT(V_VT(pVar))); return E_FAIL; } // Indicate success return S_OK; }