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

x
?
Solved

stretchdraw ... how to use it?

Posted on 2001-07-14
3
Medium Priority
?
651 Views
Last Modified: 2010-04-04
Guys,

I want to copy a region of a timage to another timage, but I want that region of timage fit completely into the second timage. I am sure i have to use stretchdraw, but i can't figure out how to do this. Any help?

best wishes,
Manuel Lopez (lopem)
0
Comment
Question by:lopem
  • 2
3 Comments
 
LVL 34

Expert Comment

by:Slick812
ID: 6283060
hello lopem, getting a part (or region) of one TImage to fill another TImage will use the Canvas.StretchDraw procedure. But first you need to make a Bitmap copy of that region and then stretch that bitmap into the second Image. You will have to get the Rectangle size and placement of the the part of the first Image you want to get into the second Image.

{Image1 and Image2 are TImages, Image1 is 250 wide and 150 tall, ,
Image2 is 120 wide and 50 tall}

procedure TForm1.CopyRectOfImage;
var
TempBit: TBitmap;
CopyRect: TRect;
begin
TempBit := TBitmap.Create;
// define the Rect that will be copied from Image1
CopyRect := := Rect(17,36,117,136);
// Set the width and height of TempBit to equal CopyRect
TempBit.Height := CopyRect.Bottom - CopyRect.Top;
TempBit.Width := CopyRect.Right - CopyRect.Left;
// Copy the Rect to Tempbit
Tempbit.Canvas.CopyRect(Rect(0,0,Tempbit.Width, Tempbit.Heigh), Image1.Canvas, CopyRect);
// StretchDraw the Tempbit to fill Image2
Image2.Canvas.StretchDraw(Rect(0,0,Image2.Width, Image2.Height), Tempbit);
Tempbit.Free;
end;

the new image in Image2 will be stretched and distorted. . . if you don't want it to be distorted then set the CopyRect to the width and height of Image2
0
 
LVL 34

Accepted Solution

by:
Slick812 earned 200 total points
ID: 6283696
I'm so used to working with 24bit color pics that I forgot to add the necessary color depth matching for TempBit and Images. So here's a more workable stretchdraw that will use most any Color Depth Picture in Image1.  

procedure TForm1.CopyRectOfImage;
var
TempBit: TBitmap;
CopyRect: TRect;
begin
TempBit := TBitmap.Create;
// get TempBit to match color specs of Image1
TempBit.PixelFormat := Image1.Picture.Bitmap.PixelFormat;
Tempbit.Palette := Image1.Picture.Bitmap.Palette;
// define the Rect that will be copied from Image1
CopyRect := Rect(17,36,117,136);
// Set the width and height of TempBit to equal CopyRect
TempBit.Height := CopyRect.Bottom - CopyRect.Top;
TempBit.Width := CopyRect.Right - CopyRect.Left;
// Copy the Rect to Tempbit
Tempbit.Canvas.CopyRect(Rect(0,0,Tempbit.Width, Tempbit.Height), Image1.Canvas,CopyRect);
// get Image2 to match color specs of TempBit
Image2.Picture.Bitmap.PixelFormat := TempBit.PixelFormat;
Image2.Picture.Bitmap.Palette := TempBit.Palette;
// StretchDraw the Tempbit to fill Image2
Image2.Canvas.StretchDraw(Rect(0,0,Image2.Width, Image2.Height), Tempbit);
Tempbit.Free;
end;

TImage has a Stretch property (in Delphi5) which you could use instead of StretchDraw

procedure TForm1.CopyRectOfImage;
var
TempBit: TBitmap;
CopyRect: TRect;
begin
TempBit := TBitmap.Create;
TempBit.PixelFormat := Image1.Picture.Bitmap.PixelFormat;
Tempbit.Palette := Image1.Picture.Bitmap.Palette;
CopyRect := Rect(17,36,117,136);
TempBit.Height := CopyRect.Bottom - CopyRect.Top;
TempBit.Width := CopyRect.Right - CopyRect.Left;
Tempbit.Canvas.CopyRect(Rect(0,0,Tempbit.Width,Tempbit.Height), Image1.Canvas,CopyRect);
{you can set Image2 AutoSize and Stretch in the Object Inspector and not do it here}
Image2.AutoSize := False;
Image2.Stretch := True;
// assign TempBit to Image2 to stretch it to fit Image2
Image2.Picture.Assign(Tempbit);
Tempbit.Free;
end;
0
 
LVL 3

Author Comment

by:lopem
ID: 6283739
thanks a lot!
great job

Manuel Lopez (lopem)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

873 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