| RTL referenceGlossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To |
| GDI Graphics In Delphi | |||||||||||||||||||||||||||||
| Page 6: Drawing Bitmaps, the TBitmap object | |||||||||||||||||||||||||||||
What Are Bitmaps? 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? 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:
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 Note: All these functions are methods of TCanvas.
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:
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? 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.
Question, Suggestions... Next page > Four Ways To Kill Flicker > Page 1, 2, 3, 4, 5, 6, 7, 8, 9
|
|||||||||||||||||||||||||||||
All graphics (if any) in this feature created by Zarko Gajic.
| More Delphi |
|
· Learn another routine every day - RTL Quick Reference. · Download free source code applications and components. · Talk about Delphi Programming, real time. · Link to the Delphi Programming site from your Web pages. · Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC. |
|
· NEXT ARTICLE:
Articles. More Delphi articles |
| Stay informed with all new and interesting things about Delphi (for free). |
|
|
| Got some code to share? Got a question? Need some help? |

