📄 videoclass.cpp
字号:
// VideoClass.cpp: implementation of the CVideoClass class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "VideoClass.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/***---------------------------------------------------------------------****
在DIB位图上添加文字
****---------------------------------------------------------------------***/
void DibAddText(HDIB &hDib,CString text,CString text2){
if(!hDib){
return ;
}
HDC dcMemory;
HDC pDC=::GetDC(NULL);
dcMemory=CreateCompatibleDC(pDC);
CDC* pMemory=new CDC();
pMemory->Attach(dcMemory);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(pMemory,IMAGEWIDTH,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));
//修改字体
LOGFONT lf;
::ZeroMemory(&lf,sizeof(lf));
lf.lfHeight=120;
lf.lfWeight=FW_BOLD;
// lf.lfItalic=TRUE;
::lstrcpy(lf.lfFaceName,_T("宋体"));
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,BI_RGB,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);
LPVOID pD=GlobalLock(hDib);
if (start<widths )
widths=start;
int heights=sz.cy;
for(int i=sz.cy-1;i>0;i--)
memcpy(LPBYTE(pD)+40+(h-1-i)*start,LPBYTE(pS)+40+(99-i)*IMAGEWIDTH*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,LPBYTE(pS)+40+(99-i)*IMAGEWIDTH*3,widths2);
}
// memcpy(pD,pS,dwDIBSize);
GlobalUnlock(tmpHDIB);
GlobalUnlock(hDib);
GlobalFree(tmpHDIB);
}
/***---------------------------------------------------------------------****
将DIB位图转换为DDB位图
****---------------------------------------------------------------------***/
HBITMAP DIBtoDDB(HDIB hDib)
{
LPBITMAPINFOHEADER lpbi;
HBITMAP hbm;
CPalette pal;
CPalette* pOldPal;
CClientDC dc(NULL);
if (hDib == NULL)
return NULL;
LPVOID aa=GlobalLock(hDib);
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 DDBToDIB(CBitmap &bitmap, DWORD dwCompression, CPalette *pPal)
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HANDLE hDIB;
HANDLE handle;
HDC hDC;
HPALETTE hPal;
ASSERT( bitmap.GetSafeHandle() );
// 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);
// 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 = dwCompression;
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);
// 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);
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
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 绘制到制定的屏幕位置
****---------------------------------------------------------------------***/
void DrawDIB(HDC m_hDC,HDIB hDib,int left,int top,int width,int height)
{
if (!m_hDC)
return;
if (!hDib)
return;
LPSTR lpDIBHdr; // Pointer to BITMAPINFOHEADER
LPSTR lpDIBBits; // Pointer to DIB bits
LPBITMAPINFOHEADER lpbmi; // pointer to a Win 3.0-style DIB
int lWidth,lHeight;
lpDIBHdr=(LPSTR)::GlobalLock((HGLOBAL) hDib);
if (!lpDIBHdr) {
return;
}
// point to the header (whether Win 3.0 and old)
lpbmi=(LPBITMAPINFOHEADER)lpDIBHdr;
lWidth = lpbmi->biWidth;
lHeight = lpbmi->biHeight;
lpDIBBits = lpDIBHdr + *(LPDWORD)lpDIBHdr;
::GlobalUnlock((HGLOBAL) hDib);
int b=SetStretchBltMode(m_hDC, COLORONCOLOR);
int a=StretchDIBits(
m_hDC, left, top,
width, height,
0, 0, lWidth, lHeight,
lpDIBBits,
(BITMAPINFO*) hDib,
DIB_RGB_COLORS,
SRCCOPY );
}
/***---------------------------------------------------------------------****
线程ThreadSave 调用 将DIB图像保存为BMP
****---------------------------------------------------------------------***/
CString SaveBMP(CString DataPath,CString FileName, HDIB hDib)
{
/*
if(hDib)
{
VCE_Errno err;
CString ss=DataPath+FileName;
err=VCEX_saveDIBToFile(hDib,ss.GetBuffer(0));
//GlobalFree(hDib);
//hDib=NULL;
if (err==VCE_success)
return FileName;
}
*/
return "";
}
/***---------------------------------------------------------------------****
线程ThreadSave 调用 将DIB图像保存为JPG
****---------------------------------------------------------------------***/
CString SaveJPG(CString DataPath, CString FileName, HDIB hDib, int nQulity)
{
if(nQulity>0 && nQulity <=100)
{
if(hDib)
{
CString ss=DataPath+FileName;
LPBYTE p=(LPBYTE)::GlobalLock(hDib);
BITMAPFILEHEADER bfh;
memset( &bfh, 0, 14 );
bfh.bfType = 'MB';
bfh.bfSize = GlobalSize(hDib)+14;
bfh.bfOffBits = 54;
// BYTE* buffer=new BYTE[bfh.bfSize];
HDIB hh;
hh=(HDIB)GlobalAlloc(GMEM_FIXED,bfh.bfSize);
LPBYTE dest=(LPBYTE)::GlobalLock(hh);
BOOL bSaveSuccess=FALSE;
try
{
memcpy(dest,&bfh,14);
memcpy(dest+14,p,bfh.bfSize-14);
::GlobalUnlock(hh);
bSaveSuccess=::SaveJPG(ss.GetBuffer(0),HGLOBAL(hh),nQulity);
if(!bSaveSuccess)
{
try
{
DeleteFile(ss.GetBuffer(0));
}
catch (...) {
}
}
// delete[] buffer;
::GlobalFree(hh);
hh=NULL;
}
catch (...) {
// delete[] buffer;
::GlobalUnlock(hh);
::GlobalFree(hh);
hh=NULL;
}
::GlobalUnlock(hDib);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -