Trying to scale a TBitMap image onto a TImage Canvas

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?
LVL 15
mikelittlewoodAsked:
Who is Participating?
 
JohnjcesConnect With a Mentor Commented:
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
 
JohnjcesCommented:
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
 
mikelittlewoodAuthor Commented:
Thanks John Ill have a try today.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
mikelittlewoodAuthor Commented:
hmm I can't seem to get it working correctly still John.

Do you have an example of what you did?
0
 
JohnjcesCommented:
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
 
JohnjcesCommented:
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
 
mikelittlewoodAuthor Commented:
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
 
mikelittlewoodAuthor Commented:
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
 
JohnjcesCommented:
I am glad I did manage to help somehow!

John
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.