timage rotation

Posted on 1998-04-09
Last Modified: 2010-04-06
how can a timage be rotated in a fix angle say 90 degress
can any body help me..
this is an easy point.
may the force be with you.....
Question by:SABOG7
1 Comment

Accepted Solution

Waldek earned 20 total points
Comment Utility
Maybe this : from UDDF :}
From: Dave Shapiro <>

Counterclockwise, that is. This rotates a 640x480 24-bit bitmap 90 degrees
in about 2/10 of sec on my P133 (4MB video card, 128MB RAM). It works
for Delphi 2, and should work for 1 and 3, too.

One thing: This does not work for bitmaps that aren't an integral number of
colors per pixel. If that's the case, you'll have to do some bit twiddling. I'm
working on that part right now (I need it too), but it'll be a few days. Anyways,
suggestions, comments, etc., always welcome.

Special thanks to David Ullrich (ooh, I hope I spelled that correctly) for
pointing out the non-published-but-public-anyway SaveToStream method for
the TBitmap class.

Advance apologies for any formatting problems. Netscape's editor is so
ridiculously crippled that I can't accomplish anything (give me rtin and vim
any day).

procedure RotateBitmap90Degrees(ABitmap: TBitmap);

  BITMAP_HEADER_SIZE = SizeOf(TBitmapFileHeader) +

  { Things that end in R are for the rotated image. }
  PbmpInfoR: PBitmapInfoHeader;
  bmpBuffer, bmpBufferR: PByte;
  MemoryStream, MemoryStreamR: TMemoryStream;
  PbmpBuffer, PbmpBufferR: PByte;
  PbmpBufferRFirstScanLine, PbmpBufferColumnZero: PByte;
  BytesPerPixel, BytesPerScanLine, BytesPerScanLineR: Integer;
  X, Y, T: Integer;

    Don't *ever* call GetDIBSizes! It screws up your bitmap.
    I'll be posting about that shortly.

  MemoryStream := TMemoryStream.Create;

   To do: Put in a SetSize, which will eliminate any reallocation


   Don't need you anymore. We'll make a new one when the time comes.

  bmpBuffer := MemoryStream.Memory;

  { Set PbmpInfoR to point to the source bitmap's info header. }
  { Boy, these headers are getting annoying. }
  Inc( bmpBuffer, SizeOf(TBitmapFileHeader) );
  PbmpInfoR := PBitmapInfoHeader(bmpBuffer);

  { Set bmpBuffer to point to the original bitmap bits. }
  Inc(bmpBuffer, SizeOf(PbmpInfoR^));
  { Set the ColumnZero pointer to point to, uh, column zero. }
  PbmpBufferColumnZero := bmpBuffer;

  with PbmpInfoR^ do
    BytesPerPixel := biBitCount shr 3;
    { ScanLines are DWORD aligned. }
    BytesPerScanLine := ((((biWidth * biBitCount) + 31) div 32) * SizeOf(DWORD));
    BytesPerScanLineR := ((((biHeight * biBitCount) + 31) div 32) * SizeOf(DWORD));

    { The TMemoryStream that will hold the rotated bits. }
    MemoryStreamR := TMemoryStream.Create;
     Set size for rotated bitmap. Might be different from source size
     due to DWORD aligning.
    MemoryStreamR.SetSize(BITMAP_HEADER_SIZE  + BytesPerScanLineR * biWidth);

  { Copy the headers from the source bitmap. }
  MemoryStream.Seek(0, soFromBeginning);
  MemoryStreamR.CopyFrom(MemoryStream, BITMAP_HEADER_SIZE);

  { Here's the buffer we're going to rotate. }
  bmpBufferR := MemoryStreamR.Memory;
  { Skip the headers, yadda yadda yadda... }
  Inc(bmpBufferR, BITMAP_HEADER_SIZE);

   Set up PbmpBufferRFirstScanLine and advance it to end of the first scan
   line of bmpBufferR.
  PbmpBufferRFirstScanLine := bmpBufferR;
  Inc(PbmpBufferRFirstScanLine, (PbmpInfoR^.biHeight - 1) * BytesPerPixel);

  { Here's the meat. Loop through the pixels and rotate appropriately. }

  { Remember that DIBs have their origins opposite from DDBs. }
  for Y := 1 to PbmpInfoR^.biHeight do
    PbmpBuffer := PbmpBufferColumnZero;
    PbmpBufferR := PbmpBufferRFirstScanLine;

    for X := 1 to PbmpInfoR^.biWidth do
      for T := 1 to BytesPerPixel do
        PbmpBufferR^ := PbmpBuffer^;
      Dec(PbmpBufferR, BytesPerPixel);
      Inc(PbmpBufferR, BytesPerScanLineR);

    Inc(PbmpBufferColumnZero, BytesPerScanLine);
    Dec(PbmpBufferRFirstScanLine, BytesPerPixel);

  { Done with the source bits. }

  { Now set PbmpInfoR to point to the rotated bitmap's info header. }
  PbmpBufferR := MemoryStreamR.Memory;
  Inc( PbmpBufferR, SizeOf(TBitmapFileHeader) );
  PbmpInfoR := PBitmapInfoHeader(PbmpBufferR);

  { Swap the width and height of the rotated bitmap's info header. }
  with PbmpInfoR^ do
    T := biHeight;
    biHeight := biWidth;
    biWidth := T;

  ABitmap := TBitmap.Create;

  { Spin back to the very beginning. }
  MemoryStreamR.Seek(0, soFromBeginning);


Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now