Join 132,694 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,315 people online right now. Registration is fast and FREE... Join Now!
Here's what I'm trying to do. Ive created a custom HttpHandler to do some image manipulation (create a thumbnail, write a watermark on the image, etc). I have all that working, but theres one thing bothering me. How, if possible as I haven't found a lot of information on it), would I go about making the text "less visible", so it appears as a watermark?
Because I know the rules here, here is the code Im using to write the text on the image
csharp
/// <summary> /// method for getting the display image and write the watermark on it /// </summary> /// <returns></returns> private Image GetDisplayImage(Image img, HttpContext context) { //get image to be written on Bitmap bmp = new Bitmap(img);
//create a graphics object from the image Graphics gfx = Graphics.FromImage(bmp);
//Write your text. gfx.DrawString("Sample from AngelzDesigns.com", new Font(IMAGE_FONT, 17, FontStyle.Bold, GraphicsUnit.Pixel), SystemBrushes.WindowText, new Point(75, 25)); gfx.DrawString("Image is copyright protected " + System.DateTime.Now.Year, new Font(IMAGE_FONT, 17, FontStyle.Bold, GraphicsUnit.Pixel), SystemBrushes.WindowText, new Point(75, 125));
//Save the new image to the current output stream. bmp.Save(context.Response.OutputStream, this._formatType);
//Clean house. gfx.Dispose();
//return the image return bmp;
}
Anyone got any ideas on how I can accomplish this?
This post has been edited by PsychoCoder: 26 May, 2008 - 09:10 PM
I wrote it just for you and hopefully it will help you out. Even if you don't want to use the function I wrote, you could always exploit it for its secrets and then you will know how to do it.
Thought I'd show the final reqults. Here for writing to the image
csharp
/// <summary> /// method for getting the display image and write the watermark on it /// </summary> /// <returns></returns> private Image GetDisplayImage(Image img, HttpContext context) { //get image t be written on Bitmap bmp = new Bitmap(img);
//create a graphics object from the image Graphics gfx = Graphics.FromImage(bmp);
//Save the new image to the current output stream. bmp.Save(context.Response.OutputStream, this._formatType);
//Clean house. gfx.Dispose();
//return the image return bmp;
}
Combined with Marty's snippet
csharp
// Takes a graphics object, a font object for our text, and the text we want to write. // Then writes it to the handle as a watermark private void writeWaterMark(Graphics graphicsHandle, int x, int y, Font ourFont, String text) { StringFormat strFormatter = new StringFormat(); SolidBrush transBrush = new SolidBrush(Color.FromArgb(90, 133, 72, 77));
// Drawstring the transparent text to the Graphics context at x,y position. graphicsHandle.DrawString(text, ourFont, transBrush, new PointF(x, y), strFormatter);
}
Gives the following results:
I smell a detailed tutorial on working with images in C# *evil grin*
This post has been edited by PsychoCoder: 26 May, 2008 - 09:10 PM