Solved

stretchdraw ... how to use it?

Posted on 2001-07-14
3
551 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
[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
  • 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 50 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

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!

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 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…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

691 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