[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 972
  • Last Modified:

CopyRect from one Bitmap to another

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
fuluppi
Asked:
fuluppi
  • 4
  • 2
1 Solution
 
mokuleCommented:
try ScanLine.
In a few moments I'll give You some code
0
 
kretzschmarCommented:
use the bitblt-api
0
 
mokuleCommented:
// 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
mokuleCommented:
All right meikl
BitBlt is 6 times faster
0
 
kretzschmarCommented:
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
 
fuluppiAuthor Commented:
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
 
mokuleCommented:
Hi

Set
b1.PixelFormat := b.PixelFormat;
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now