如何创建特定区域的屏幕截图? [英] How create a screenshot of a particular area?

查看:194
本文介绍了如何创建特定区域的屏幕截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码,它接收到服务器端之前已经定义的特定区域,并在客户端的Form上创建一个孔。而不是这样,我想得到这个相同区域的屏幕截图,但是没有出现我的Form在最终的结果,像一个正常的桌面捕获,但在这种情况下只会被捕获,这个小区域。



所以,我可以如何调整这个我的代码在这里?

 程序TForm1.CS1Read发件人:TObject;套接字:TCustomWinSocket); 
var
X1,X2,Y1,Y2:整数;
列表:TStrings;
FormRegion,HoleRegion:HRGN;
StrCommand:String;
begin
StrCommand:= Socket.ReceiveText;

如果Pos('§',StrCommand)> 0然后
begin
列表:= TStringList.Create;
try
FormRegion:= CreateRectRgn(0,0,Form12.Width,Form12.Height);
ExtractStrings(['§'],[],PChar(StrCommand),List);

X1:= StrToIntDef(List [0],0) - Form12.Left - 2;
Y1:= StrToIntDef(List [1],0) - Form12.Top - 2;
X2:= StrToIntDef(List [2],0) - Form12.Left - 2;
Y2:= StrToIntDef(List [3],0) - Form12.Top - 2;

HoleRegion:= CreateRectRgn(X1,Y1,X2,Y2);
CombineRgn(FormRegion,FormRegion,HoleRegion,RGN_DIFF);
SetWindowRgn(Form12.Handle,FormRegion,True);
finally
List.Free;
结束
结束
结束


解决方案

我没有所有的无关信息,但是我可以告诉你如何将一个区域的内容捕获到位图中。当然,您需要适当调整坐标以适应您的需要。您可能希望看到 GetRgnBox ,以了解如何在组合它们之后获取整个区域的边界矩形。我的例子不这样做,因为我有一个区域。



该示例需要两个TButtons和一个表单上的TImage。我已经在表单中定义了三个组件,并将它们放在代码中,因此不需要包含一个DFM。但是,您需要将组件放在表单上并连接事件处理程序。 : - )



单击Button1将在窗体上创建一个矩形区域,填充一个红色线条和一点文本的网格类型,只需定义哪里区域位于。单击第二个按钮将在位图上绘制该区域内容的副本,并将该位图分配给图像控件。

  unit Unit1 ; 

接口

使用
Winapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,System.Classes,Vcl.Graphics,
Vcl.Controls,Vcl.Forms,Vcl.Dialogs,Vcl.StdCtrls,Vcl.ExtCtrls;

type
TForm1 = class(TForm)
Button1:TButton;
Button2:TButton;
Image1:TImage;
procedure Button1Click(Sender:TObject);
procedure FormCreate(Sender:TObject);
程序Button2Click(发件人:TObject);
private
{私人声明}
//区域协调
R:TRect;
public
{公开声明}
end;

var
Form1:TForm1;

执行

{$ R * .dfm}

//创建区域(孔)
程序TForm1.Button1Click(发件人:TObject);
var
区域:HRGN;
begin
Canvas.TextOut(R.Left + 60,R.Top + 60,'Test text');
Canvas.Brush.Style:= bsCross;
Canvas.Brush.Color:= clRed;
Region:= CreateRectRgn(R.Left,R.Top,R.Right,R.Bottom);
{
注意:通常你想检查上述API调用
的结果,只有当它不为NULL(0)时才会继续。您也想使用
try..finally确保该区域被正确删除。
因为
而忽略a)这段代码被测试正常工作,而
b)这是一个演示应用程序,用于与该区域进行某些操作,
没有其他的。如果创建该区域的呼叫失败,
应用程序是无用的,您将关闭它,这意味着
该区域将被自动销毁。
}
FillRgn(Canvas.Handle,Region,Canvas.Brush.Handle);
DeleteObject(Region);
Button2.Enabled:= True;
结束

//捕获区域(孔)并显示在TImage中。
procedure TForm1.Button2Click(Sender:TObject);
var
Bmp:TBitmap;
begin
Bmp:= TBitmap.Create;
try
Bmp.SetSize(R.Right - R.Left,r.Bottom - R.Top);
Bmp.Canvas.CopyRect(Rect(0,0,Bmp.Width,Bmp.Height),Canvas,R);
Image1.Picture.Assign(Bmp);
finally
Bmp.Free;
结束
结束

//以
procedure TForm1.FormCreate(Sender:TObject)的形式设置区域(孔)的坐标;
begin
R:= Rect(10,40,175,175);
//大小我们稍后将使用的图像来适应矩形。我们设置
//下面的位置。
Image1.Width:= R.Right - R.Left;
Image1.Height:= R.Bottom - R.Top;

