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.
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.
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:
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.
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
收藏
推荐专栏更多
猜你喜欢
领域驱动设计战略建模在重构业务系统时的应用
如果你是业务线的程序员
Java线程:线程的调度-休眠
用光影魔术手制作一寸照片(8张一寸)
Linux关闭休眠和屏保模式
Windows7删除休眠文件hiberfil.sys节省大量C盘空间
康熙对容妃的最后的心里话
《一位IT退休老兵的感言、工作、生活、前途、选择》
加入域时遇到“找不到网络路径”错误解决办法汇总
史上最全:怎样买到最便宜的机票 超级实用篇
易都市三维城市地图网址
新东方王强经典语录
智能合约编程语言-solidity快速入门(下)
Android中适配器的notifyDataSetChanged()为何有时不刷新
如何在Spring Boot中使用Hibernate Natural ID
从hook开始聊聊那些windows内核数据结构
Scala的actor
正则性能调优
windows下kafka+ELK的日志系统
Nginx下,请求本机另外Host很慢
扫一扫,领取大礼包
Ctrl+Enter 发布
发布
取消