Solved

stretchdraw ... how to use it?

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

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 33

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 89
delphi exception 7 58
Making delphi communicate with a c# service 16 70
Find and Replace Stream with 0s 8 45
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now