Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Trying to scale a TBitMap image onto a TImage Canvas

Posted on 2004-08-10
9
Medium Priority
?
1,260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 400 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

604 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