Bitmap Rotation

Posted on 1997-05-12
Last Modified: 2010-04-06
how do I rotate the bitmap in a standard TBitmap say 90 degrees left or right
Question by:elessar
  • 2

Accepted Solution

erajoj earned 50 total points
ID: 1336157
I had no clue whatsoever howto rotate a bitmap fast... I had to find out.
This code is fast(!/?). And it can rotate a TImage at ANY
Set the arbangle real to what you want and the rest will be done
for you:

procedure TMainForm.Image1DblClick(Sender: TObject);
  points      : array[0..3] of TPoint;
  bmp         : TBitmap; { temporary storage }
  index       : Integer;
  minx, miny  : Integer;
  maxx, maxy  : Integer;
  arbangle, r : Real;

  function min(a, b: Integer): Integer;
    if a<b then result := a else result := b;

  function max(a, b: Integer): Integer;
    if a>b then result := a else result := b;

  arbangle := 5; { arbitrary angle, can be any angle }
  if sender is TImage then with (sender as TImage) do { Is it a TImage? }
  if not picture.bitmap.empty then begin              { Is it a bitmap? }
    bmp := TBitmap.Create;
    r := Math.Degtorad(arbangle);
    { Some lame maths }
    points[0].x := round(width-(cos(r)*width-sin(r)*height));
    points[0].y := round(height-(sin(r)*width+cos(r)*height));
    points[1].x := round(width+sin(r)*height);
    points[1].y := round(height-cos(r)*height);
    points[2].x := round(width-cos(r)*width);
    points[2].y := round(height-sin(r)*width);
    points[3].x := Width;
    points[3].y := Height;
    { Remove negative coordinates }
    minx := min(points[0].x, min(points[1].x, min(points[2].x, points[3].x)));
    miny := min(points[0].y, min(points[1].y, min(points[2].y, points[3].y)));
    for index := 0 to 3 do begin
      dec(points[index].x, minx);
      dec(points[index].y, miny);
    maxx := max(points[0].x, max(points[1].x, max(points[2].x, points[3].x)));
    maxy := max(points[0].y, max(points[1].y, max(points[2].y, points[3].y)));
    { Set size }
    bmp.width  := maxx;
    bmp.Height := maxy;
    { Here's the magic }
    if not PlgBlt(
      bmp.canvas.handle, // handle of destination device context
      points,               // vertices of destination parallelogram
      canvas.handle,       // handle of source device context
      0,                // x-coord. of upper-left corner of source rect.
      0,                // y-coord. of upper-left corner of source rect.
      Width,             // width of source rectangle
      Height,             // height of source rectangle
      0,                 // handle of bitmask
      0,                 // x-coord. of upper-left corner of bitmask rect.
      0                   // y-coord. of upper-left corner of bitmask rect.
     ) then { errorhandling stuff };
     { Could possibly have been done in a better way }
     picture.bitmap.Width  := bmp.Width;
     picture.bitmap.Height := bmp.Height;
     canvas.draw(0, 0, bmp);


Hope you can use it!

/// John


Author Comment

ID: 1336158
exactly what I was looking for ta! - since I have also posted this question on newsgroups and some people have requested that if I get any replies could I forward them... any objections if I pass this code around?

Expert Comment

ID: 1336159
No objections whatsoever!
Just make sure to mention that you got it via
the Experts Exchange.

/// John

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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