急急急 有哪位可以告诉Canvas.CopyRect 的用法(50分)

  • 主题发起人 主题发起人 tsedlinux
  • 开始时间 开始时间
T

tsedlinux

Unregistered / Unconfirmed
GUEST, unregistred user!
Fullscreen.Canvas.CopyRect
(Rect (0, 0, screen.Width, screen.Height), fullscreenCanvas,
Rect (0, 0, Screen.Width, Screen.Height));
主要是这些参数是什么意思
 
ZT:
CopyRect 方法:从其原型CopyRect(Dest: TRect; Canvas: TCanvas; Source: TRect)
可看出,它将源画布某一矩形区域的图像复制到另一个画布的
矩形区域。由于是内存的成块复制,因此具有很高的执行效率。
在Timer 组件的OnTimer 事件程序中灵活使用该函数,可以设计出各
种美观演示效果:如百叶窗、推拉、马赛克、随机线、反像等等。
将以下代码加入到OnTimer 事件程序,可演示各种图像效果,这里
仅举出两种效果,读者可参考有关资料设计更多更漂亮的演示
效果。

......
case PlayMode of
0:begin //从左向右移动(设x初值为Screen.Width)
x:=x-10;
SCRSaverFrom.CopyRect(Rect
(x,0,x+10,Screen.Height),Bit.Canvas,
Rect(x,0,x+10,Screen.Height));
if x=0 then ChangePictureAndPlayMode;//改变图像及演示模式
end;
1:begin //马赛克
for i:=0 to Screen.Width*Screen.Height div 10 do
begin
j := Random(Screen.Width div 4)*4;
k := Random(Screen.Height div 4)*4;
SCRSaverFrom.CopyRect(Rect(j,k,j+4,k+4),Bit.Canvas,
Rect(j,k,j+4,k+4));
end;
SCRSaverFrom.CopyRect(Rect
(0,0,Screen.Width,Screen.Height),Bit.Canvas,
Rect(0,0,Screen.Width,Screen.Height));
ChangePictureAndPlayMode; //改变图像及演示模式
end;
2: ......
......
end;

在DELPHI中能够实现图像象素操作的有两种方法即两个命令,即Copyrect及BitBlt,具体使用方法如下:
1.Copyrect(Dest:TRect;Canvas:Tcanvas;Source:TRect);其中:Dest:目标画布矩形Canvas:源画布Source:源矩形,这是一个DELPHI内部的命令,它主要用于某些控件的画布操作,即CANVAS属性下支持的一个图像复制命令,其功能是把图像从一个源RECT内,复制到目标RECT内,复制的图像具有自动伸缩性质,其功能类似于WINDOWSAPI函数的BITBLT,但操作相对简单;例如命令:
form1.canvas.copyrect(rect2,bitmap.canvas,rect1);
则实现把RECT1中图像复制到窗体的RECT2中;用此命令实现镜像操作的过程是,首先从源图像上的边缘定义宽度为1的矩形RECT,同时从屏幕的中央位置向两侧定义相同尺寸的两个矩形RECT,之后把源图像写到两个目标RECT之中,通过循环过程完成整个图像的重写过程,即在屏幕上出现完整的镜像显示效果。
2.BitBlt(hDestDC,x,y,nWidth,nHeight,hSrcDC,xSrc,ySrc,dwRop)这是一个WINDOWSAPI函数,在DELPHI中由于不用做单独的API函数说明,可以和调用DELPHI内部函数一样使用,所以使用非常方便;其中hDestDC源设备句柄,xy目标矩形右上角坐标,nWidthnHeight为目标矩形的宽度和高度,hSrcDC目标矩形句柄,xSrcySrc源位图矩形左上角坐标,dwRop光栅操作码,比如SRCCOPY为正常拷贝;利用此命令操作起来更为方便,无需事先定义操作区域,全部工作由BitBlt的参数完成,其原理就是把源图像的像素逐条向屏幕的两个方向拷贝,即可完成镜像显示效果。
无论是利用上述的哪个命令,具体操作过程中可通过目标区域的灵活控制,实现具有特殊效果的镜像显示,比如从中心向外显示,或者从两侧向中心显示等等。
 
help中的TCanvas.CopyRect
Copies part of an image from another canvas into the canvas.
procedure CopyRect(const Dest: TRect; Canvas: TCanvas; const Source: TRect);
Description

Use CopyRect to transfer part of the image on another canvas to the image of the TCanvas object. Dest specifies the rectangle on the canvas where the source image will be copied. The Canvas parameter specifies the canvas with the source image. Source specifies a rectangle bounding the portion of the source canvas that will be copied.

The portion of the source canvas is copied using the mode specified by CopyMode.

Fullscreen.Canvas.CopyRect
(Rect (0, 0, screen.Width, screen.Height), fullscreenCanvas,
Rect (0, 0, Screen.Width, Screen.Height));
即:从fullscreenCanvas画布的图像区域Rect (0, 0, Screen.Width, Screen.Height)第三个拷贝到Fullscreen的图像区域第一个参数
我说明白了吗?
 
CopyMode的属性:
cmBlackness Fills the destination rectangle on the canvas with black.
cmDstInvert Inverts the image on the canvas and ignores the source.
cmMergeCopy Combines the image on the canvas and the source bitmap by using the Boolean AND operator.
cmMergePaint Combines the inverted source bitmap with the image on the canvas by using the Boolean OR operator.
cmNotSrcCopy Copies the inverted source bitmap to the canvas.
cmNotSrcErase Combines the image on the canvas and the source bitmap by using the Boolean OR operator, and inverts the result.

