Solved

CopyRect from one Bitmap to another

Posted on 2004-08-04
7
855 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

729 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