views:

252

answers:

4

I've used this piece of code to put an image in a variable. What I wanna do is draw dots on it on various places and then save the result.

What I do is I open a dialog to select the file and to check if it worked, I put it in a picturebox. Using winforms offcourse. Using Visual Studio 2008 Professional.

EDIT:the openImg variable you see used below as the name I gave the openFileDialog instance I'm using.

private string CurrentFile;
private Image img;

private void fileToolStripMenuItem_Click(object sender, EventArgs e)
{
    openImg.Title = "Open Image File";
    openImg.Filter = "JPEG Files|*.jpg" +
                     "|Enhanced Windows MetaFile|*.emf" +
                     "|Exchangeable Image File|*.exif" +
                     "|Gif Files|*.gif|Icons|*.ico" +
                     "|PNG Files|*.png|TIFF Files|*.tif|Windows MetaFile|*.wmf";
    openImg.DefaultExt = "jpg";
    openImg.FilterIndex = 1;
    openImg.FileName = "";
    openImg.ShowDialog();
    if (openImg.FileName == "")
    {
        return;
    }
    CurrentFile = openImg.FileName.ToString();
    img = Image.FromFile(openImg.FileName);
    pictureBox1.Image = img;
}

So far so good.

With this first stage done, I wanted to define a Color object, sothat I can use it to draw at certain locations later.

I've never worked with them before though.

Color yellow = new Color();

I realize that just naming it "yellow" won't make it yellow, but I'm not given the option to select a color... Visual Studio didn't even show the variable in the autocomplete. I'm kinda stumped. What I wanna do is define a certain region on the image to draw in a certain color.

+10  A: 

You cannot construct a new Color the way you have in your question. Color has lots of static properties on it for different colours, plus some methods for defining your own colours (see FromArgb for an example).

To get a yellow colour, you want to use Color.Yellow.


To draw on your image, you want to use Graphics.FromImage(img) to get an instance of Graphics, using which you can draw directly onto the image. Have a look at the methods of Graphics for how to draw lines, curves, shapes, etc...

adrianbanks
Thanks, I should have probably looked up the MSDN documentation on Color.
WebDevHobo
A: 

Color yellow = Color.Yellow or Color yellow = Color.FromArgb(0, 255, 255)

Shay Erlichmen
+1  A: 

Use:

Color yellow = Color.Yellow;
tster
+1  A: 

If you type this:

Color yellow = Color.

... as soon as you type the ".", you will see an Intellisense pop-up that lists the properties and methods available to the Color class.

Sometimes classes can be instantiated with the "new" syntax, and sometimes they can't (like Color). I couldn't really tell you why in any given circumstance, but it's good to get in the habit of checking for static instance creation methods whenever the compiler tells you the "new" syntax won't work.

MusiGenesis