1. Home
  2. Computing & Technology
  3. Delphi Programming
Picture Motion - Double Buffering
Part II: Double Buffering Project
 More of this Feature
• Part I: Intro and Overview: What is Double Buffering.
 
  Related Resources
• Graphics Programming with Delphi
• Delphi Graphics Effects
• TImage.Bitmap Fade Out
 
 

On the Code
The problem with TImage is that it descends from TGraphicControl (which in turn descends from TControl) which means that it does not reside inside it's own window. TImage, TLabel, and TShape all derive from TGraphicControl. Moving these types of controls around (by changing the Top and Left properties) will cause flicker . This is because when moved, they need constant repainting (unlike TWinControl descendants which don't need constant repainting when moved) to be displayed in the new location. Windows itself knows the location of window controls, but knows nothing about TGraphicControls. It is very analogous to having a posterboard: TWinControls are pinned to the posterboard, while TGraphicControls are drawn on the posterboard. It's really easy to move the pinned controls around by simply unpinning them then changing their location. The controls that are drawn directly to the posterboard need to be erased then redrawn. Does this make sense? So the easiest thing to do is Parent the GraphicControl descendant on a WinControl, and move the WinControl (such as a TPanel). Using a TPanel component, be sure to set the FullRepaint property to false.

At this point I decided to create a component that derived from TwinControl but Borland suggests using TCustomControl which has the OnPaint event.I followed it and created TImageWindow.Moving a TImageWindow over a background image causes less flicker.TImageWindow has a Round property that rounds the image.I will use that property while I explain Rotating Bitmaps Using Scanline.

Download TImageWindow [3 KB]

Go for it

  • Download BackGroundImages [1.189 KB]
  • Download Double Buffering [6 KB]
  • To use DirectX with Delphi you will need DelphiX package: downLoad DelphiX Package [1.187 KB]
  • After loading Delphi X you can change TPaintBox with DXPaintBox to convert the project to a DirectX project. Download DirectXBuffering [61 KB]
  • To Rotate Bitmaps Download this project: RotateBitmaps [36 KB]
  • If you wonder how I created all those background Images in the Double Buffering Projects read these lines carefully.

    First I notify you that you don’t need Video Capture Devices to capture videos!!!

    Run the Capture program and then open your AVI file.The frames of your AVI will be saved under directory C:\Screen\Screen1.bmp~Screen2.bmp...~Screeni.bmp

    I run my Cart Racing and Captured the Bitmaps that way.But there were other cars on the track and it was very difficult to save bitmaps while playing and later I played a replay with the view of front wing and no opponent car running.The bitmaps (I captured more than 100) I have got that way are similar to RamBmp.bmp.As you see there are many other things in this Bitmap.Now I have to clip the Track part of this pictures and save them to a file.First I opened RawBmp with MSPAINT and by moving mouse I find the position of Rectangle which has the track picture : Rect(201,204,655,544).

    I will copy this part of picture to Rect(0,0,655-201,544-204) of another bitmap.At this point We should make

    Image1.Width:=655-201;
    Image1.Height:=544-204;

    Now all I have to do is just copying that part of picture from RawBmp to a new bitmap file.

    The source code for that is as follows:

    procedure TForm1.ClipBmpClick(Sender: TObject);
    var
      Bitmap: TBitmap;
      MyRect, MyOther: TRect;
    begin
     // rect of source bitmap 
     MyRect := Rect(201,204,655,544);
    // rect of destination bitmap 
     MyOther:= Rect(0,0,655-201,544-204); 
    
     Bitmap := TBitmap.Create;
     Bitmap.LoadFromFile
      ('c:\Screen\Screen'+IntToStr(i)+'.bmp');
     {this creates a transparent bitmap}
     //Image1.Canvas.BrushCopy
       (MyRect, Bitmap, MyRect, clNone); 
     Image1.Canvas.CopyRect
       (MyOther,Bitmap.Canvas,MyRect);
     Image1.Picture.Bitmap.SaveToFile
      ('C:\Cart1\Michigan'+IntToStr(i)+'.bmp');
     Bitmap.Free;
     i:=i+1;
    end;
    

  • The result bitmap is Michigan.bmp.The size of RawBmp is more than 1.5 MB and the size of Michigan.bmp is 302 KB.That is too much for a game program because you will need hundreds of frames for a good game and The size of your program will be very large.For this reason,I added a BmpToJpg routine to convert bitmaps to jpg.

    Download CaptureScreen [3 KB]

    Previous page > Intro and Overview: What is Double Buffering. > Page 1, 2

     

    Subscribe to the Newsletter
    Name
    Email

  • Explore Delphi Programming
    About.com Special Features

    Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

    Easy ways to connect two computers for networking purposes. More >

    1. Home
    2. Computing & Technology
    3. Delphi Programming

    ©2009 About.com, a part of The New York Times Company.

    All rights reserved.