Solved

CopyRect from one Bitmap to another

Posted on 2004-08-04
7
771 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now