Graphics and images, when used properly and with good taste, can easily enhance the overall layout and perception on any application or project. The simplest way to display an image would be to use the picture box control that ships with the .Net frameworks, but this is not the the most efficient method. You can draw an image on almost any control in the .Net frameworks using the paint event and a some simple coding.
Drawing with the Paint Event
To access the paint even of your desired control simply click it in design view mode and under the events tab double click the Paint event. This will bring you to code view into the new painting function for the control you selected.
Type or copy and paste in the follow source code into the newly created function.
Graphics GFX = e.Graphics;
Bitmap Img = new Bitmap("path/to/desired/image");
IMG.DrawImage(Img, x, y);
You will need to replace where is says, "path/to/desired/image", with the entire path, including the file name, of your image file. Also replace the x and y with the coordinates on your control where you would like to start painting the image.
At this point the code used will not work because you have not yet included the proper references in your project. Scroll to the top of the code view window where you will see this code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
...
Add this code to the bottom of these commands:
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
Now you can successfully compile your application and see the desired results.
Published by James Cloud
I like to program and do basically anything that has to do with technology and computers. View profile
