哦~~呵呵,我改进了下,下面的代码我经过测试2304*1728的图像一点也不卡
主要是用蒙版一次生成后保存,以后每次重画更新的区域
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, jpeg, ExtCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
sdimg:Timage;
sdbmp:TBitMap; {增加个变量保存蒙版}
mdpt,mlpt:TPoint; {增加个变量保存上次的选区}
public
{ Public declarations }
end;
const
CL_BG=clgray; {蒙版颜色,自己改}
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure ATGBitMap(var bmp:TBitMap;TR,TG,TB:byte;
alpha:smallint;WPixel:integer);
var
mb,mg,mr,
tmpg,i,j:integer;
p
ByteArray;
begin
j:=255-Alpha;
mb:=TB*j;
mg:=TG*j;
mr:=TR*j;
for i:=bmp.height-1 downto 0 do
begin
p:=bmp.ScanLine
;
j:=0;
while j<WPixel do
begin
tmpg := (p[j]*28+p[j+1]*151+p[j+2]*77) shr 8*Alpha;
p[j]:=(tmpg+mb) shr 8;
p[j+1]:=(tmpg+mg) shr 8;
p[j+2]:=(tmpg+mr) shr 8;
inc(j,3);
end;
end;
end;
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
with TImage(Sender) do
begin
sdimg:=TImage.Create(Parent);
sdimg.SetBounds(left,top,width,height);
sdimg.Parent:=parent;
sdbmp:=TBitMap.Create;
sdbmp.Assign(picture.Graphic);
ATGBitMap(sdbmp,
GetRValue(CL_BG),GetGValue(CL_BG),GetBValue(CL_BG),
100,sdbmp.Width*3);
sdimg.Picture.Assign(sdbmp);
mdpt.X:=x;
mdpt.Y:=y;
mlpt.X:=x;
mlpt.Y:=y;
end;
end;
procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if assigned(sdimg) then
sdimg.Free;
sdimg:=nil;
if assigned(sdbmp) then
sdbmp.Free;
sdbmp:=nil;
end;
procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
rc:TRect;
begin
if assigned(sdimg) then
begin
with TImage(Sender) do
begin
rc:=rect(mdpt.X,mdpt.Y,mlpt.X,mlpt.Y);
sdimg.Canvas.CopyRect(rc,sdbmp.Canvas,rc);
rc:=rect(mdpt.X,mdpt.Y,x,y);
sdimg.Canvas.CopyRect(rc,Canvas,rc);
end;
mlpt.X:=x;
mlpt.Y:=y;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
bmp:TBitMap;
begin
image1.Parent.DoubleBuffered:=true;
bmp:=TBitMap.Create;
try {这里最好先把image里的图像改为bmp格式,免的每次获取图像时都要转换}
bmp.Assign(image1.Picture.Graphic);
image1.Picture.Assign(bmp);
finally
bmp.Free;
end;
end;
end.