What Are Bitmaps?
Drawing lines and shapes is useful, but not really that exciting. What you really want is the ability to draw pictures, right? Well, that's where bitmaps come in.
When drawing pictures, the object usually used is a bitmap. This is a graphical object that consists of a header, which contains important information about the picture (e.g. height, width, color info, etc.), and the actual bitmap data itself (a massive array giving each pixel's color). Luckily for you, you need not concern yourself with this - a bitmap class already exists in Delphi, called TBitmap.
   So Why Do I Want Bitmaps?
Bitmaps are fantastic, and allow you a lot more freedom than just lines and shapes. You can create a bitmap using your favorite graphics package (e.g. Paint Shop Pro, Photoshop or even Paint) and display it in your program. This is a good thing.
You can use bitmaps to draw off-screen. This might sound a little strange, but it is also officially a good thing. However, first you will want an example of how to use a bitmap, I assume. Well, here goes:
procedure Form1.DrawBitmap(const Filename: String; const x,y: Integer);
var
  Bmp: TBitmap;
begin
  
  if not FileExists(Filename) then
  begin
    ShowMessage( + Filename + );
    Exit;
  end;
  Bmp := TBitmap.Create;
  try
    Bmp.LoadFromFile(Filename);
    Canvas.Draw(x, y, Bmp);
  finally
    Bmp.Free;
  end;
end; 
 
This function tries to load and display a bitmap (specified by Filename, for example 'myBitmap.bmp') at the given (x,y) coordinates supplied (again, remember that y is 0 at the screen top, and is positive down the screen!).
If you are not sure about creating objects at run-time I strongly advise you to check out the article on using Pointers in Delphi. Anyway, the above procedure creates a bitmap, loads a given picture and draws it to screen. The try..finally block ensures the bitmap is always freed, even if there is an error (for example, an invalid bitmap picture).
One quick point to note about the above function is that it isn't very efficient. It creates and destroys a bitmap every time it gets called, and always checks if the file exists first. A better option if you want to draw the same picture a lot is to declare the TBitmap object as part of your form, creating it and loading the picture in FormCreate, and freeing it in FormDestroy. This would be much more efficient, and is the usual course of action.
   GDI Drawing Functions
TCanvas has several useful drawing functions, which all deal with a TGraphic type. The TGraphic type is simply the base class for a graphic object in Delphi, and some examples are: bitmaps (TBitmap), icons (TIcon), metafiles (TMetafile) and JPEGs (TJPEGImage). All use the same drawing functions, so here is a list for you:
Note: All these functions are methods of TCanvas.
 
Note: All these functions are methods of TCanvas.
NAME DESCRIPTION EXAMPLE USE
Draw Draws a TGraphic to the canvas, exactly as is. Does no stretching Canvas.Draw(5,10,MyGraphic);
StrechDraw Draws a TGraphic to the canvas, stretching it to fit the given area Canvas.StretchDraw( Bounds(0,0,32,32), MyGraphic);
CopyRect Copies a portion of a TCanvas to another, stretching it if necessary Canvas.CopyRect( Bounds(0,0,32,32), MyBmp.Canvas, Bounds(0, 0, 640, 480));
These are all nice and straightforward. At this point, however, I think it's time to scare you senseless. Just for laughs, here's the Windows GDI drawing function that TCanvas.Draw nicely wraps up, BitBlt:
function BitBlt(
  hdcDest: HDC;     
  nXDest,           
  nYDest,           
  nWidth,           
  nHeight: Integer; 
  hdcSrc: HDC;      
  nXSrc,            
  nYSrc: Integer;   
  dwRop: DWORD      
): Boolean; 

This is about the time you let out a sigh of relief - you see what you don't need to put up with, thanks to Delphi :-). If you're feeling brave you can use BitBlt instead of Canvas.Draw. Go on, it's something everyone needs to do at least once.
   What Other Options Are There?
The above assumes that you will be drawing your bitmaps at run-time. There is, of course, an easier way - just place a TImage on the form and set its picture at design time. The p_w_picpath will stay where you placed it. However, that's no fun ;-). Besides, it's easier to do animation using bitmaps.
It's very easy to use TImages on your form, but the animation/off-screen drawing techniques you learn using bitmaps can be easily transferred to other graphics libraries (e.g. DirectX) and will lessen the learning curve.

0

收藏

byteh

152篇文章,115W+人气,6粉丝

Ctrl+Enter 发布

发布

取消

扫一扫,领取大礼包

0

分享
byteh