⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myimage.cpp

📁 电子监控的网络编程实例
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		if( err != IMAGE_OK)
			return err;
		
		memset(&bfh, 0, sizeof(bfh));
		bfh.bfType = 'MB';
		bfh.bfOffBits = 54;
		bfh.bfSize = dwSize + 54;

		p = (PBYTE)::GlobalLock(hImage);
		if(!p)
			return IMAGE_ERR_LOCK;
		memcpy(p, &bfh, sizeof(bfh));
		memcpy(p + 14, &m_bih, sizeof(m_bih));
		memcpy(p + 54, pImage, dwSize);
		b = ::GlobalUnlock(hImage);
		/*win98下禁止使用
		if(!b)
		return IMAGE_ERR_UNLOCK;*/
	}
	return IMAGE_OK;
}

//填充BITMAPINFOHEADER 结构
IMAGE_ERROR CMyImage::SetBih(int width, int height, int bitcount)
{
//	BITMAPINFOHEADER bih;
	memset( &m_bih, 0, sizeof( BITMAPINFOHEADER ) );
	m_bih.biSize = sizeof( BITMAPINFOHEADER );
	m_bih.biWidth = width;
	m_bih.biHeight = height;
	m_bih.biPlanes = 1;
	m_bih.biBitCount = bitcount;
	return IMAGE_OK;
}

//获得BITMAPINFOHEADER结构相关参数
IMAGE_ERROR CMyImage::GetBih(int &width, int &height, int& bitcount)
{
	width = m_bih.biWidth;
	height = m_bih.biHeight;
	bitcount = m_bih.biBitCount;
	return IMAGE_OK;
}

