注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 可扩展、高可用、负载均衡..
 帮助

GDI Graphics In Delphi(Drawing Bitmaps, the TBitmap object )


2008-04-15 14:48:19
 标签:delphi 2D GDI 绘图   [推送到技术圈]

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
  // Make sure the file exists first!
  if not FileExists(Filename) then
  begin
    ShowMessage('The bitmap ' + Filename + ' was not found!');
    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;     // handle to destination device context 
  nXDest,           // x-coordinate of destination rectangle's upper-left corner
  nYDest,           // y-coordinate of destination rectangle's upper-left corner
  nWidth,           // width of destination rectangle 
  nHeight: Integer; // height of destination rectangle 
  hdcSrc: HDC;      // handle to source device context 
  nXSrc,            // x-coordinate of source rectangle's upper-left corner 
  nYSrc: Integer;   // y-coordinate of source rectangle's upper-left corner
  dwRop: DWORD      // raster operation code 
): 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 image 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.


上一篇 迭代法  下一篇 Delphi中的消息截获



    文章评论
 
 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: