View Single Post
  #19  
Old 12-26-2006, 06:07 AM
Vodstok's Avatar
Vodstok Vodstok is offline
Fear scented candle
 
Join Date: Jan 2004
Location: The edge of forever
Posts: 13,650
And here is the breakdown:
For starters, we instantiate a Bitmap object, name it "bm" (for bitmap, not bowel movement), and set it's height and width to 300 pixels each, and set it to be a 24 bit image (so we can eventually use all the fancy colors we want).
Bitmap bm = new Bitmap(300, 300, PixelFormat.Format24bppRgb);


next, we instantiate a Graphics object. Think of this as opening photoshop, as it allows us to manipulate our new (empty) bitmap image. we use Graphics.FromImage(bm) to grab the image.

next is Rectangle r = new Rectangle(0, 0, 300, 300);

this created a new Rectangle object, to be used by the Graphics object. This is, in a manner of speaking, a piece of construction paper. the 2 0s set the rectangle's upper left corner position, for our needs, it will be at th pixel located at x 0 and y 0 (eek, geometry... Remember 9th grade math and you will be okay, but dont be afraid to ask if you dont know or remember what i am talking about), and it will be 300 pixels tall and 300 pixels wide. This is how we set the background for our image.

Now, we "fill" the rectangle with color:
g.FillRectangle(new SolidBrush(Color.Red), r);

We use "Brushes" to paint colors in ourt programming. A SloidBrush is simply a one color brush. if you have ever used Paint in Windows, this is the same as clikcing on the bucket, and using red.

Next, we instantiate a new SolidBrush:
SolidBrush sb = new SolidBrush(Color.Black);

Why am I instantiating thisone with a name, and i didnt for the last one? because. No really, that is it. A deeper menaing would be this:

In the first example (where i decalred a new solidbrush in g.FillRectangle), i wanted to make the rectangle red, but i have no need or desire to use that brush for anything else.

However, I instantiated sb so that it can be reused. I can now use sb for any brush needs i want, i just have to manipulate it's properties. Plus, for what i will do later, it will make things easier. Why not instantiate a new named one every time? Well, although it's not as big an issue with C# as it is with older lanbguages like C++, every time you instantiate an object, you use up a little bit of your pc's memory. On this scale, it's like a grain of sand in the Sahara, big deal if it's the size of a buick. When you start getting into bigger applications that are running with thousands or even millions of grains of sand, tyhose buicks begin to add up, and make your machione run like crap, or fill up your memory and crash it. That is bad programming. So even on a small scale, we keep things clean and neat, so that we are intimately familiar with the good practices, in case we ever need to make a million buick-sized grains of sand.



okay, next step:
g.DrawString(tbImage.Text, new Font(FontFamily.GenericSansSerif,12), sb, 10, 10);

g.DrawString is like using a pencil to write text onto our construction paper. tbImage.Text is the text that you will be typing into your text box when the program runs. Next, we need to tell g the font to use. We instatiate a new one, and tell it to use a nice generic font, and to be 12 pixels high (very standard stuff). We tell it to use sb to draw, basically sb becomes our pencil, and it writes in black. we tell it to begin in the 10th pixel on the x axis, and the 10th pixel on the y axis, so it isnt stuck up in a corner somewhere.

this:
g.SmoothingMode = SmoothingMode.AntiAlias;
Just makes it come out nicer, it smooths over rough edges so you dont get those ugly jagged edges on round letters.

this little guy:
string path = @"c:\";
is where the program will save our new file. strings are simply text. Any character can be a string as long as it is contained in "".

Here is where something really simple gets very complicated. Why is there an @ sign out front? Well, C# has something called "Escape Characters", and the biggest one is \ (backslash). it is used to turn characters whithin strings into a part of that string, as opposed to acting upon it. To clarify:
this in code: "Bob is a tool."
will Look like this when displayed by a program:
Bob is a tool.

And this in code: "Bob is a \"tool\"."
will Look like this when displayed by a program:
Bob is a "tool".


Now, if we do it like this:
"Bob is a "tool\"."
We will get an error when we try to compile the code. same if we remove the second \. this is because adding \ in front of " tells the compiler that we want the quotation marks to be part of the text, not encapsulate it. Cool, no?


But it adds a complication to things, because as a result of this behavior, the compiler looks at any backslash as an escape character, and therefore causes problems itself when used. you can fix this in one of 2 ways:
use \\ if you need to use a backslash in text (and for using files, we often need to explicitly set the filepath), OR, we can add @ to the front, which tells the compiler to ignore any backslashes, and treat them as part of the text. No one bothered to teach me this little fact in the beginningand it lead to some horrible headaches.

We could have just as easily dont this for our path:
"c:\\", and it would be looked at by the program as "c:\" when it ran.


Lastly, we do this:
bm.Save(path + "mine.jpg", ImageFormat.Jpeg);

this takes our little path, adds to it a filename (mine.jpg), and saves it as a jpg image, which i am sure all of us are familiar with.

now, hit ctrl-f5, and your program should run. Type something into the textbox (make it vulgar, it's more fun that way), then click the Make Image button. Go to your C drive, and mine.jpg should be hangingout in there. Open it up and marvel at your masterful programming skills.

Now, want to make things a bit more interesting? I thought so.
__________________
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