/***---------------------------------------------------------------------****
将DIB位图转换为DDB位图
****---------------------------------------------------------------------***/
HBITMAP CMyImage::DIBtoDDB(HDIB hDib)
{
	LPBITMAPINFOHEADER	lpbi;
	HBITMAP 		hbm;
	CPalette		pal;
	CPalette*		pOldPal;
	CClientDC		dc(NULL);
	
	if (hDib == NULL)
		return NULL;
	LPVOID aa=GlobalLock(hDib); 
	if(!aa)
		return NULL;
	
	lpbi = (LPBITMAPINFOHEADER)aa;//hDib;
	
	int nColors = lpbi->biClrUsed ? lpbi->biClrUsed : 1 << lpbi->biBitCount;
	
	BITMAPINFO &bmInfo = *(LPBITMAPINFO)aa;//hDib ;
	LPVOID lpDIBBits;
	if( bmInfo.bmiHeader.biBitCount > 8 )
		lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors + 
		bmInfo.bmiHeader.biClrUsed) + 
		((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
	else
		lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);
	
	// Create and select a logical palette if needed
	if( nColors <= 256 && dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
	{
		UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
		LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
		
		pLP->palVersion = 0x300;
		pLP->palNumEntries = nColors;
		
		for( int i=0; i < nColors; i++)
		{
			pLP->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed;
			pLP->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen;
			pLP->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;
			pLP->palPalEntry[i].peFlags = 0;
		}
		
		pal.CreatePalette( pLP );
		
		delete[] pLP;
		
		// Select and realize the palette
		pOldPal = dc.SelectPalette( &pal, FALSE );
		dc.RealizePalette();
	}
	
	
	hbm = CreateDIBitmap(dc.GetSafeHdc(),		// handle to device context
		(LPBITMAPINFOHEADER)lpbi,	// pointer to bitmap info header 
		(LONG)CBM_INIT,			// initialization flag
		lpDIBBits,			// pointer to initialization data 
		(LPBITMAPINFO)lpbi,		// pointer to bitmap info
		DIB_RGB_COLORS );		// color-data usage 
	
	if (pal.GetSafeHandle())
		dc.SelectPalette(pOldPal,FALSE);
	GlobalUnlock(hDib);
	//GlobalFree(hDib);
	//DeleteObject(hbm);
	
	return hbm;
}

/*
DDB位图转换为DIB位图
*/
HDIB CMyImage::DDBtoDIB(HBITMAP hBitmap, HPALETTE hPal)
{
	BITMAP			bm;
	BITMAPINFOHEADER	bi;
	LPBITMAPINFOHEADER 	lpbi;
	DWORD			dwLen;
	HANDLE			hDIB;
	HANDLE			handle;
	HDC 			hDC;
//	HPALETTE		hPal;


	if( !hBitmap )
		return NULL;

	// The function has no arg for bitfields
//	if( dwCompression == BI_BITFIELDS )
//		return NULL;

	// If a palette has not been supplied use defaul palette
//	hPal = (HPALETTE) pPal->GetSafeHandle();
	if (hPal==NULL)
		hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);

	// Get bitmap information
	//bitmap.GetObject(sizeof(bm),(LPSTR)&bm);
	if (!::GetObject(hBitmap, sizeof(bm), &bm)) return NULL;
	
	

	// Initialize the bitmapinfoheader
	bi.biSize		= sizeof(BITMAPINFOHEADER);
	bi.biWidth		= bm.bmWidth;
	bi.biHeight 		= bm.bmHeight;
	bi.biPlanes 		= 1;
	bi.biBitCount		= 24;//GetBitCount(m_cfg);//bm.bmPlanes * bm.bmBitsPixel;
	bi.biCompression	= BI_RGB;
	bi.biSizeImage		= 0;
	bi.biXPelsPerMeter	= 0;
	bi.biYPelsPerMeter	= 0;
	bi.biClrUsed		= 0;
	bi.biClrImportant	= 0;

	// Compute the size of the  infoheader and the color table
	int nColors = (1 << bi.biBitCount);
	if( nColors > 256 ) 
		nColors = 0;
	dwLen  = bi.biSize + nColors * sizeof(RGBQUAD);

	// We need a device context to get the DIB from
	hDC = GetDC(NULL);
	hPal = SelectPalette(hDC,hPal,FALSE);
	RealizePalette(hDC);
//	ReleaseDC( NULL, hDC ); 

	// Allocate enough memory to hold bitmapinfoheader and color table
	hDIB = GlobalAlloc(GMEM_FIXED,dwLen);

	if (!hDIB){
		SelectPalette(hDC,hPal,FALSE);
		ReleaseDC(NULL,hDC);
		return NULL;
	}

	lpbi = (LPBITMAPINFOHEADER)hDIB;

	*lpbi = bi;

	// Call GetDIBits with a NULL lpBits param, so the device driver 
	// will calculate the biSizeImage field 
//	GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
//			(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
	GetDIBits(hDC, hBitmap, 0L, (DWORD)bi.biHeight,
				(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);

	bi = *lpbi;

	// If the driver did not fill in the biSizeImage field, then compute it
	// Each scan line of the image is aligned on a DWORD (32bit) boundary
	if (bi.biSizeImage == 0){
		bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8) 
						* bi.biHeight;

		// If a compression scheme is used the result may infact be larger
		// Increase the size to account for this.
//		if (dwCompression != BI_RGB)
//			bi.biSizeImage = (bi.biSizeImage * 3) / 2;
	}

	// Realloc the buffer so that it can hold all the bits
	dwLen += bi.biSizeImage;
	if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
		hDIB = handle;
	else{
		GlobalFree(hDIB);

		// Reselect the original palette
		SelectPalette(hDC,hPal,FALSE);
		ReleaseDC(NULL,hDC);
		return NULL;
	}

	// Get the bitmap bits
	lpbi = (LPBITMAPINFOHEADER)hDIB;

	// FINALLY get the DIB
/*	BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
				0L,				// Start scan line
				(DWORD)bi.biHeight,		// # of scan lines
				(LPBYTE)lpbi 			// address for bitmap bits
				+ (bi.biSize + nColors * sizeof(RGBQUAD)),
				(LPBITMAPINFO)lpbi,		// address of bitmapinfo
				(DWORD)DIB_RGB_COLORS);		// Use RGB for color table
*/
	BOOL bGotBits = GetDIBits( hDC, hBitmap,
		0L,				// Start scan line
		(DWORD)bi.biHeight,		// # of scan lines
		(LPBYTE)lpbi 			// address for bitmap bits
		+ (bi.biSize + nColors * sizeof(RGBQUAD)),
		(LPBITMAPINFO)lpbi,		// address of bitmapinfo
		(DWORD)DIB_RGB_COLORS);		// Use RGB for color table
	
	if( !bGotBits )
	{
		GlobalFree(hDIB);
		
		SelectPalette(hDC,hPal,FALSE);
		ReleaseDC(NULL,hDC);
		return NULL;
	}

	SelectPalette(hDC,hPal,FALSE);
	ReleaseDC(NULL,hDC);

//	DrawDIB(HDIB(hDIB),300,400);
//	bitmap.DeleteObject();
	
	return HDIB(hDIB);
}

