Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Images handeling

Status
Not open for further replies.

Snaples

Programmer
May 7, 2007
16
IL
Hi there,
i have this regular VCL form application with a JPG image,
How do i Resize, Rotate, etc... ?
(As code, not initial values).

Thanks in advance!
 
What you need to do is:

1. Create a bitmap
2. use a rectangle (TRect) to assign set the size of the new image (Left & Top = 0, right = new width, bottom = new height)
3. use the StretchDraw bitmap procedure to draw the jpeg on the bitmap canvas
4. Save the JPEG

Here is a basic example:
Code:
uses JPEG;
....

procedure ResampleJPEG(AJPG : TJPEGImage; AWidth, AHeight : Integer; AFileName : String);
var
  bmp : Tbitmap;
  rect : TRect;
begin
  rect.Left    := 0;       //left corner
  rect.Top     := 0;       //top
  rect.Right   := AWidth;  //width
  rect.Bottom  := AHeight; //height

  
  bmp         := TBitmap.Create;
  bmp.Width   := rect.Right;
  bmp.Height  := rect.Bottom;

  bmp.Canvas.StretchDraw(rect,AJPG);
  
  AJPG.Assign(bmp);
  AJPG.CompressionQuality := 100;
  AJPG.SaveToFile(AFileName);

  bmp.Free;
end;

This is rough example, so you should add a try-finally statement to free the bitmap (unless you do it on form.destroy). You should also free the jpeg (AJPG.Free) in the procedure that you create it.

I can't think of how to rotate off the top of my head.

Also try googling:

Delphi resample OR resize image
Delphi rotate image
 
Resize (dispwidth is the width to make the jpg)
Code:
function ResizeImg(Image : TJPegImage) : TJPegImage;
var bitmap: TBitmap;
const Dispwidth = 309;
begin
  Try
   bitmap := TBitmap.create;
   result := TJPegImage.create;
   bitmap.width := Dispwidth;
   bitmap.height := round(Image.Height/(Image.Width/Dispwidth));
   bitmap.canvas.stretchDraw(rect( 0,0, Dispwidth, round(Image.Height/(Image.Width/Dispwidth))), Image);
   result.Assign(bitmap);
   result.JpegNeeded;
  Finally
   bitmap.free;
 end;
end;
Rotate 90deg
Code:
function RotateImg(Bitmap : TBitmap): TBitmap;
var
  x,y : Integer;
  H,W : Integer;
begin
  W:=Bitmap.Width;
  H:=Bitmap.Height;
  Result := TBitmap.Create;
  try
    with Result do
    begin
      Width:=H;
      Height:=W;
      for x:=0 to W-1 do
        for y:=0 to H-1 do Canvas.Pixels[H-y-1,x]:=Bitmap.Canvas.Pixels[x,y];
    end;
  except
    Result.Free;
    raise;
  end;
end;

you will need to tweak these because the result isnt freed.

Aaron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top