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

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.
NAMEDESCRIPTIONEXAMPLE USE
DrawDraws a TGraphic to the canvas, exactly as is. Does no stretching Canvas.Draw(5,10,MyGraphic);
StrechDrawDraws a TGraphic to the canvas, stretching it to fit the given areaCanvas.StretchDraw( Bounds(0,0,32,32), MyGraphic);
CopyRectCopies a portion of a TCanvas to another, stretching it if necessaryCanvas.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 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.

0

收藏

byteh

151篇文章,108W+人气,4粉丝

Ctrl+Enter 发布

发布

取消

078772c84eb23213ea90f577d9316ce4.png
left-qr.jpg

扫一扫,领取大礼包

0

分享
qr-url?url=http%3A%2F%2Fblog.51cto.com%2Fbyteh%2F71673
byteh
noavatar_middle.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值