cmPatCopy Copies the source pattern to the canvas.
cmPatInvert Combines the source pattern with the image on the canvas using the Boolean XOR operator
cmPatPaint Combines the inverted source bitmap with the source pattern by using the Boolean OR operator. Combines the result of this operation with the image on the canvas by using the Boolean OR operator.
cmSrcAnd Combines the image on the canvas and source bitmap by using the Boolean AND operator.
cmSrcCopy Copies the source bitmap to the canvas.

cmSrcErase Inverts the image on the canvas and combines the result with the source bitmap by using the Boolean AND operator.
cmSrcInvert Combines the image on the canvas and the source bitmap by using the Boolean XOR operator.
cmSrcPaint Combines the image on the canvas and the source bitmap by using the Boolean OR operator.
cmWhiteness Fills the destination rectangle on the canvas with white.
常用的是cmSrcAnd, cmSrcPaint, cmSrcCopy属性。
cmSrcAnd, cmSrcPaint用来复制镂空图,效果不错。
cmSrcCopy则是常用普通的复制。
学会使用CopyRect和其中的CopyMode属性,很有用的。
 
先谢谢了
比如我想截(12,22)到(67,90)这一区域中的内容应该怎么写呢?
 
procedure TForm1.Button1Click(Sender: TObject);
var
aaa: TRect;
begin
aaa.Top := 12;
aaa.Left := 22;
aaa.Right := 67;
aaa.Bottom := 90;
Canvas.CopyRect(Dest, Canvas, aaa);
end;
 
to qince:
那个DEST是怎么定义的啊?
 
DEST是目标Canvas的,看看这个你就明白了
var
newbmp:TBitmap;
i,bmpheight,bmpwidth:integer;
begin
newbmp:=TBitmap.Create;
newbmp.Width:=image1.Width;
newbmp.Height:=image1.Height;
bmpheight:=image1.Height;
bmpwidth:=image1.Width;
for i:=0 to bmpheight do
begin
newbmp.Canvas.CopyRect(Rect(0,bmpheight-i,bmpwidth,bmpheight),image1.Canvas,Rect(0,0,bmpwidth,i));
end;
 
var
aaa,bbb: TRect;
bitmap:tbitmap;
begin
bitmap:=tbitmap.Create;
with bitmap do
begin
aaa.Top := 12;
aaa.Left := 22;
aaa.Right := 67;
aaa.Bottom := 90;
bbb.Top := 12;
bbb.Left := 22;
bbb.Right := 67;
bbb.Bottom := 90;
bitmap.Canvas.CopyRect( bbb , bitmap.Canvas, aaa);
//canvas.CopyRect(canvas.cliprect,desk,desk.cliprect);
//canvas.CopyRect( Rect (h, i, width, height),Canvas ,Rect (h, i, idth , height));
end;
bitmap.SaveTofile('c:/aa.bmp');
end;

这段有什么错误? 图片总是截不下来
 
canvas.CopyRect( Rect (h, i, width, height),Canvas ,Rect (h, i, idth , height));
这句里第一个RECT和第二个RECT中的参数的区别是什么啊??
 
兄弟们都不知道吗?  不会吧
 
以下代码是将窗体考屏并存盘的代码!
代码已经测试,绝对没问题!
注:CopyRect(rRect,myCanvas,rRect)函数中的第一个rRect是目的地的位图位置范围,第二个是源位图的位置范围。
procedure TfrmMain.BitCaptureClick(Sender: TObject);
var
myBmp : TBitmap;
myCanvas : Tcanvas;
dc : Hdc;
rRect : TRect;
begin
myCanvas := TCanvas.Create ;
myBmp := TBitMap.Create ;
Dc := GetDc(Handle);
try
myCanvas.Handle := dc;

rRect := Rect(0,0,Width,Height);
myBmp.Width := rRect.Right;
myBmp.Height := rRect.Bottom;
myBmp.Canvas.CopyRect(rRect,myCanvas,rRect);
myBmp.SaveToFile('c:/test.bmp');
finally
ReleaseDC(0 ,dc);
myBmp.Free;
myCanvas.Free;
end;
end;
 
一个是目标位置及大小和源位置及大小呀。

procedure TCanvas.CopyRect(const Dest: TRect; Canvas: TCanvas;
const Source: TRect);
begin
Changing;
RequiredState([csHandleValid, csFontValid, csBrushValid]);
Canvas.RequiredState([csHandleValid, csBrushValid]);
StretchBlt(FHandle, Dest.Left, Dest.Top, Dest.Right - Dest.Left,
Dest.Bottom - Dest.Top, Canvas.FHandle, Source.Left, Source.Top,
Source.Right - Source.Left, Source.Bottom - Source.Top, CopyMode);
Changed;
end;
 
请告诉我 比如想截(12,22)到(67,90)这一区域中的内容那条语句要怎么写呢?
[:)]
 
var
ARect, BRect: TRect;
Bitmap: TBitmap;
begin
Bitmap := TBitmap.Create;
try
Bitmap.Width := 100;
Bitmap.Height := 100;
A := Bounds(12, 22, 67, 90);
B := Rect(0, 0, Bitmap.Width, Bitmap.Height);
Bitmap.Canvas.CopyRect(B, Form1.Canvas, A);
Bitmap.SaveTofile('c:/aa.bmp');
finally
Bitmap.Free;
end;
end;
 
已经说的这么清楚了你还不明白,一看你就对Delphi很不熟悉!
我说的没错吧!
 
to yaoc:
兄台说的对 画布我还是第一次用
以前主要是网络开发
 
问题解决了 谢谢大家啦 散分 呵呵 [:D]
 

Similar threads

回复
0
查看
888
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部