怎样得到一HBITMAP变量的第n行的图像数据?(最好能直接读取变量在内存中的数据,不用API) help you to up one up... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //// Function : LoadBitmap()// Purpose : Load the bitmap and translate to the GAPI recoginzlia memory buffer...//BOOL CCEBitmap::LoadBitmap( CCEDraw* pCEDraw, LPCTSTR lpszBitmapName ){ HBITMAP hBitmap; BITMAP Bitmap; // Free the buffer, if it exist... if( m_pBitmapBuffer ) free( m_pBitmapBuffer ); // If the lpszBitmapName == NULL, that means we create a screen bitmap... if( NULL == lpszBitmapName ) { m_sizeBitmap.cx = pCEDraw->GetDisplayProperties().cxWidth; m_sizeBitmap.cy = pCEDraw->GetDisplayProperties().cyHeight; m_pBitmapBuffer = (unsigned char*)malloc( m_sizeBitmap.cx * m_sizeBitmap.cy * pCEDraw->GetDisplayProperties().cBPP / 8 ); memcpy( m_pBitmapBuffer, pCEDraw->GetBuffer(), m_sizeBitmap.cx * m_sizeBitmap.cy * pCEDraw->GetDisplayProperties().cBPP / 8 ); m_nType = CEB_TYPE_SCREENBUFFER; return TRUE; } // First to load the bitmap from file... hBitmap = (HBITMAP) LoadFile( lpszBitmapName ); //hBitmap =(HBITMAP)::LoadBitmap( GetModuleHandle( NULL ), MAKEINTRESOURCE(IDB_BITMAP1)); if( hBitmap == NULL ) { // Failed to load the bitmap... return FALSE; } GetObject( hBitmap, sizeof(BITMAP), &Bitmap ); // Creating new bitmap and receive pointer to it's bits. HBITMAP hTargetBitmap; unsigned char *pBuffer; // Initilize DIBINFO structure BITMAPINFO dibInfo; dibInfo.bmiHeader.biBitCount = 24; dibInfo.bmiHeader.biClrImportant = 0; dibInfo.bmiHeader.biClrUsed = 0; dibInfo.bmiHeader.biCompression = 0; dibInfo.bmiHeader.biHeight = Bitmap.bmHeight; dibInfo.bmiHeader.biPlanes = 1; dibInfo.bmiHeader.biSize = sizeof( BITMAPINFOHEADER ); dibInfo.bmiHeader.biSizeImage = Bitmap.bmWidth*Bitmap.bmHeight*3; dibInfo.bmiHeader.biWidth = Bitmap.bmWidth; dibInfo.bmiHeader.biXPelsPerMeter = 3780; dibInfo.bmiHeader.biYPelsPerMeter = 3780; dibInfo.bmiColors[0].rgbBlue = 0; dibInfo.bmiColors[0].rgbGreen = 0; dibInfo.bmiColors[0].rgbRed = 0; dibInfo.bmiColors[0].rgbReserved = 0; // Create bitmap and receive pointer to points into pBuffer HDC hDC = ::GetDC(NULL); ASSERT(hDC); hTargetBitmap = CreateDIBSection( hDC, (const BITMAPINFO*)&dibInfo, DIB_RGB_COLORS, (void**)&pBuffer, NULL, 0 ); ::ReleaseDC(NULL, hDC); // Copy source bitmap into the target bitmap. // Create 2 device contexts HDC memDc; if ( !( memDc = CreateCompatibleDC(NULL) ) ) { DeleteObject( hBitmap ); DeleteObject( hTargetBitmap ); return FALSE; } HDC targetDc; if ( !( targetDc = CreateCompatibleDC(NULL) ) ) { DeleteDC( memDc ); DeleteObject( hBitmap ); DeleteObject( hTargetBitmap ); return FALSE; } // Select source bitmap into one DC, target into another HBITMAP hOldBitmap1 = (HBITMAP)::SelectObject( memDc, hBitmap); HBITMAP hOldBitmap2 = (HBITMAP)::SelectObject( targetDc, hTargetBitmap ); // Copy source bitmap into the target one ::BitBlt( targetDc, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight, memDc, 0, 0, SRCCOPY ); // Create my own bitmap buffer use the GAPI recognized style... // Free the memory if it exist... int nX, nY; unsigned short nColor; int nBitmapYPitch = ((Bitmap.bmWidth*3)+3) & 0xfffc; BOOL bReturn = TRUE; if( !m_pBitmapBuffer ) free( m_pBitmapBuffer ); long cByte = pCEDraw->GetDisplayProperties().cBPP / 8; m_sizeBitmap.cx = Bitmap.bmWidth; m_sizeBitmap.cy = Bitmap.bmHeight; m_pBitmapBuffer = (unsigned char*)malloc( Bitmap.bmWidth * Bitmap.bmHeight * cByte ); if( !m_pBitmapBuffer ) { bReturn = FALSE; goto Exit; } /* Warning : Does not support 8 byte color model in here */ for( nY = 0; nY < Bitmap.bmHeight ; nY ++ ) { for( nX = 0; nX < Bitmap.bmWidth ; nX ++ ) { nColor = pCEDraw->GetBitmapPointColor( ( pBuffer + ( nX ) * 3 + ( Bitmap.bmHeight - nY - 1 ) * nBitmapYPitch ) ); if( cByte == 2 ) *(unsigned short*)( m_pBitmapBuffer + ( nX + nY * Bitmap.bmWidth ) * cByte ) = nColor; else *( m_pBitmapBuffer + ( nX + nY * Bitmap.bmWidth ) * cByte ) = (BYTE)nColor; } } // Restore device contextsExit: ::SelectObject( memDc, hOldBitmap1 ); ::SelectObject( targetDc, hOldBitmap2 ); DeleteDC( memDc ); DeleteDC( targetDc ); DeleteObject( hBitmap ); DeleteObject( hTargetBitmap ); return bReturn;} Sorry,代码比较乱,看不懂得化问我,主要是用CreateDIBSection,然后用2个device contex 我觉得这样程序的运行效率不太高吧,有直接得到HBITMAP数据在内存的地址的方法吗? 这可是微软工程师告诉我的方法了,应为hbitmap在windows中算是系统的部分,我当时就是想查hbitmap是否是一结构,但是查来查去都没查出来,只有问微软的人,结果就是我写的上面的代码了:) 用ExportAsFixedFormat方法将PPT转为PDF的问题 关于提取图像的RGB数据 对话框显示不出来 ,麻烦大家看看哪出问题了 请问大家一个问题:关于发送垃圾邮件的(纯属技术研究) &操作符何时为取地址?何时表示引用? 请问如何把HBITMAP × 转化为HBITMAP 写文件结束标志的问题 RasGetConnectionStatistics使用问题 论坛这么多人,其中有多少是靠VC吃饭的? 在ATLService中如何使用MFC CSocket? 要订制一个AppWizard,出错!高手请进!!!!!! 如何根据project文件判断project的类型?
// Function : LoadBitmap()
// Purpose : Load the bitmap and translate to the GAPI recoginzlia memory buffer...
//
BOOL CCEBitmap::LoadBitmap( CCEDraw* pCEDraw, LPCTSTR lpszBitmapName )
{
HBITMAP hBitmap;
BITMAP Bitmap; // Free the buffer, if it exist...
if( m_pBitmapBuffer ) free( m_pBitmapBuffer ); // If the lpszBitmapName == NULL, that means we create a screen bitmap...
if( NULL == lpszBitmapName )
{
m_sizeBitmap.cx = pCEDraw->GetDisplayProperties().cxWidth;
m_sizeBitmap.cy = pCEDraw->GetDisplayProperties().cyHeight;
m_pBitmapBuffer = (unsigned char*)malloc( m_sizeBitmap.cx * m_sizeBitmap.cy * pCEDraw->GetDisplayProperties().cBPP / 8 );
memcpy( m_pBitmapBuffer, pCEDraw->GetBuffer(), m_sizeBitmap.cx * m_sizeBitmap.cy * pCEDraw->GetDisplayProperties().cBPP / 8 );
m_nType = CEB_TYPE_SCREENBUFFER;
return TRUE;
} // First to load the bitmap from file...
hBitmap = (HBITMAP) LoadFile( lpszBitmapName );
//hBitmap =(HBITMAP)::LoadBitmap( GetModuleHandle( NULL ), MAKEINTRESOURCE(IDB_BITMAP1));
if( hBitmap == NULL )
{
// Failed to load the bitmap...
return FALSE;
}
GetObject( hBitmap, sizeof(BITMAP), &Bitmap );
// Creating new bitmap and receive pointer to it's bits.
HBITMAP hTargetBitmap;
unsigned char *pBuffer; // Initilize DIBINFO structure
BITMAPINFO dibInfo;
dibInfo.bmiHeader.biBitCount = 24;
dibInfo.bmiHeader.biClrImportant = 0;
dibInfo.bmiHeader.biClrUsed = 0;
dibInfo.bmiHeader.biCompression = 0;
dibInfo.bmiHeader.biHeight = Bitmap.bmHeight;
dibInfo.bmiHeader.biPlanes = 1;
dibInfo.bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
dibInfo.bmiHeader.biSizeImage = Bitmap.bmWidth*Bitmap.bmHeight*3;
dibInfo.bmiHeader.biWidth = Bitmap.bmWidth;
dibInfo.bmiHeader.biXPelsPerMeter = 3780;
dibInfo.bmiHeader.biYPelsPerMeter = 3780;
dibInfo.bmiColors[0].rgbBlue = 0;
dibInfo.bmiColors[0].rgbGreen = 0;
dibInfo.bmiColors[0].rgbRed = 0;
dibInfo.bmiColors[0].rgbReserved = 0; // Create bitmap and receive pointer to points into pBuffer
HDC hDC = ::GetDC(NULL);
ASSERT(hDC);
hTargetBitmap = CreateDIBSection( hDC,
(const BITMAPINFO*)&dibInfo,
DIB_RGB_COLORS,
(void**)&pBuffer,
NULL,
0
); ::ReleaseDC(NULL, hDC); // Copy source bitmap into the target bitmap. // Create 2 device contexts
HDC memDc;
if ( !( memDc = CreateCompatibleDC(NULL) ) )
{
DeleteObject( hBitmap );
DeleteObject( hTargetBitmap );
return FALSE;
} HDC targetDc;
if ( !( targetDc = CreateCompatibleDC(NULL) ) )
{
DeleteDC( memDc );
DeleteObject( hBitmap );
DeleteObject( hTargetBitmap );
return FALSE;
} // Select source bitmap into one DC, target into another
HBITMAP hOldBitmap1 = (HBITMAP)::SelectObject( memDc, hBitmap);
HBITMAP hOldBitmap2 = (HBITMAP)::SelectObject( targetDc, hTargetBitmap ); // Copy source bitmap into the target one
::BitBlt( targetDc, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight, memDc, 0, 0, SRCCOPY ); // Create my own bitmap buffer use the GAPI recognized style...
// Free the memory if it exist... int nX, nY;
unsigned short nColor;
int nBitmapYPitch = ((Bitmap.bmWidth*3)+3) & 0xfffc; BOOL bReturn = TRUE;
if( !m_pBitmapBuffer ) free( m_pBitmapBuffer ); long cByte = pCEDraw->GetDisplayProperties().cBPP / 8;
m_sizeBitmap.cx = Bitmap.bmWidth;
m_sizeBitmap.cy = Bitmap.bmHeight;
m_pBitmapBuffer = (unsigned char*)malloc( Bitmap.bmWidth * Bitmap.bmHeight * cByte );
if( !m_pBitmapBuffer )
{
bReturn = FALSE;
goto Exit;
} /* Warning : Does not support 8 byte color model in here */
for( nY = 0; nY < Bitmap.bmHeight ; nY ++ )
{
for( nX = 0; nX < Bitmap.bmWidth ; nX ++ )
{ nColor = pCEDraw->GetBitmapPointColor( ( pBuffer + ( nX ) * 3 +
( Bitmap.bmHeight - nY - 1 ) *
nBitmapYPitch ) );
if( cByte == 2 )
*(unsigned short*)( m_pBitmapBuffer + ( nX + nY * Bitmap.bmWidth ) * cByte ) = nColor;
else *( m_pBitmapBuffer + ( nX + nY * Bitmap.bmWidth ) * cByte ) = (BYTE)nColor;
}
} // Restore device contexts
Exit:
::SelectObject( memDc, hOldBitmap1 );
::SelectObject( targetDc, hOldBitmap2 );
DeleteDC( memDc );
DeleteDC( targetDc );
DeleteObject( hBitmap );
DeleteObject( hTargetBitmap );
return bReturn;
}