
So I saw that Jeff Sandquist’s webcam solution had some issues and I was kind of surprised there wasn’t a decent solution out there. I won’t pledge a great application, but I will pledge a decent one that can be altered to fit your needs.
So I have three requirements for this:
- Uploads to FTP
- Uploads to flickr also
- Can embed a time stamp with a nice transparency
From a high level, here is the application’s load function. Note this is just the image processing part. Since this is GDI+, we want to be very careful to dispose of our graphic objects since we don’t want memory leaks. Now I have not verified this is memory leak free so be warned.
We do the resize so we don’t resize the text we’ll pop on top of the image. I did some research and found some nice guy did a bit of help for me when it came to resizing over at Switch On The Code. I won’t really repeat that code as they’ve already done it. I tweaked it slightly but for the most part, their code works.
var returnValue = new Bitmap("C:\\test.jpg");
returnValue = (Bitmap)resizeImage(returnValue, new Size(250, 400));
int rectHeight = 25;
addRectangle(returnValue, 0, returnValue.Height - rectHeight,
returnValue.Width, rectHeight, Color.FromArgb(180, Color.Black));
addImageText(returnValue, "Hi from Mix'09", 0,
returnValue.Height - rectHeight, Color.White);
pictureBox1.Image = returnValue;
After that, we’ll pop on the rectangle overlay then put on the text.
private static void addImageText(Image imgToAddText,
string text, int x, int y, Color color)
{
using (Graphics g = Graphics.FromImage(imgToAddText))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawString(text,
new Font("Arial", 20, GraphicsUnit.Pixel),
new SolidBrush(color),
x, y);
g.DrawImage(imgToAddText,
0, 0, imgToAddText.Width, imgToAddText.Height);
}
}
private static void addRectangle(Image imgToAddRect,
int x, int y, int width, int height, Color color )
{
using (Graphics g = Graphics.FromImage(imgToAddRect))
g.FillRectangle(
new SolidBrush(color),
new Rectangle(x, y, width, height));
}
So after all that, here is the image after all the processing. Next up, DirectShow.Net integration followed by some FTP and Flickr action.