HDIB CMyImage::DDBtoDIB(CBitmap *pBitmap, CPalette *pPalette)
{
	if (!pBitmap)
		return NULL;
	if(pPalette)
		return DDBtoDIB( (HBITMAP)(pBitmap->GetSafeHandle()),
					(HPALETTE)(pPalette->GetSafeHandle()) );
	else
		return DDBtoDIB( (HBITMAP)(pBitmap->GetSafeHandle()), NULL );
}
/*
在DIB位图上叠加文字和号牌图片
//修改字体
LOGFONT lf;
::ZeroMemory(&lf,sizeof(lf));
lf.lfHeight=160;
lf.lfWeight=FW_BOLD;
//	lf.lfItalic=TRUE;
::lstrcpy(lf.lfFaceName,_T("宋体"));
*/
void CMyImage::DibAdd(HDIB &hDib, CString text, CString text2, LPLOGFONT plf, HBITMAP hBitmap, int xstart, int ystart)
{
	if(!hDib)
		return;

	LOGFONT lf;
	
	if(!plf)
	{	
		::ZeroMemory(&lf,sizeof(lf));
		lf.lfHeight=160;
		lf.lfWeight=FW_BOLD;
		//	lf.lfItalic=TRUE;
		::lstrcpy(lf.lfFaceName,_T("宋体"));
	}
	else
	{
		memcpy(&lf, plf, sizeof(LOGFONT));
	}

	LPBITMAPINFOHEADER lpbih;
	// 计算位图信息
	lpbih =(LPBITMAPINFOHEADER)::GlobalLock(hDib);
	if( !lpbih )
		return;
	LONG dibWidth = lpbih->biWidth;
	LONG dibHeight = lpbih->biHeight;
	::GlobalUnlock(hDib);
	
	HDC dcMemory;
	HDC pDC=::GetDC(NULL);
	dcMemory=CreateCompatibleDC(pDC);
	CDC* pMemory=new CDC();
	pMemory->Attach(dcMemory);
	CBitmap bitmap;
	bitmap.CreateCompatibleBitmap(pMemory,dibWidth,100);
	CBitmap* pOldBitmap =(CBitmap*)::SelectObject(dcMemory,HBITMAP(bitmap.m_hObject));
	//	::SetBkMode(dcMemory,TRANSPARENT);
	::SetBkColor(dcMemory,RGB(0,0,0));
	::SetTextColor(dcMemory,RGB(255,255,255));
	
	CFont font;
	font.CreatePointFontIndirect(&lf);
	::SelectObject(dcMemory,font.GetSafeHandle());
	CSize sz=pMemory->GetTextExtent(text);
	CSize sz2=pMemory->GetTextExtent(text2);
	DWORD widths=sz.cx*3;
	DWORD widths2=0;
	::TextOut(dcMemory,0,0,text,text.GetLength());
	if(text2.GetLength()>1)
	{
		::TextOut(dcMemory,0,sz.cy,text2,text2.GetLength());
		widths2=sz2.cx*3;
	}
	
	
	pMemory->Detach();
	
	
	//	ReleaseDC(NULL,dcMemory);
	DeleteDC(dcMemory);
	ReleaseDC(NULL,pDC);
	DeleteDC(pMemory->GetSafeHdc());
	//	ReleaseDC(NULL,pMemory->GetSafeHdc());
	
	delete pMemory;
	
	HDIB tmpHDIB = DDBtoDIB(bitmap, NULL);
	
	DWORD dwDIBSize=GlobalSize(hDib);
	
	LPSTR				 lpDIBHdr;  // Pointer to BITMAPINFOHEADER
	LPBITMAPINFOHEADER lpbmi;		// pointer to a Win 3.0-style DIB
	LPBITMAPCOREHEADER lpbmc;		// pointer to an other-style DIB
	int h;
	// point to the header (whether old or Win 3.0
	lpDIBHdr  = (LPSTR) ::GlobalLock((HGLOBAL) hDib);
	lpbmi = (LPBITMAPINFOHEADER)lpDIBHdr;
	lpbmc = (LPBITMAPCOREHEADER)lpDIBHdr;
	// return the DIB height if it is a Win 3.0 DIB 
	if (IS_WIN30_DIB(lpDIBHdr))
		h=lpbmi->biHeight;
	else  // it is an other-style DIB, so return its height 
		h=lpbmc->bcHeight;
	
	::GlobalUnlock((HGLOBAL) hDib);
	DWORD start=dwDIBSize/h;
	
//	GlobalReAlloc(hDib,dwDIBSize,GMEM_MODIFY);
	LPVOID pS=GlobalLock(tmpHDIB);
	if(!pS)
	{
		GlobalFree(tmpHDIB);
		return;	
	}
	LPVOID pD=GlobalLock(hDib);
	if(!pS){
		GlobalUnlock(tmpHDIB);
		GlobalFree(tmpHDIB);
		return;
	}
	if (start<widths )
		widths=start;
	int heights=sz.cy;
	//	if(text2.GetLength()>1)
	//		heights=2*sz.cy;
	//	for(int i=99;i>99-heights;i--)
	//		memcpy(LPBYTE(pD)+40+(h-1-(99-i))*start/*+start-300*3*/,LPBYTE(pS)+40+i*720*3/*+(720-300)*3*/,widths);
	for(int i=sz.cy-1;i>0;i--)
		memcpy(LPBYTE(pD)+40+(h-1-i)*start/*+start-300*3*/,LPBYTE(pS)+40+(99-i)*dibWidth*3/*+(720-300)*3*/,widths);
	if(widths2>0)
	{
		if(start<widths2)
			widths2=start;
		for(int i=sz.cy*2-1;i>sz.cy;i--)
			memcpy(LPBYTE(pD)+40+(h-1-i)*start/*+start-300*3*/,LPBYTE(pS)+40+(99-i)*dibWidth*3/*+(720-300)*3*/,widths2);
	}
	
	if(hBitmap)
	{
	
	//绘制号牌图片
		HDIB tmpHDIB2 = DDBtoDIB(hBitmap, NULL);
		if(!tmpHDIB2)
		{
			GlobalUnlock(tmpHDIB);
			GlobalUnlock(hDib);
			GlobalFree(tmpHDIB);
			return;
		}
		PBYTE ph = (PBYTE)::GlobalLock(tmpHDIB2);
		if(!ph)
		{
			GlobalUnlock(tmpHDIB);
			GlobalUnlock(hDib);
			GlobalFree(tmpHDIB);
			GlobalFree(tmpHDIB2);
			return;
		}
		int cx=0,cy=0;
		LPBITMAPINFOHEADER lp = (LPBITMAPINFOHEADER)ph;
		cx = lp->biWidth;
		cy = lp->biHeight;
		
		if (cx + xstart>dibWidth)
			cx = dibWidth - xstart;
		if (cy + ystart>dibHeight)
			cy = dibHeight - ystart;
		for(i=0;i<cy;i++)	
			memcpy(LPBYTE(pD)+40+(dibHeight-ystart-cy+i)*start+xstart*3, LPBYTE(ph)+40+ (lp->biHeight-cy+i)*lp->biWidth*3 , cx * 3);  
			//	memcpy(pD,pS,dwDIBSize);
		GlobalUnlock(tmpHDIB2);
		GlobalFree(tmpHDIB2);
	}
	GlobalUnlock(tmpHDIB);
	GlobalUnlock(hDib);
	GlobalFree(tmpHDIB);

}

