Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I rotate a JPG image

1,197 views
Skip to first unread message

Mark Kimball

unread,
Nov 6, 2005, 11:55:19 PM11/6/05
to

I'm sure this has been asked and answered many times here, but
I have never had to be it until now.
How do I rotate by 90 degrees, a TImage.Picture that has been
loaded from a jpg file? Does anyone have sample code or a
reference to code to do this?
Message has been deleted

dd

unread,
Nov 7, 2005, 9:18:12 AM11/7/05
to

Gabriel Corneanu

unread,
Nov 7, 2005, 1:09:48 PM11/7/05
to
Or use my wrapper:
http://codecentral.borland.com/Author.aspx?ID=162258

Gabriel

"Mark Kimball" <maki...@hotmail.com> wrote in message
news:436e...@newsgroups.borland.com...

JB

unread,
Nov 8, 2005, 6:22:01 AM11/8/05
to

Here is the fastest way to do it: 4 times rotatate in 0milliseconds (0.8ms)

procedure TForm1.Button1Click(Sender: TObject);
Var
T1,T2 : DWord;
begin
T1:=gettickcount;
// Image1 must contain the picture to rotate, remember: no strect
Image2.Canvas.CopyRect(Rect(0,0,217,185),Image1.Canvas,Rect(217,0,0,185));
Image3.Canvas.CopyRect(Rect(0,0,217,185),Image2.Canvas,Rect(0,185,217,0));
Image4.Canvas.CopyRect(Rect(0,0,217,185),Image3.Canvas,Rect(217,0,0,185));
T2:=gettickcount;
Label1.caption:=' Time '+inttoStr(T2-T1);
end;

Regards

Jorgen Bauer
www.dncflex.dk please sign the guestbook to do a country test for me

JB

unread,
Nov 8, 2005, 6:29:21 AM11/8/05
to
> procedure TForm1.Button1Click(Sender: TObject);
> Var
> T1,T2 : DWord;
> begin
> T1:=gettickcount;
> // Image1 must contain the picture to rotate, remember: no strect
> Image2.Canvas.CopyRect(Rect(0,0,217,185),Image1.Canvas,Rect(217,0,0,185));
> Image3.Canvas.CopyRect(Rect(0,0,217,185),Image2.Canvas,Rect(0,185,217,0));
> Image4.Canvas.CopyRect(Rect(0,0,217,185),Image3.Canvas,Rect(217,0,0,185));
> T2:=gettickcount;
> Label1.caption:=' Time '+inttoStr(T2-T1);
> end;
>

Sorry that was mirror / flip

Mark Kimball

unread,
Nov 9, 2005, 7:11:52 AM11/9/05
to

OK, so do you (or does anyone) have example source CODE
(not components) to do the 90 degree rotate? Or links to
recent code examples would work too.

dd

unread,
Nov 9, 2005, 9:36:04 AM11/9/05
to
Mark Kimball wrote:
> OK, so do you (or does anyone) have example source CODE
> (not components) to do the 90 degree rotate? Or links to
> recent code examples would work too.
>
>

Just turn the pixels around. You need to work with intermediate bitmaps
to do the pixel processing.

procedure Rotate90(Source: TGraphic; Target: TJpegImage);
var
SourceBmp, TargetBmp: TBitmap;
r, c: Integer;
x, y: Integer;
begin
SourceBmp := TBitmap.Create;
SourceBmp.Assign(Source);

TargetBmp := TBitmap.Create;
TargetBmp.Width := SourceBmp.Height;
TargetBmp.Height := SourceBmp.Width;

for r := 0 to SourceBmp.Height - 1 do
begin
for c := 0 to SourceBmp.Width - 1 do
begin
//x := (SourceBmp.Height-1) - r; // -90
//y := c; //-90

x := r; //90
y := (SourceBmp.Width-1) - c; //90

// look into Bitmap.ScanLine for faster pixel access
TargetBmp.Canvas.Pixels[x, y] := SourceBmp.Canvas.Pixels[c, r];
end;
end;

Target.Assign(TargetBmp);

SourceBmp.Free;
TargetBmp.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Jpeg: TJPEGImage;
begin
Jpeg := TJPEGImage.Create;

Rotate90(Image1.Picture.Graphic, Jpeg);

Image1.Picture.Assign(Jpeg);

Jpeg.Free;
end;

Nicholas Sherlock

unread,
Nov 9, 2005, 1:23:55 PM11/9/05
to
Mark Kimball wrote:
> OK, so do you (or does anyone) have example source CODE
> (not components) to do the 90 degree rotate? Or links to
> recent code examples would work too.

http://codecentral.borland.com/Item.aspx?id=19723

It rotates Jpegs losslessly so that you don't lose any quality doing it.
Recommended!

Cheers,
Nicholas Sherlock

Scott

unread,
Dec 14, 2005, 8:53:07 AM12/14/05
to
I use Graphics32. http://sourceforge.net/projects/graphics32. Just takes 1
line of code:

var
MySourceImage: tBitmap32;
MyDestImage: tBitmap32;
begin
.
.
MySourceImage.Rotate90(MyDestImage);
.
.
end;

both images need to be tBitMap32.

-scott

"Mark Kimball" <maki...@hotmail.com> wrote in message
news:436e...@newsgroups.borland.com...
>

0 new messages