Self.Height:= 375;
Self.Width:= 350;
Button1.Left:= 238;
Button1.Top:= 16;
Button2.Left:= 238;
Button2.Top:= 48;
Image1.Left:= 160;
Image1.Top:= 190;
//禁用第二个按钮,直到第一个按钮被点击
Button2.Enabled:= False;
结束

结束。


I have code that receives a specific area already defined before on server side and creates a hole on Form in client side. Instead of this, i want to get a screen capture of this same area but without appears my Form in final result, like a normal desktop capture, but in this case will be only captured, this small area.

So, how i can adapt this my code below for this?

procedure TForm1.CS1Read(Sender: TObject; Socket: TCustomWinSocket);
var
  X1, X2, Y1, Y2: Integer;
  List: TStrings;
  FormRegion, HoleRegion: HRGN;
  StrCommand: String;
begin
  StrCommand := Socket.ReceiveText;

  if Pos('§', StrCommand) > 0 then
  begin
    List := TStringList.Create;
    try
      FormRegion := CreateRectRgn(0, 0, Form12.Width, Form12.Height);
      ExtractStrings(['§'], [], PChar(StrCommand), List);

      X1 := StrToIntDef(List[0], 0) - Form12.Left - 2;
      Y1 := StrToIntDef(List[1], 0) - Form12.Top - 2;
      X2 := StrToIntDef(List[2], 0) - Form12.Left - 2;
      Y2 := StrToIntDef(List[3], 0) - Form12.Top - 2;

      HoleRegion := CreateRectRgn(X1, Y1, X2, Y2);
      CombineRgn(FormRegion, FormRegion, HoleRegion, RGN_DIFF);
      SetWindowRgn(Form12.Handle, FormRegion, True);
    finally
      List.Free;
    end;
  end;
end;

解决方案

I don't have all of your extraneous information, but I can show you how to capture the contents of a region into a bitmap. You'll need to adjust the coordinates appropriately to suit your needs, of course. You may want to see GetRgnBox to see how to get the total region's bounding rectangle after you've combined them. My example doesn't do so, because I have a single region.

The example requires two TButtons and a TImage on a form. I've sized the form and located the three components in code, so that it's not necessary to include a DFM. You will need to drop the components on a form and connect the event handlers, however. :-)

Clicking Button1 will create a rectangular region on the form, fill it with a grid type pattern of red lines and a bit of text, just to define where the region is located. Clicking the second button will draw a copy of that region's content on a bitmap and assign that bitmap to the image control.

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Image1: TImage;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    // Region coords
    R: TRect;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// Create the region (hole)
procedure TForm1.Button1Click(Sender: TObject);
var
  Region: HRGN;
begin
  Canvas.TextOut(R.Left + 60, R.Top + 60, 'Test text');
  Canvas.Brush.Style := bsCross;
  Canvas.Brush.Color := clRed;
  Region := CreateRectRgn(R.Left, R.Top, R.Right, R.Bottom);
  { 
    Note: Normally you'd want to check the result of the above API call
    and only proceed if it's not NULL (0). You'd also want to use a
    try..finally to make sure that the region was deleted properly.
    Omitted here because 
      a) This code was tested to work properly, and
      b) It's a demo app for doing something with the region and
         nothing else. If the call to create the region fails, the
         app is useless, and you'll close it anyway, which means
         the region will be automatically destroyed.
  }
  FillRgn(Canvas.Handle, Region, Canvas.Brush.Handle);
  DeleteObject(Region);
  Button2.Enabled := True;
end;

// Capture the region (hole) and display in the TImage.
procedure TForm1.Button2Click(Sender: TObject);
var
  Bmp: TBitmap;
begin
  Bmp := TBitmap.Create;
  try
    Bmp.SetSize(R.Right - R.Left, r.Bottom - R.Top);
    Bmp.Canvas.CopyRect(Rect(0, 0, Bmp.Width, Bmp.Height), Canvas, R);
    Image1.Picture.Assign(Bmp);
  finally
    Bmp.Free;
  end;
end;

// Set up the coordinates for the region (hole) in the form
procedure TForm1.FormCreate(Sender: TObject);
begin
  R := Rect(10, 40, 175, 175);
  // Size the image we'll use later to fit the rectangle. We set
  // the position below.
  Image1.Width := R.Right - R.Left;
  Image1.Height := R.Bottom - R.Top;

  Self.Height := 375;
  Self.Width := 350;
  Button1.Left := 238;
  Button1.Top := 16;
  Button2.Left := 238;
  Button2.Top := 48;
  Image1.Left := 160;
  Image1.Top := 190;
  // Disable the second button until the first has been clicked
  Button2.Enabled := False;
end;

end.

这篇关于如何创建特定区域的屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