Solved

CopyRect from one Bitmap to another

Posted on 2004-08-04
7
801 Views
Last Modified: 2010-04-05
Dear experts,
I have got a camera image 240x320 px which comes 6 times
per second. I need to copy a specific part out of the image
into another one. This must be really fast !!! I tried using
the canvas.CopyRect function (code below) but this is extremely
slow and makes my CPU going up to 100%.

I need a working solution (code) very fast.

Thanks a lot
fuluppi

My code example:

 b:=TBitmap.Create;
  b.Width := 320;
  b.Height := 240;
  b.LoadFromStream(ms);
  b.Canvas.FillRect(Rect(0,0,320,100));
  b.Canvas.FillRect(Rect(0,150,320,240));
 
n := TBitmap.Create;
n.width := 190;
n.height := 100;

//THIS IS SO SLOW !!!
n.Canvas.CopyRect(rect(0,0,190,100),b.Canvas,Rect(60,100,250,200));

  image1.Picture.Bitmap := b;
0
Comment
Question by:fuluppi
  • 4
  • 2
7 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 11718776
try ScanLine.
In a few moments I'll give You some code
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 11718826
use the bitblt-api
0
 
LVL 17

Accepted Solution

by:
mokule earned 500 total points
ID: 11719052
// I don't check which is faster

// with ScanLine
procedure TForm1.Button1Click(Sender: TObject);
var
  leftsrc,topsrc: integer;
  hsrc,wsrc:integer;
  leftdst,topdst: integer;
  h,w: integer;
  rowsrc, rowdst: pByteArray;
begin
  leftsrc := 0;
  topsrc := 0;
  hsrc := 100;
  wsrc := 100;
  leftdst := 120;
  topdst := 120;
  for h := 0 to hsrc-1 do
    begin
    rowsrc := pByteArray(Image1.Picture.Bitmap.ScanLine[topsrc+h]);
    rowdst := pByteArray(Image1.Picture.Bitmap.ScanLine[topdst+h]);
    for w := 0 to wsrc-1 do
      begin
      // assumming pixelformat pf24bit
      rowdst[3*(leftdst+w)] := rowsrc[3*(leftsrc+w)];
      rowdst[3*(leftdst+w)+1] := rowsrc[3*(leftsrc+w)+1];
      rowdst[3*(leftdst+w)+2] := rowsrc[3*(leftsrc+w)+2];
      end;
    end;
  Image1.Invalidate;
end;

// with BitBlt

procedure TForm1.Button2Click(Sender: TObject);
var
  leftsrc,topsrc: integer;
  hsrc,wsrc:integer;
  leftdst,topdst: integer;
begin
  leftsrc := 0;
  topsrc := 0;
  hsrc := 100;
  wsrc := 100;
  leftdst := 120;
  topdst := 120;
  BitBlt(Image1.Canvas.Handle,leftdst,topdst,wsrc,hsrc,
          Image1.Canvas.Handle,leftsrc,topsrc,SRCCOPY);
  Image1.Invalidate;
end;
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 17

Expert Comment

by:mokule
ID: 11719340
All right meikl
BitBlt is 6 times faster
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 11723472
yep, just got no time for a sample yesterday,
but you did this already mokule, you deserves the points,
if this is the solution

meikl ;-)
0
 

Author Comment

by:fuluppi
ID: 11888369
hi !
i tried bitblt. it works fine and pretty fast with a TImage but when I try to copy thge rectangle into a new bitmap
it doesn't work any longer. why ???

  b:=TBitmap.Create;
  b.Width := 320;
  b.Height := 240;
  b.LoadFromStream(ms);

  b1:=TBitmap.Create;
  b1.Width  := 320;
  b1.Height := 100;

  BitBlt (b1.Canvas.Handle,0,0,320,100,b.Canvas.Handle,0,0,srccopy);

I'm not able to pass b1 as parameter to a component for further processing....

thanks
Philip
0
 
LVL 17

Expert Comment

by:mokule
ID: 11889529
Hi

Set
b1.PixelFormat := b.PixelFormat;
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question