大致要求如下:
函数调用成功返回 1失败 返回0
函数参数有如下
1、bmp图片所在位置 char * path
2、截取图片开始位置 POINT Srcpoint
3、截取图片结束位置 POINT Tarpoint
4、图片显示在应用程序上的位置可以使用RECT rect;
函数主要完成对目的图片截取后 显示在应用程序相应的位置。
急求代码! 希望大神能给段代码 要求使用SDK函数编写。
我的编译器是VS2010;
14 个解决方案
#1
其实很简单,我实现的是截桌面图,不过没啥区别的说
#2
能写个函数给我布罗,小弟急需啊!
#3
CaptureDesktop 我刚上传,地址自己找我发布的资源,现在还没审核过
#4
http://download.csdn.net/download/hfz8867879/4784320 仅供参考
#6
能给段函数来研究下不,那个我看看了 一点注释都没有,我是个新手,看着费劲!
#7
我不知道桌面截取是怎么回事,我这边截图是要先加载位图,然后对位图进行截取。这样丢段代码给我,表示压力山大!
#8
这么做的方法我觉得是对的~先截好桌面图,然后根据你鼠标画的RECT,然后再从桌面上截出来RECT
#9
......我这边我是这么做的! 你看看对不?(刚刚自己研究了下)
HDC hdc;
CDC *pDC;
CDC Mcard;
CBitmap m_card;
HBITMAP m_Oldcard;
case WM_LBUTTONDOWN:
hdc=::GetDC(hWnd);
pDC = CDC::FromHandle(hdc);
Mcard.CreateCompatibleDC(pDC);
//m_card.CreateCompatibleBitmap(pDC,100,100);
m_card.LoadBitmapA(IDB_CARD);
m_Oldcard = (HBITMAP)Mcard.SelectObject(m_card);
pDC->BitBlt(100,100,130,260,&Mcard,20,20,SRCCOPY);
::ReleaseDC(hWnd,hdc);
break;
但是一执行LOADBITMAP就报错,运行时报错,实在没看懂是怎么回事? 能不能借阁下经验谈谈是什么原因罗!
#10
BOOL CScreenCapture::GetScreenCapture(WCHAR *path,ULONG inquality)
{
BOOL nRet = FALSE;
//截屏 保存在BYTE * pCapture指针中
CDC *pDC,memDC;//屏幕DC,内存DC
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("FromHandle! "));
pDC = CDC::FromHandle(GetDC(NULL));//获取当前整个屏幕DC
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("FromHandle! "));
if (pDC==NULL)
{
int aa=GetLastError();
WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("pDC NULL LastError %d! "),aa);
return FALSE;
}
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("GetDeviceCaps! "));
int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);//获得颜色模式
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("GetDeviceCaps! "));
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("GetDeviceCaps! "));
int Width = pDC->GetDeviceCaps(HORZRES);
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("GetDeviceCaps! "));
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("GetDeviceCaps! "));
int Height = pDC->GetDeviceCaps(VERTRES);
/* WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("GetDeviceCaps! "));*/
/* WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("CreateCompatibleDC! "));*/
memDC.CreateCompatibleDC(pDC);
/* WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("CreateCompatibleDC! "));*/
CBitmap memBitmap, *oldmemBitmap;
/* WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("CreateCompatibleBitmap! "));*/
memBitmap.CreateCompatibleBitmap(pDC, Width, Height);//建立和屏幕兼容的bitmap
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("CreateCompatibleBitmap! "));
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("SelectObject! "));
oldmemBitmap = memDC.SelectObject(&memBitmap);//将memBitmap选入内存DC
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("SelectObject! "));
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("BitBlt! "));
memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("BitBlt! "));
Bitmap bm((HBITMAP)memBitmap, NULL);//定义bitmap
IStream *stream = NULL;
ULONG quality = inquality; //质量
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("EncoderParameters! "));
EncoderParameters enParameters;
enParameters.Count = 1;
enParameters.Parameter[0].Guid = EncoderQuality;
enParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
enParameters.Parameter[0].NumberOfValues = 1;
enParameters.Parameter[0].Value = &quality;
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("EncoderParameters! "));
//先准备stream
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("CreateStreamOnHGlobal! "));
if(CreateStreamOnHGlobal( NULL, TRUE, &stream ) != S_OK )
{
WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("CreateStreamOnHGlobal Failed! "));
goto Ext;
}
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("CreateStreamOnHGlobal! "));
//查找编码器 这里也可以使用
//用编码器 将bmp保存成jpg的istream流
/// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("Save! "));
if(Ok != bm.Save(stream, &g_pngClsid, &enParameters))
{
WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("bm.Save Failed! "));
goto Ext;
}
/// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("Save! "));
//将流转换成buffer
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("stream_to_mem! "));
if(!stream_to_mem(stream, &m_nSizeJpg))
{
WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("stream_to_mem Failed! "));
goto Ext;
}
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("stream_to_mem! "));
if(m_nSizeJpg <= 0)
goto Ext;
else
{
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("Write! "));
CFile jpgfile;
if(!jpgfile.Open(path,CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate,NULL))
{
// WriteRunLogAlwaysT(g_ExeRunPath+_T("CaptureErrorLog.txt"),_T("Open Failed err:%d"),GetLastError());
nRet = FALSE;
goto Ext;
}
jpgfile.Write(JPG_HEADER,16);
jpgfile.SeekToEnd();
jpgfile.Write((void *)g_buff,m_nSizeJpg);
jpgfile.Close();
nRet = TRUE;
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("Write! "));
}
Ext:
//清理资源
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("Release! "));
if(stream) stream->Release();
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("Release! "));
//SelectObject(memDC,oldmemBitmap);
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("SelectObject! "));
SelectObject(memDC,oldmemBitmap);
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("SelectObject! "));
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("DeleteDC! "));
if(!memDC.DeleteDC())
{
WriteRunLogAlwaysT(g_ExeRunPath+_T("CaptureErrorLog.txt"),_T("memDC Delete err:%d"),GetLastError());
return FALSE;
}
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("DeleteDC! "));
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("DeleteObject! "));
if(!memBitmap.DeleteObject())
{
WriteRunLogAlwaysT(g_ExeRunPath+_T("CaptureErrorLog.txt"),_T("memBitmap delete err:%d"),GetLastError());
return FALSE;
}
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("DeleteObject! "));
::ReleaseDC(NULL, pDC->m_hDC);
return nRet;
}
#11
//流数据转换为buffer malloc出来的 记得销毁
BOOL CScreenCapture::stream_to_mem(IStream *stream, size_t *size)
{
ULARGE_INTEGER ulnSize;
LARGE_INTEGER lnOffset;
lnOffset.QuadPart = 0;
/* get the stream size */
if( stream->Seek( lnOffset, STREAM_SEEK_END, &ulnSize ) != S_OK )
{
return FALSE;
}
if( stream->Seek( lnOffset, STREAM_SEEK_SET, NULL ) != S_OK )
{
return FALSE;
}
/* read it */
*size = (size_t) ulnSize.QuadPart;
if(g_buffsize.QuadPart<ulnSize.QuadPart) //图像需要的内存有变化,以最大一次为准
{
free(g_buff);
g_buff=NULL;
g_buff = malloc((size_t)ulnSize.QuadPart );
// DePrintf("realloc memory oldsize %d,new %d\n!",oldsize,*size);
g_buffsize.QuadPart=ulnSize.QuadPart;
}
// *outbuf = malloc((size_t)ulnSize.QuadPart );
// *size = (size_t) ulnSize.QuadPart;
ULONG bytesRead=0;
HRESULT lret=stream->Read((void *)(g_buff),(ULONG)(*size),&bytesRead);
if (lret!=S_OK)
{
// free(*outbuf);
return FALSE;
}
return TRUE;
}
#12
loadresource那个,看看资源文件在不在再说啊~
#13
非常感谢你这么努力的帮我解答! 我已经解决了! 在我查看完一些教材后,我终于成功了。
不过还是非常感谢你这么热心的帮我!
#14
大哥在SDK下是如何使用CBitmap这些的,加了afxwin.h?我加了这个,但是会出现如下错误:
fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>
但实际上又不能去掉windows.h,因为去掉后会多几个链接错误。。
请问大哥你是怎么编译、链接成功的啊?
fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>
但实际上又不能去掉windows.h,因为去掉后会多几个链接错误。。
请问大哥你是怎么编译、链接成功的啊?
#1
其实很简单,我实现的是截桌面图,不过没啥区别的说
#2
能写个函数给我布罗,小弟急需啊!
#3
CaptureDesktop 我刚上传,地址自己找我发布的资源,现在还没审核过
#4
http://download.csdn.net/download/hfz8867879/4784320 仅供参考
#5
#6
能给段函数来研究下不,那个我看看了 一点注释都没有,我是个新手,看着费劲!
#7
我不知道桌面截取是怎么回事,我这边截图是要先加载位图,然后对位图进行截取。这样丢段代码给我,表示压力山大!
#8
这么做的方法我觉得是对的~先截好桌面图,然后根据你鼠标画的RECT,然后再从桌面上截出来RECT
#9
......我这边我是这么做的! 你看看对不?(刚刚自己研究了下)
HDC hdc;
CDC *pDC;
CDC Mcard;
CBitmap m_card;
HBITMAP m_Oldcard;
case WM_LBUTTONDOWN:
hdc=::GetDC(hWnd);
pDC = CDC::FromHandle(hdc);
Mcard.CreateCompatibleDC(pDC);
//m_card.CreateCompatibleBitmap(pDC,100,100);
m_card.LoadBitmapA(IDB_CARD);
m_Oldcard = (HBITMAP)Mcard.SelectObject(m_card);
pDC->BitBlt(100,100,130,260,&Mcard,20,20,SRCCOPY);
::ReleaseDC(hWnd,hdc);
break;
但是一执行LOADBITMAP就报错,运行时报错,实在没看懂是怎么回事? 能不能借阁下经验谈谈是什么原因罗!
#10
BOOL CScreenCapture::GetScreenCapture(WCHAR *path,ULONG inquality)
{
BOOL nRet = FALSE;
//截屏 保存在BYTE * pCapture指针中
CDC *pDC,memDC;//屏幕DC,内存DC
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("FromHandle! "));
pDC = CDC::FromHandle(GetDC(NULL));//获取当前整个屏幕DC
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("FromHandle! "));
if (pDC==NULL)
{
int aa=GetLastError();
WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("pDC NULL LastError %d! "),aa);
return FALSE;
}
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("GetDeviceCaps! "));
int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);//获得颜色模式
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("GetDeviceCaps! "));
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("GetDeviceCaps! "));
int Width = pDC->GetDeviceCaps(HORZRES);
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("GetDeviceCaps! "));
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("GetDeviceCaps! "));
int Height = pDC->GetDeviceCaps(VERTRES);
/* WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("GetDeviceCaps! "));*/
/* WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("CreateCompatibleDC! "));*/
memDC.CreateCompatibleDC(pDC);
/* WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("CreateCompatibleDC! "));*/
CBitmap memBitmap, *oldmemBitmap;
/* WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("CreateCompatibleBitmap! "));*/
memBitmap.CreateCompatibleBitmap(pDC, Width, Height);//建立和屏幕兼容的bitmap
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("CreateCompatibleBitmap! "));
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("SelectObject! "));
oldmemBitmap = memDC.SelectObject(&memBitmap);//将memBitmap选入内存DC
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("SelectObject! "));
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("BitBlt! "));
memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("BitBlt! "));
Bitmap bm((HBITMAP)memBitmap, NULL);//定义bitmap
IStream *stream = NULL;
ULONG quality = inquality; //质量
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("EncoderParameters! "));
EncoderParameters enParameters;
enParameters.Count = 1;
enParameters.Parameter[0].Guid = EncoderQuality;
enParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
enParameters.Parameter[0].NumberOfValues = 1;
enParameters.Parameter[0].Value = &quality;
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("EncoderParameters! "));
//先准备stream
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("CreateStreamOnHGlobal! "));
if(CreateStreamOnHGlobal( NULL, TRUE, &stream ) != S_OK )
{
WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("CreateStreamOnHGlobal Failed! "));
goto Ext;
}
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("CreateStreamOnHGlobal! "));
//查找编码器 这里也可以使用
//用编码器 将bmp保存成jpg的istream流
/// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("Save! "));
if(Ok != bm.Save(stream, &g_pngClsid, &enParameters))
{
WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("bm.Save Failed! "));
goto Ext;
}
/// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("Save! "));
//将流转换成buffer
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("stream_to_mem! "));
if(!stream_to_mem(stream, &m_nSizeJpg))
{
WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("stream_to_mem Failed! "));
goto Ext;
}
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("stream_to_mem! "));
if(m_nSizeJpg <= 0)
goto Ext;
else
{
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("Write! "));
CFile jpgfile;
if(!jpgfile.Open(path,CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate,NULL))
{
// WriteRunLogAlwaysT(g_ExeRunPath+_T("CaptureErrorLog.txt"),_T("Open Failed err:%d"),GetLastError());
nRet = FALSE;
goto Ext;
}
jpgfile.Write(JPG_HEADER,16);
jpgfile.SeekToEnd();
jpgfile.Write((void *)g_buff,m_nSizeJpg);
jpgfile.Close();
nRet = TRUE;
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("Write! "));
}
Ext:
//清理资源
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("Release! "));
if(stream) stream->Release();
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("Release! "));
//SelectObject(memDC,oldmemBitmap);
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("SelectObject! "));
SelectObject(memDC,oldmemBitmap);
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("SelectObject! "));
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("DeleteDC! "));
if(!memDC.DeleteDC())
{
WriteRunLogAlwaysT(g_ExeRunPath+_T("CaptureErrorLog.txt"),_T("memDC Delete err:%d"),GetLastError());
return FALSE;
}
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("DeleteDC! "));
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("DeleteObject! "));
if(!memBitmap.DeleteObject())
{
WriteRunLogAlwaysT(g_ExeRunPath+_T("CaptureErrorLog.txt"),_T("memBitmap delete err:%d"),GetLastError());
return FALSE;
}
// WriteRunLogAlwaysT(g_ExeRunPath+_T("GetScreenCapture.txt"),_T("DeleteObject! "));
::ReleaseDC(NULL, pDC->m_hDC);
return nRet;
}
#11
//流数据转换为buffer malloc出来的 记得销毁
BOOL CScreenCapture::stream_to_mem(IStream *stream, size_t *size)
{
ULARGE_INTEGER ulnSize;
LARGE_INTEGER lnOffset;
lnOffset.QuadPart = 0;
/* get the stream size */
if( stream->Seek( lnOffset, STREAM_SEEK_END, &ulnSize ) != S_OK )
{
return FALSE;
}
if( stream->Seek( lnOffset, STREAM_SEEK_SET, NULL ) != S_OK )
{
return FALSE;
}
/* read it */
*size = (size_t) ulnSize.QuadPart;
if(g_buffsize.QuadPart<ulnSize.QuadPart) //图像需要的内存有变化,以最大一次为准
{
free(g_buff);
g_buff=NULL;
g_buff = malloc((size_t)ulnSize.QuadPart );
// DePrintf("realloc memory oldsize %d,new %d\n!",oldsize,*size);
g_buffsize.QuadPart=ulnSize.QuadPart;
}
// *outbuf = malloc((size_t)ulnSize.QuadPart );
// *size = (size_t) ulnSize.QuadPart;
ULONG bytesRead=0;
HRESULT lret=stream->Read((void *)(g_buff),(ULONG)(*size),&bytesRead);
if (lret!=S_OK)
{
// free(*outbuf);
return FALSE;
}
return TRUE;
}
#12
loadresource那个,看看资源文件在不在再说啊~
#13
非常感谢你这么努力的帮我解答! 我已经解决了! 在我查看完一些教材后,我终于成功了。
不过还是非常感谢你这么热心的帮我!
#14
大哥在SDK下是如何使用CBitmap这些的,加了afxwin.h?我加了这个,但是会出现如下错误:
fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>
但实际上又不能去掉windows.h,因为去掉后会多几个链接错误。。
请问大哥你是怎么编译、链接成功的啊?
fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>
但实际上又不能去掉windows.h,因为去掉后会多几个链接错误。。
请问大哥你是怎么编译、链接成功的啊?