DelphiFAQ Home Search:

How to flip a bitmap image

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Question:
Is there a RTL or API function to flip a bitmap image?

Answer:

You can use the Windows function StrechBlt() to help you with this. Some older versions of Windows (Windows 3.1, Windows 95) had problems with the graphic card drivers for these functions.
I myself had a Diamond Stealth (or was it a Diamond Speedstar?) where this Windows function was not implemented properly in the Windows 95 driver. There is another Win API function for it.. for device independant bitmaps - it is called StretchDIBits(), but StretchBlt() is faster.

And nowadays (June 2004), all graphic cards should be supported properly.

type
  TMirror = (mtHorizontal, mtVertical, mtBoth );

procedure Mirror(Picture: TPicture; MirrorType: TMirror);
var
  MemBmp: Graphics.TBitmap;
  Dest: TRect;
begin { Mirror }
  if Assigned(Picture.Graphic) then
  begin
    MemBmp := Graphics.TBitmap.Create;
    try
      MemBmp.PixelFormat := pf24bit;
      MemBmp.HandleType := bmDIB;
      MemBmp.Width := Self.Picture.Graphic.Width;
      MemBmp.Height := Self.Picture.Height;
      MemBmp.Canvas.Draw(0, 0, Picture.Graphic);

      case MirrorType of
        mtHorizontal:
          begin
            //SpiegelnVertikal(MemBmp);
            //SpiegelnHorizontal(MemBmp);
            Dest.Left := MemBmp.Width;
            Dest.Top := 0;
            Dest.Right := -MemBmp.Width;
            Dest.Bottom := MemBmp.Height
          end;
        mtVertical:
          begin
            Dest.Left := 0;
            Dest.Top := MemBmp.Height;
            Dest.Right := MemBmp.Width;
            Dest.Bottom := -MemBmp.Height
          end;
        mtBoth:
          begin
            Dest.Left := MemBmp.Width;
            Dest.Top := MemBmp.Height;
            Dest.Right := -MemBmp.Width;
            Dest.Bottom := -MemBmp.Height
          end;
      end; { case MirrorType }
      StretchBlt(MemBmp.Canvas.Handle, Dest.Left, Dest.Top, Dest.Right, Dest.Bottom,
                 MemBmp.Canvas.Handle, 0, 0, MemBmp.Width, MemBmp.Height,
                 SRCCOPY);

      Picture.Graphic.Assign(MemBmp);
      Invalidate
    finally
      FreeAndNil(MemBmp)
    end; { try }
  end; { Assigned(Picture.Graphic) }
end; { Mirror }
You don't like the formatting? Check out SourceCoder then!

Comments:

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.