Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2341972
  • 博文数量: 816
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-17 17:57
文章分类

全部博文(816)

文章存档

2011年(1)

2008年(815)

分类:

2008-12-17 18:03:13

CaptureImage函数我测试过,工作正常。

//---------------------------------------------------------------------------

#include
#pragma hdrstop

#include "Unit1.h"
#include "jpeg.hpp"
#define CM_ENTIRESCREEN 201   //捕获全屏
#define CM_ACTIVEWINDOW 202   //捕获当前窗口

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}

// 捕获当前屏幕并保存到imagestream中
void CaptureImage(int options, int level, int cq, TMemoryStream* imgstream)
{
LONG width,height;
RECT capRect;
HDC DesktopDC;
switch (options) {
case CM_ENTIRESCREEN:  // 捕获整个屏幕
// 取得桌面的矩形区域范围
GetWindowRect(GetDesktopWindow(),&capRect);  
break;
case CM_ACTIVEWINDOW:  // 捕获当前窗口
HWND ForegWin;
ForegWin = GetForegroundWindow();  // 取得当前窗口句柄
if (!ForegWin)
ForegWin = GetDesktopWindow();
GetWindowRect(ForegWin,&capRect);  // 取得当前窗口的矩形区域范围
break;
}

DesktopDC = GetDC(GetDesktopWindow());  // 创建内存设备描述表
width = capRect.right - capRect.left;
height = capRect.bottom - capRect.top;
Graphics::TBitmap *bBitmap;  // 定义位图变量
try {
bBitmap = new Graphics::TBitmap(); // 创建位图
bBitmap->Width=width;
bBitmap->Height=height;
if ((level>0)&&(level<8))
bBitmap->PixelFormat = TPixelFormat(level);  // 设定色深
// 拷贝屏幕的指定区域到位图
BitBlt(bBitmap->Canvas->Handle,0,0,width,height,DesktopDC,
capRect.left,capRect.top,SRCCOPY);

if (cq>=0) {
TJPEGImage *jpeg;
try {
jpeg = new TJPEGImage;  // 创建JPEG图象
jpeg->CompressionQuality = cq;  // 设定图象品质
jpeg->Assign(bBitmap);  // 将位图转化为JPEG格式
jpeg->SaveToStream(imgstream);  // 保存JPEG图象信息
}
__finally {
delete jpeg;  // 释放资源
}
}
else {
bBitmap->SaveToStream(imgstream);  // 保存位图信息
}
}
__finally {
delete bBitmap;  // 释放资源
}
}
//---------------------------------------------------------------------------


void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
        TMemoryStream * imagstream;
        imagstream=new TMemoryStream;
        CaptureImage(CM_ENTIRESCREEN, 7, 3, imagstream);

        Image1->Picture->Bitmap->LoadFromStream(imagstream) ;
          //Image1->Picture->Bitmap->SaveToFile("c:\\a.bmp");
        delete imagstream;

}

--------------------next---------------------

阅读(1108) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~