View Single Post
  #20  
Old 12-26-2006, 07:44 AM
Vodstok's Avatar
Vodstok Vodstok is offline
Fear scented candle
 
Join Date: Jan 2004
Location: The edge of forever
Posts: 13,650
Okay, lets mix things up a little. underneath you last block of code, type this:

private Color Randy
{
get
{
Random r = new Random();
int i = r.Next(0, 255);
int y = r.Next(0, 255);
int u = r.Next(0, 255);
Color c = Color.FromArgb(i, y, u);
return c;
}

}

this does the same thing we did back when we made the groupBox xchange color, only now we have created a Field. We can now call this Color anywhere in the code simply by typing Randy.

go back up to our btnImage_Click event, and make some changes:

private void btnImage_Click(object sender, EventArgs e)
{
Bitmap bm = new Bitmap(300, 300, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bm);
Rectangle r = new Rectangle(0, 0, 300, 300);
//g.FillRectangle(new SolidBrush(ColorRed), r);
//SolidBrush sb = new SolidBrush(Color.Black);
//g.DrawString(tbImage.Text, new Font(FontFamily.GenericSansSerif,12), sb, 10, 10);

g.SmoothingMode = SmoothingMode.AntiAlias;
string path = @"c:\";
bm.Save(path + "mine.jpg", ImageFormat.Jpeg);
}


I know what you are thinking; What's with the // guys and the new green? Those are called "Comment Tags", they tell the compiler to ignore anything on the same line, typed after them. They exist so that, like best practices in naming, you can make your code clearer to yourself and anyone else who may be looking at the code in the future. In this case, it takes lines of code that we were using previously, and makes them invisible in a sense, so that we dont have to actually erase them (in case we want to use them again), but we dont have to worry about them causing any problems with the new code. More on commenting at a later time

private void btnImage_Click(object sender, EventArgs e)
{
Bitmap bm = new Bitmap(300, 300, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bm);
Rectangle r = new Rectangle(0, 0, 300, 300);
//g.FillRectangle(new SolidBrush(ColorRed), r);
g.FillRectangle(new LinearGradientBrush(new Rectangle(0, 0, 300, 300), groupBox1.ForeColor, groupBox1.BackColor, 180), r);
//SolidBrush sb = new SolidBrush(Color.Black);
LinearGradientBrush lgb = new LinearGradientBrush(new Rectangle(0, 0, 300, 300), Randy, Randy, 90);
//g.DrawString(tbImage.Text, new Font(FontFamily.GenericSansSerif,12), sb, 10, 10);
g.DrawString(tbImage.Text, new Font(FontFamily.GenericSansSerif,12,FontStyle.Bold), lgb, 10, 10);
g.SmoothingMode = SmoothingMode.AntiAlias;
string path = @"c:\";
bm.Save(path + "mine.jpg", ImageFormat.Jpeg);
}

What are these exciting new lines, you ask? Well, rather than the boring old SolidBrush, with it's one color, we have gone with Linear Gradient Brushes. They make a nice transition from one color to another. here is how they break down:
we instantiate a new one, like we do with most of this monsters, and we give it a new set of coordinates, that exactly mimic our rectangle friend (if we re-use the rectangle we already have for everything, it can get messy). For LinearGradientBrushes, we need to set 2 colors. To keep things simple fo rthe first one,w e set it to grab the forecolor and the backcolor to our groupBox, which is awlays changing, so we get random results every time, amking life exciting. we then set the angle of the gradient, this will determine if the colors fade left to right, top to bottom, in a diagonal, etc.

the second one, lgb, is exactly the same, only we use Randy to determine the color, so we come up with a completely seperate random color from the others, and theoretically have enough contrast to read what we typed.

Everything else works the same, so run your program (ctrl-f5), type something, and now look at the results. much more fun.
__________________
Some misguided people decided I was funny enough to pay. See if they're right:
http://www.cracked.com/members/Vodstok/
(I tweet pretty hardcore, too)
Reply With Quote