void CMyImage::ImageAdd(CString text, CString text2, LPLOGFONT plf, HBITMAP hBitmap, int xstart, int ystart)
{
	if(!hImage)
		return;

	LOGFONT lf;
	if(!plf)
	{
		::ZeroMemory(&lf,sizeof(lf));
		lf.lfHeight=160;
		lf.lfWeight=FW_BOLD;
		//	lf.lfItalic=TRUE;
		::lstrcpy(lf.lfFaceName,_T("宋体"));
	}
	else
	{
		memcpy(&lf, plf, sizeof(LOGFONT));
	}

	LPBITMAPINFOHEADER lpbih;
	PBYTE ph2=NULL;
	ph2 = (PBYTE)::GlobalLock(hImage);
	if( !ph2 )
		return;
	// 计算位图信息
	lpbih =(LPBITMAPINFOHEADER)(ph2+14);
	if( !lpbih )
		return;
	LONG dibWidth = lpbih->biWidth;
	LONG dibHeight = lpbih->biHeight;
	::GlobalUnlock(hImage);
	
	HDC dcMemory;
	HDC pDC=::GetDC(NULL);
	dcMemory=CreateCompatibleDC(pDC);
	CDC* pMemory=new CDC();
	pMemory->Attach(dcMemory);
	CBitmap bitmap;
	bitmap.CreateCompatibleBitmap(pMemory,dibWidth,100);
	CBitmap* pOldBitmap =(CBitmap*)::SelectObject(dcMemory,HBITMAP(bitmap.m_hObject));
	//	::SetBkMode(dcMemory,TRANSPARENT);
	::SetBkColor(dcMemory,RGB(0,0,0));
	::SetTextColor(dcMemory,RGB(255,255,255));
	
	CFont font;
	font.CreatePointFontIndirect(&lf);
	::SelectObject(dcMemory,font.GetSafeHandle());
	CSize sz=pMemory->GetTextExtent(text);
	CSize sz2=pMemory->GetTextExtent(text2);
	DWORD widths=sz.cx*3;
	DWORD widths2=0;
	::TextOut(dcMemory,0,0,text,text.GetLength());
	if(text2.GetLength()>1)
	{
		::TextOut(dcMemory,0,sz.cy,text2,text2.GetLength());
		widths2=sz2.cx*3;
	}
	
	
	pMemory->Detach();
	
	
	//	ReleaseDC(NULL,dcMemory);
	DeleteDC(dcMemory);
	ReleaseDC(NULL,pDC);
	DeleteDC(pMemory->GetSafeHdc());
	//	ReleaseDC(NULL,pMemory->GetSafeHdc());
	
	delete pMemory;
	
	HDIB tmpHDIB = DDBtoDIB(bitmap, NULL);
	
	DWORD dwDIBSize=GlobalSize(hImage) - 14;
	
	LPSTR				 lpDIBHdr;  // Pointer to BITMAPINFOHEADER
	LPBITMAPINFOHEADER lpbmi;		// pointer to a Win 3.0-style DIB
	LPBITMAPCOREHEADER lpbmc;		// pointer to an other-style DIB
	int h;
	// point to the header (whether old or Win 3.0
	lpDIBHdr  = (LPSTR) ::GlobalLock(hImage);
	lpDIBHdr = lpDIBHdr + 14;
	lpbmi = (LPBITMAPINFOHEADER)lpDIBHdr;
	lpbmc = (LPBITMAPCOREHEADER)lpDIBHdr;
	// return the DIB height if it is a Win 3.0 DIB 
	if (IS_WIN30_DIB(lpDIBHdr))
		h=lpbmi->biHeight;
	else  // it is an other-style DIB, so return its height 
		h=lpbmc->bcHeight;
	
	::GlobalUnlock(hImage);
	DWORD start=dwDIBSize/h;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -