Solved

stretchdraw ... how to use it?

Posted on 2001-07-14
3
517 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 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