View Single Post
  #18  
Old 12-26-2006, 06:34 AM
Vodstok's Avatar
Vodstok Vodstok is offline
Fear scented candle
 
Join Date: Jan 2004
Location: The edge of forever
Posts: 13,650
Okay, how about a new lesson? We will stick with the same project and add to it.

Wanna make a .Jpg image with some text? No? Too bad.


here we go. Open your project, and look at Forms.cs. Way at the top, you should see this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


Add this to the end of that list (remember, C# is CASE SENSITIVE):
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

Now, go to Form1.cs [Design]. Drag and drop a new button onto the form. Right click it and select "Properties". The properties pane shoudl appear (if it isnt already there). There is a long list of stuff, but find (Name). Change the name to btnImage.

Why the weird name? There is this fun little thing i mentioned before called "Best Practices". They are a list of suggestions to keep code clean, consistent, and easy to read. As a programmer, i cant tell you the number of times i have worked on projects where the guy before me didnt use this, and it is a godamn nightmare... Never mind going back to something you wrote a year before, and knew inside and out, and now you are looking at it with fresh eyes and wondering why you called a textbox "Zombie".

The standard goes like this, start with lowercase, and on the first part that is a new word, use uppercase, and the same for any other word there after. Keep it simple but descriptive.

Example, i renamed the button that we will use to creat the image btnImage. (sorry if this seems condescending to some, but i would rather not leave out anything.) btn for Button, and Image for Image. If i was going to make another one that saved the image, i might call it btnImageSave, or somethingto that effect.


change the text of the button to Make Image.

now, drag and drop a textBox onto the form and stretch it out to hold more text (this is only aesthetic). Rename it tbImage. Now, double-click btnImage.
And we get:

private void btnImage_Click(object sender, EventArgs e)
{

}
Which should be getting a little familiar now.

Now, things start to get a little more complicated here.

Type all of this, then i will explain why:

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(Color.Red), 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);
}
__________________
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