0X20.cpp
上传用户:hyz2004817
上传日期:2022-03-30
资源大小:226k
文件大小:5k
- #include "stdafx.h"
- extern BOOL g_bMustExit;
- BOOL
- WINAPI
- ResponseJPGImage(
- SOCKET s,
- HANDLE hEventArray[2],
- int x,
- int y,
- int w,
- int h,
- int zw,
- int zh,
- int iQuality
- )
- {
-
- HDC hdcScreen = CreateDC( "DISPLAY", NULL, NULL, NULL );
- if( hdcScreen == NULL )
- return FALSE;
- BYTE * pRGBBuffer = NULL;
-
- BITMAPINFOHEADER bmih;
- DWORD dwBI = sizeof(BITMAPINFOHEADER);
- HBITMAP hbm = // 24;JPEG 压缩要求 24 位色深
- GetDCPixel_BMP(hdcScreen, x, y, w, h, zw, zh, 24, (void**)&pRGBBuffer, &bmih, &dwBI );
-
- if( hbm == NULL )
- {
- DeleteDC( hdcScreen );
- return FALSE;
- }
- BYTE * pJPEGBuffer;
- DWORD dwJPEGBufferSize;
- // 将 24 位 WINDOWS DIB 像素值编码进一个 JPEG 缓冲,
- // 缓冲内存由 EncodeToJPEGBuffer 分配, 由调用者释放.
- if( FALSE == EncodeToJPEGBuffer( pRGBBuffer, bmih.biWidth, bmih.biHeight,
- &pJPEGBuffer, &dwJPEGBufferSize, iQuality ) )
- {
- DeleteDC( hdcScreen );
- DeleteObject( hbm );
- return FALSE;
- }
- BOOL bExit = FALSE;
- RCARESPONSEHEADER rresh;
- // 初始化一个 "200 OK"应答头
- InitResponseHead( &rresh, RCA_RESPONSE_200_OK, sizeof(RCARESPONSEHEADER)+dwJPEGBufferSize );
-
- // 发送应答头
- if( FALSE == RCASend_EventSelectIO( s, hEventArray, (char*)&rresh,
- sizeof(RCARESPONSEHEADER)) )
- {
- DeleteDC( hdcScreen );
- DeleteObject( hbm );
- delete[] pJPEGBuffer;
- return FALSE;
- }
- // 发送编码后的 JPEG 数据
- if( FALSE == RCASend_EventSelectIO( s, hEventArray, (char*)pJPEGBuffer,
- dwJPEGBufferSize) )
- {
- DeleteDC( hdcScreen );
- DeleteObject( hbm );
- delete[] pJPEGBuffer;
- return FALSE;
- }
- // clean up resources
- DeleteDC( hdcScreen );
- DeleteObject( hbm );
- delete[] pJPEGBuffer;
- return TRUE;
- }
- BOOL
- WINAPI
- ResponseBMPImage(
- SOCKET s,
- HANDLE hEventArray[2],
- int x,
- int y,
- int w,
- int h,
- int zw,
- int zh,
- WORD wBitCount
- )
- {
-
- HDC hdc = CreateDC( "DISPLAY", NULL, NULL, NULL );
-
- if( hdc == NULL )
- return FALSE;
-
- DWORD dwBMPInfoSize;
- if( wBitCount <= 8 )
- dwBMPInfoSize = sizeof(RGBQUAD) * 256 + sizeof(BITMAPINFOHEADER);
- else
- dwBMPInfoSize = sizeof(BITMAPINFOHEADER);
- // allocate memory for BITMAPINFOHEADER struct
- BYTE * ptmp = new BYTE[dwBMPInfoSize];
- if( ptmp == NULL )
- {
- DeleteDC( hdc );
- return FALSE;
- }
-
- PBITMAPINFOHEADER pbmih = (PBITMAPINFOHEADER)ptmp;
- PVOID pBits;
- // 获取请求 DC 矩形的象素值和 BITMAPINFOHEADER 结构
- HBITMAP hbm = GetDCPixel_BMP( hdc, x, y, w, h, zw, zh, dwBMPInfoSize==40 ? 24 : 8,
- &pBits, pbmih, &dwBMPInfoSize );
- if( hbm == NULL )
- {
- DeleteDC( hdc );
- delete[] ptmp;
- return FALSE;
- }
- RCARESPONSEHEADER rresh;
- DIBSECTION ds;
- //DWORD dwTotalBytes;
- GetObject( hbm, sizeof(ds), &ds );
-
- BITMAPFILEHEADER bmfh;
- bmfh.bfType = 0x404D;
- bmfh.bfOffBits = sizeof(bmfh) + dwBMPInfoSize;
- bmfh.bfSize = sizeof(bmfh) + dwBMPInfoSize + ds.dsBmih.biSizeImage;
- bmfh.bfReserved1 = 0;
- bmfh.bfReserved2 = 0;
-
- InitResponseHead( &rresh, RCA_RESPONSE_200_OK, sizeof(rresh)+bmfh.bfSize);
-
-
- // 首先发送 RCA response header
- BOOL bExit = FALSE;
- INT ret;
-
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)&rresh,sizeof(rresh));
- if( ret != sizeof(rresh) )
- {
- bExit = TRUE;
- goto ResponseBMPImage_end;
- }
-
- // 然后发送 BITMAP file header
-
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)&bmfh, sizeof(bmfh));
- if( ret != sizeof(bmfh))
- {
- bExit = TRUE;
- goto ResponseBMPImage_end;
- }
-
-
- // 这里发送 BITMAP information header
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)pbmih, dwBMPInfoSize);
- if( ret != dwBMPInfoSize)
- {
- bExit = TRUE;
- goto ResponseBMPImage_end;
- }
-
- // 最后发送像素数据
-
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)pBits, ds.dsBmih.biSizeImage);
- if( ret != ds.dsBmih.biSizeImage )
- {
- bExit = TRUE;
- goto ResponseBMPImage_end;
- }
-
- ResponseBMPImage_end: // clean up resource
-
- DeleteDC( hdc );
- delete[] ptmp;
- DeleteObject( hbm );
- return bExit;
- }
- ////////////////////////////////////////////////////////////
- //
- // 截屏
- //
- // ID: 0x20
- //
- BOOL
- WINAPI
- RCAID_0X20_HANDLER(
- SOCKET s,
- RCAREQUESTHANDLEDATA * pData,
- RCAREQUESTHEADER * pRCAHead,
- HANDLE hEventArray[2]
- )
- {
-
- DWORD c = pRCAHead->requestBytes - sizeof(RCAREQUESTHEADER); // 计算需要接收的总字节数
- char * buf; // 数据接收缓冲
- int ret; // 保存返回值
- int i = c; // 还剩多少字节需要接收
- int left = 0; // 接收数据偏移量
- buf = (char*)new char[c];
-
- if( buf == NULL )
- return FALSE;
-
- ret = RCARecv_EventSelectIO( s, hEventArray, buf, c );
-
- if( ret != c )
- {
- delete[] buf;
- return FALSE;
- }
- // 成功的后的操作
- PRCAGETIMAGEDATA p;
- BOOL b;
- p = (PRCAGETIMAGEDATA)buf;
- if( p->dwImageFormat == IMAGE_FORMAT_BMP ) // 请求的是 BMP 格式
- {
- WORD w;
- if( p->iBitCount <= 8 )
- w = 8;
- else if ( p->iBitCount <=16 )
- w = 16;
- else if( p->iBitCount <=24 )
- w = 24;
- else
- w = 32;
-
- b = ResponseBMPImage( s, hEventArray,
- p->x, p->y, p->w, p->h, p->zw, p->zh, w );
- } else
- {
-
- if( p->dwImageFormat == IMAGE_FORMAT_JPG )
- {
- // p->dwFlagEx1 是一个特定图象的附加数据, 这里描述 JPEG 图象的质量
- b = ResponseJPGImage( s, hEventArray,
- p->x, p->y, p->w, p->h, p->zw, p->zh, p->dwFlagEx1 );
- }
- }
-
- delete[] buf;
- return TRUE;
- }