directdraw direct3d 保存位图

6 篇文章 0 订阅
4 篇文章 0 订阅

直接上代码

// 功能:将一个DirectDraw表面,存为一张24位BMP位图 (传入主表面即截屏)
// 输入:表面指针,输出的文件名
// 输出:是否成功
bool SaveToBitmapFile(LPDIRECTDRAWSURFACE lpSurface, char* filename)
{
WORD* lpBuffer; // 表面指针
int nPitch; // 表面跨距
int nWidth, nHeight; // 表面宽高

// 打开文件s
FILE* fp;
if( (fp=fopen(filename, "wb ")) != NULL )
{
// 锁定表面
DDSURFACEDESC ddsd;
ddsd.dwSize = sizeof(ddsd);
HRESULT ddrval = lpSurface-> Lock( NULL, &ddsd, DDLOCK_WAIT, NULL );
if( ddrval == DD_OK )
{
lpBuffer = (WORD *)ddsd.lpSurface;
nWidth = ddsd.dwWidth;
nHeight = ddsd.dwHeight;
nPitch = ddsd.lPitch > > 1; //lPitch以Byte为单位,GraphPitch以WORD为单位。所以GraphPitch = lPitch / 2;
}

// 保存文件头
BITMAPFILEHEADER FileHeader;
FileHeader.bfType = 'BM ';
FileHeader.bfSize = nWidth * nHeight * 3 + 0x36;
FileHeader.bfReserved1 = 0;
FileHeader.bfReserved2 = 0;
FileHeader.bfOffBits = 0x36;
fwrite(&FileHeader, sizeof(BITMAPFILEHEADER), 1, fp);

// 保存文件信息
BITMAPINFOHEADER Header;
Header.biSize = sizeof(BITMAPINFOHEADER); // 结构的大小
Header.biWidth = nWidth; // 宽
Header.biHeight = nHeight; // 高
Header.biPlanes = 1; // 固定
Header.biBitCount = 24; // 颜色数
Header.biCompression = BI_RGB; // 是否压缩
Header.biSizeImage = nWidth * nHeight * 3; // 图片的大小
Header.biXPelsPerMeter = 0;
Header.biYPelsPerMeter = 0;
Header.biClrUsed = 0;
Header.biClrImportant = 0;
fwrite(&Header, Header.biSize, 1, fp);

// 写入具体内容(从下向上存放)
fseek(fp, 0x36, SEEK_SET);
WORD word;
lpBuffer += nWidth * (nHeight - 1);
for(int i=0; i <nHeight; i++)
{
for(int j=0; j <nWidth; j++)
{
word = *lpBuffer;
fputc( GetBlue( word ), fp); // 蓝
fputc( GetGreen( word ), fp); // 绿
fputc( GetRed( word ), fp); // 红
lpBuffer++;
}
lpBuffer -= nPitch*2; // 指针转到上一行的开始
}

fclose(fp);

// 解锁表面
ddrval = lpSurface-> Unlock( NULL );
return true;
}

return false;
}

inline unsigned char GetRed(WORD color)
{
if( Is555 )
return (color> > 7) & 0xff;
else
return (color> > 8) & 0xff;
}

inline unsigned char GetGreen(WORD color)
{
if( Is555 )
return (color> > 2) & 0xff;
else
return (color> > 3) & 0xff;
}

inline unsigned char GetBlue(WORD color)
{
return (color & 0x1f) < < 3;
}

 

 

//direct3d

HBITMAP MyClass::ScreenGrab()
{

//RenderTargetSurface.
IDirect3DSurface9* pRenderTargetSurface = NULL;

//DestinationTargetSurface
IDirect3DSurface9* pDestinationTargetSurface = NULL;

//DisplayMode
D3DDISPLAYMODE d3dDipMode;

//HBITMAP that will be the return of the method
HBITMAP hbm;

if (m_pd3dDevice == NULL)
return NULL;

//Get the client rectangle
RECT rc;
GetClientRect(myhWnd, &rc);
ClientToScreen(myhWnd, LPPOINT(&rc.left));
ClientToScreen(myhWnd, LPPOINT(&rc.right));

//GetRenderTargetSurface.
if(FAILED(m_pd3dDevice->GetRenderTarget(0, &pRenderTargetSurface)))
return NULL;

//Display Mode (d3dDipMode)
if(FAILED(m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3dDipMode)))
return NULL;

//GetDestinationTargetSurface
if(FAILED(m_pd3dDevice->CreateOffscreenPlainSurface((rc.right - rc.left),
(rc.bottom - rc.top),
d3dDipMode.Format,
D3DPOOL_SYSTEMMEM,
&pDestinationTargetSurface,
NULL)))
return NULL;

//copy RenderTargetSurface -> DestTarget
if(FAILED(m_pd3dDevice->GetRenderTargetData(pRenderTargetSurface, pDestinationTargetSurface)))
return NULL;

LPD3DXBUFFER bufferedImage = NULL;

//Save the DestinationTargetSurface into memory (as a bitmap)
if(FAILED(D3DXSaveSurfaceToFileInMemory(&bufferedImage,
D3DXIFF_BMP,
pDestinationTargetSurface,
NULL,
NULL)))
{
return NULL;
}

//TRANSFORMATION REQUIRED HERE!!!!
//I WANT TO TRANSFORM bufferedImage INTO A HBITMAP SO THAT THE METHOD RETURNS
//THAT HBITMAP TO FULFIL MY CONTRACT.

//Release Resources
pRenderTargetSurface->Release();
pDestinationTargetSurface->Release();

//Return HBITMAT

return hbm;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值