Solved

Trying to scale a TBitMap image onto a TImage Canvas

Posted on 2004-08-10
9
1,151 Views
Last Modified: 2012-06-27
Trying to scale a TBitMap image onto a TImage Canvas

Ive tried to be a bit clever (though not succeeding) and try to scale an TBitMap image onto a TImage canvas.

At the beginning of my application I set up bitmap size to printer canvas size
bmpPreview.Width := Printer.PageWidth;
bmpPreview.Height:= Printer.PageHeight;

and then I run a procedure (combo box change for 3 types of aspect ratios)
to default the aspect to combobox.itemindex = 0

procedure cbAspectChange(Sender: TObject);
var
  dFactor: double;
begin
  // set out scaling factor
  case cbAspect.ItemIndex of
  0: dFactor := 5;
  1: dFactor := 7.5;
  2: dFactor := 10;
  end;

  // set page size for viewing and global page size variables
  imgPreview.Width := round(Printer.PageWidth / dFactor);
  imgPreview.Height := round(Printer.PageHeight / dFactor);

  // send the bitmap to the image
  imgPreview.Canvas.StretchDraw(imgPreview.Canvas.ClipRect, bmpPreview);
end;

Then I go draw my bitmap

procedure CreatePage(iPageNum: integer);
begin
  // clear the canvas
  bmpPreview.Canvas.brush.Color := clWhite;
  bmpPreview.Canvas.FillRect(bmpPreview.Canvas.ClipRect);

  ** DO SOME DRAWING **

  // send the bitmap to the image
  imgPreview.Canvas.StretchDraw(imgPreview.Canvas.ClipRect, bmpPreview);
end;

this works great the first time it is drawn, what ever aspect ratio I start it at.

why then when I change the combo box aspect to one of my other options,
the command imgPreview.Canvas.StretchDraw(imgPreview.Canvas.ClipRect, bmpPreview)
doesnt just redraw the bitmap correctly scaled down onto my new image canvas size?
0
Comment
Question by:mikelittlewood
  • 5
  • 4
9 Comments
 
LVL 18

Expert Comment

by:Johnjces
ID: 11764685
I had a similar problem in an application I wrote when I was using TImage, the componnent.

I solved it by creating a TempImg :TBitmap. I would use it then destroy it when finished after copying the canvas of BMP to the image preview. Then if I want to remanipulate the image, I create the TemImg again, redraw, rescale whatever, copy it a TImage to view it and destroy the TmpImg again.

Worth a try anyway. It fixed my problem.
0
 
LVL 15

Author Comment

by:mikelittlewood
ID: 11772771
Thanks John Ill have a try today.
0
 
LVL 15

Author Comment

by:mikelittlewood
ID: 11781388
hmm I can't seem to get it working correctly still John.

Do you have an example of what you did?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
LVL 18

Expert Comment

by:Johnjces
ID: 11784201
I do. It will be later today as my code is at home. It may not exactly be what you're looking for, but in my routine, I rescale and reposition graphic text.

I'll post it around 8PM tonight, GMT -7.

John
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 11789394
Here's the code I use to put some text in a an image, creating a temporary bmp, which is bmp and Tempbmp.

I hope this helps you. It may not. But, I tried.


procedure TFWaterMark.Image1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
 bmp, TempBMP : TBitmap;
 TheAngle : Integer;
begin
if FScanAsPDF.JvThumbview1.Count > 0  then
begin
 image1.Picture := nil;
 bmp := TBitmap.Create;
 try
  bmp.Width := 650;      //210
   bmp.Height := 150;
   bmp.Canvas.FillRect(rect(0, 0, bmp.Width, bmp.Height));
   bmp.Canvas.Font.Color := ColorComboBox1.ColorValue;
   bmp.Canvas.Font.Name := FontComboBox1.FontName;
   bmp.Canvas.Font.Style := [fsBold];
   bmp.Canvas.Font.Size := SpinEdit1.Value;
   bmp.Transparent := True;
   bmp.Canvas.TextOut(5, 5, ComboBox3.Text); // This is the text
   try
    tempbmp := TBitmap.Create;
    tempbmp.LoadFromFile(ExtractFileDir(ParamStr(0)) + '\~tmp.bmp');
   if ComboBox2.Text = 'Angled Down' then
    begin
     TheAngle := -SpinEdit2.Value;
     tempbmp.Canvas.Draw(Round(X*2.6), Round(Y*3),CreateRotatedBitmap(bmp, TheAngle, 16777215));
    end
     else
   if ComboBox2.Text = 'Angled Up' then
    begin
     TheAngle := SpinEdit2.Value;   //3.86          //4.3
     tempbmp.Canvas.Draw(Round(X*3), Round(Y*0.8),CreateRotatedBitmap(bmp, TheAngle, 16777215));
    end
     else
    begin
     ComboBox2.Text := 'Horizontal';
     TheAngle := 0;                           //4.5
     tempbmp.Canvas.Draw(Round(X*3.3), Round(Y*3.35),CreateRotatedBitmap(bmp, TheAngle, 16777215));
    end;
    Image1.Picture.Bitmap := tempbmp;
     finally
      bmp.Free;
   end;
  finally
 end;
end;
end;
0
 
LVL 15

Author Comment

by:mikelittlewood
ID: 11792495
Ah ok

Slightly different to what Im doing, but I maybe able to do this.
What Im not sure about is what I should be sending to my final image.

I make my image to be a percentage size of my printer canvas, and then I want to stretchdraw the bitmap onto it
imgPreview.Canvas.StretchDraw(imgPreview.Canvas.ClipRect, bmpPreview);

in your case though, you are setting the bitmap property of the image to be the bitmap.
I guess therefore I would need to shrink the bitmap.canvas down first to be the same size as the image canvas? or will it automatically take care of it for me?
maybe I should stretchdraw my bitmap onto a smaller temp bitmap, and then set the image.picture.bitmap property to the new temp bitmap.

Any thoughts?
0
 
LVL 18

Accepted Solution

by:
Johnjces earned 100 total points
ID: 11793357
Mike,

First set all your image sizes you want at the beginning, Your final TImage or that which displays your final output can simply be STretch := True.

I know what I am doing is different. I know that when I used a TImage for everything I get things to work one time. Then nothing else until I did the same thing with a bmp created and destroyed at runtime.

Sorry I couldn't be of more help.

John
0
 
LVL 15

Author Comment

by:mikelittlewood
ID: 11818160
ah ha!

That was the flaw in my code.
I didnt have the Stretch property set to True for my TImage.

All works well now .. though the scaling Im doing .. the image does look a bit crap  :o)

Thanks a lot John
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 11821379
I am glad I did manage to help somehow!

John
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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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