Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TBitmap vs CopyRect

Status
Not open for further replies.

rcloutie

Programmer
May 26, 2005
164
CA
Hi there,

How to capture a rect from a bitmap to save into another bitmap?

That is, I have an orignal bitmap - Rect(0, 0, 16, 1000);
I want to copy the half bottom to another bitmap - Rect(0, 501, 16, 500);

Anyone knows how to do this?

Thanks in advance
 
Hi,

first, u have a problem with your Trect. Rect(0,501,16,500) have a negative area.. from 501 to 500 = -1

this code should work.. with maybe a little modification of the TRect data..

// bmp_file_source_path='C:\source.bmp';
// bmp_file_dest_path='C:\dest.bmp';
//...
procedure TForm1.Button1Click(Sender: TObject);
var
BMsrc,BMdst: TBitmap;
Rsrc,Rdst: TRect;
begin
BMsrc:=TBitmap.create;
BMsrc.LoadFromFile(bmp_file_source_path);

Rsrc := Rect(0,BMsrc.Height div 2,16,BMsrc.Height);
Rdst := Rect(0,BMsrc.Height div 2,16,BMsrc.Height);

BMdst:=TBitmap.create;
BMdst.Height:=Rdst.Bottom;
BMdst.Width:=Rdst.Right;

//since Rdst=Rsrc u can use only one
BMdst.Canvas.CopyRect(Rdst,BMsrc.Canvas,Rsrc);
BMdst.SaveToFile(bmp_file_dest_path);

BMsrc.free;
BMdst.free;
end;

jb
 
Hi jb,

Thanks for reply.
Here's my code:
------------------------------
var
Dest: TRect;
Source: TRect;
Pic: TBitmap;
begin
Dest := Rect(0, 0, 16, picBkg.Height/2);
Source := Rect(0, picBkg.Height/2, 16, picBkg.Height);
Pic := TBitmap.Create;
Pic.Assign(picBkg);
Pic.Canvas.CopyRect(Dest, picBkg.Canvas, Source);
//...
Pic.Free;
end;
------------------------------

Thanks again.
 
Do this mean u still have problems?.. if so.. i modified the code u pasted to make it work on my side (delphi 5), to do what u ask in your first post

here it goes:

// bmp_file_source_path='C:\source.bmp';
// bmp_file_dest_path='C:\dest.bmp';
//...
procedure TForm1.Button1Click(Sender: TObject);
var
BMsrc,BMdst: TBitmap;
Pic: TBitmap;
Source,Dest: TRect;
begin
// "/2" can cause u a problem, u should use "div 2"
Dest := Rect(0, 0, 16, picBkg.Height div 2);
Source := Rect(0, picBkg.Height div 2, 16, picBkg.Height);

Pic := TBitmap.Create;

//Pic.Assign(picBkg); //<----why? this is use to
//copy the hole bitmap .. and as i read, u need only the
//half bothom

//u must set a size to your bitmap named pic
//since u dont assign anythign to it (assign set
//the size automaticly)
Pic.Height:= picBkg.Height div 2;
pic.Width:= 16;

Pic.Canvas.CopyRect(Dest, picBkg.Canvas, Source);
//...
Pic.SaveToFile(bmp_file_dest_path); //to see the result
//...
Pic.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
picBkg:=TBitmap.Create;
picBkg.LoadFromFile(bmp_file_source_path);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
picBkg.free;
end;


jb
 
>> Do this mean u still have problems?..

No. Problem solved.
Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top