Solved

Trying to scale a TBitMap image onto a TImage Canvas

Posted on 2004-08-10
9
1,126 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi 2010 Export to pdf 2 285
Delphi 10 Seattle Dataset Actions 5 78
Delphi - replicating a form 8 62
When i run adoquery my application freezes 26 150
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

919 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