Welcome to Dream.In.Code
Getting C# Help is Easy!

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!




Font opacity {RESOLVED}

 
Reply to this topicStart new topic

Font opacity {RESOLVED}

PsychoCoder
post 26 May, 2008 - 12:24 PM
Post #1


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,933



Thanked 118 times

Dream Kudos: 8525

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


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);

//set smooting mode
gfx.SmoothingMode = SmoothingMode.AntiAlias;

//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
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 26 May, 2008 - 02:52 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 177 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


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.

Martyr2's Programming Underground - Watermarking Images with Text in C#

At least someone out there may use my function some day! wink2.gif
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 26 May, 2008 - 07:08 PM
Post #3


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,933



Thanked 118 times

Dream Kudos: 8525

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


You da man Marty! Now my component is almost complete, then I will rule the world
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 26 May, 2008 - 07:26 PM
Post #4


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 177 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Just be sure to give me a nice country like Fiji or something so that I may retire in paradise. As for the rest of the world, you can have it. biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 26 May, 2008 - 07:38 PM
Post #5


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,933



Thanked 118 times

Dream Kudos: 8525

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


Pick a country, any country smile.gif
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 26 May, 2008 - 09:09 PM
Post #6


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,933



Thanked 118 times

Dream Kudos: 8525

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


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);

//set smooting mode
gfx.SmoothingMode = SmoothingMode.AntiAlias;

//Write your text.
writeWaterMark(gfx,
((bmp.Width / 2) - (bmp.Width / 3)),
(bmp.Height- 125),
new Font(IMAGE_FONT, 17, FontStyle.Bold, GraphicsUnit.Pixel),
"Sample from AngelzDesigns.com");

//write copyright year
writeWaterMark(gfx,
((bmp.Width / 2) - (bmp.Width / 3)),
(bmp.Height - 100),
new Font(IMAGE_FONT, 17, FontStyle.Bold, GraphicsUnit.Pixel),
"©2006 - " + System.DateTime.Now.Year);

//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:

Attached Image

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
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:48AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month