HOME - - - - - Delphi Tutorials TOC - - - - - - Other material for programmers

Delphi tutorial: Introduction to Graphics

This has good information, and a search button at the bottom of the page

Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!

Click here if you want to know more about the source and format of these pages.

Simple Graphics work.

This is quite an old tutorial... around since about 2000 or earlier. It has things I haven't done again elsewhere, and you might like my "novice" style. But before you read this one, you might want to try one I wrote around Christmas time 2013. It is written for the Lazarus community, but what you do in Lazarus is almost exactly what you do in Delphi, and I have noted the tiny difference in my more modern Introduction to Graphics Programming for Lazarus and Delphi. That will open in a new tab or window, so just close that, and you will be back here.
Start a project called DD04. Save the main form as DD04f1, the unit as DD04U1. Put a button on the form, caption it 'Do it'.

The following will draw a line when you click 'Do it':
procedure DD04f1.ButtonClick(Sender:TObject);

Note that the moveto/ lineto calls must not be made in the form's OnCreate event handler. You won't see the line. I think it boils down to the idea that you have been trying to draw on something before it exists.
Have a look in the Delphi help files under "Drawing lines and shapes" for the wealth of other things you can do.

Unfortunately, something as simple as the above isn't satisfactory. Run the program. Resize the window smaller to cover up some of the line. Now resize it bigger again... the bit of the line you lost has not come back, has it?

Remove the 'Do it' button and its OnClick handler.

Add to the form a TImage object... you will find it on the 'Additional' tab of the components palette. Make it 400 wide, 100 high, put top and left at 10. Leave it with its default name of Image1.

Put the following in the form's OnCreate handler:
var Bitmap:TBitmap;(*Provide a suitable variable*)
begin(*main of OnCreate*)
Bitmap:=TBitmap.create;(*Create a bitmap object*)
Bitmap.width:=400;(*Assign dimensions*)
Image1.Picture.Graphic:=Bitmap;(*Assign the bitmap to the image component*)
   (*Have a look at the canvas Help to see all the properties*)

Note that for some reason, now you CAN draw on the object even during the forms OnCreate event. This graphic DOES persist, even after the window is resized or covered.
The Delphi 1 help file has lots of good stuff in it.. but the hyper-links and index seem faulty to me. Start with a search for drawing lines. Double click on Drawing Lines and Boxes. Now double click on the topic called Drawing Lines and Shapes. From that, you can use 'See Also' to look at Using Pens and Using Brushes. See also the help material under the "Style property" topic of the result of a search on "Style"
Have fun!!!

If you have a graphic in a .BMP file (as produced, say, with Paintbrush) then replace the two lines in the above setting the bitmap width and height with:

(You must not resize the bitmap after the LoadFromFile... but you can draw on the Bitmap.canvas as before.) If Image1 wasn't big enough for the bitmap you tried to load, you will loose parts of the bitmap. However, even if Image1.autosize equals false, loading a small bitmap into a large image will shrink the image to fit the bitmap.

I do not at this time know how to load .GIF files to a bitmap. In some cases, using something like Paint Shop Pro, you can load a .GIF into a graphics package, then re-save it as a .BMP file. Answers for how to LoadFromFile('Demo.GIF') would be welcome, if someone knows how to do it.

   Search this site or the web        powered by FreeFind
  Site search Web search
Site Map    What's New    Search

Click here if you're feeling kind! (Promotes my site via "Top100Borland")
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!

Click here to visit editor's freeware, shareware page.

Link to Tutorials main page
Here is how you can contact this page's author, Tom Boyd.

Valid HTML 4.01 Transitional Page WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org

If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....