The following code will resize a graphic. For the purpose of this demo the graphic is placed in an Image container set to the dimensions 128 by 128. (Using delphi 5 the image autosize property has been set to true).
The algorithm uses simple ratios (high school algebra) to calculate the new dimensions of the resized image in order to maintain the correct aspect ratio.
If the graphic is higher than it is wide, the new height will be 128 and the variable we need to find will be the new width...
bitmap.height / bitmap.width = 128 / x
where 'x' is the unknown value for the new width. By cross mulitiplying we get the equation expressed using mulitiplication instead of division...
bitmap.height * x = bitmap.width * 128
We need to isolate the x variable on one side of the equation to be able to calculate the new width, and so this involves the next step of dividing both sides by the bitmap.height. yeilding the final equation for the new width...
x = (bitmap.width * 128) div bitmap.height
or
newwidth := (oldwidth * newheight) div oldheight;
For the case where the width is greater than the height we can assign 128 (the desired target value) to be the newwidth and then calculate the newheight using the same process (use division to compare the ratios with the unknown value being the variable we wish to find, cross multiply, and then divide to isolate the unknown the unknown ratio variable.)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtDlgs, StdCtrls, ComCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
OpenPictureDialog1: TOpenPictureDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var bitmap, resizedbitmap : tbitmap;
newheight, newwidth : integer;
stretchrect : trect;
begin
if openpicturedialog1.execute then begin
bitmap := tbitmap.create;
resizedbitmap := tbitmap.create;
bitmap.loadfromfile(openpicturedialog1.filename);
if bitmap.Width <> bitmap.height then begin
if bitmap.height > bitmap.width then begin
newheight := 128;
newwidth := (newheight * bitmap.width) div bitmap.height;
end
else begin
newwidth := 128;
newheight := (newwidth * bitmap.height) div bitmap.width;
end;
end
else begin
newheight := 128;
newwidth := 128;
end;
stretchrect.left := 0;
stretchrect.Top := 0;
stretchrect.right := newwidth;
stretchrect.bottom := newheight;
resizedbitmap.Width := newwidth;
resizedbitmap.height := newheight;
resizedbitmap.Canvas.StretchDraw(stretchrect, bitmap);
Image1.picture.bitmap := resizedbitmap;
end;
end;
end.
A Unified Field Theory
![]()
The Unified Field Theory
is also available as a zip file -> unified.zip
Introduction :The Pioneer Effect and the New Physics. A brief description of the new physics required to explain the 'Pioneer Effect', which is the constant deceleration of space craft as they fly through space.

