Watermark/label custom http handler

T

Tem

I need to write a custom handler when the handler is accessed, it returns
the photo in jpg with 2 lines on the bottom of the image.
lower left "Taken by"
lower right "January 2 2008"

This is what I have so far...

public class certificate : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.BufferOutput = false;

context.Response.WriteFile("/photos/1.jpg");

}

public bool IsReusable {
get {
return false;
}
}
}

Your help is greatly appreciated,
Tem
 
T

Tem

This is what I came up with.
It only outputs a black image.. something's not right
But I can't seem to figure out what is wrong with it.
Please have a look

Thanks

<%@ WebHandler Language="C#" Class="LabelPhoto" %>

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;


public class certificate : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.BufferOutput = false;

GenerateText(@"C:\photo1.jpg").Save(context.Response.OutputStream,
ImageFormat.Jpeg);
}

public bool IsReusable
{
get
{
return false;
}
}


private Bitmap GenerateText(string filePath)
{
int opac = 250;
string text = "hello";

Image i = Image.FromFile(filePath);

int width = i.Width;
int height = i.Height;

Graphics g = Graphics.FromImage(i);

Brush myBrush = new SolidBrush(Color.FromArgb(opac, Color.Red));

SizeF sz = g.MeasureString(text, new
Font(FontFamily.GenericSansSerif, 2));

int x;
int y;

x = width / 2;
y = height / 2;

// draw the water mark text
g.DrawString(text, new Font(FontFamily.GenericSansSerif, 7),
myBrush, new Point(x, y));

return new Bitmap(width, height, g);
}
}
 
M

Marc Gravell

Important: refer to the warning about System.Drawing; basically, it
isn't supported for this type of use (I've quoted below).
http://msdn2.microsoft.com/en-us/library/system.drawing.aspx

However, the problem is that you are manipulating "i", then returning
a blank (new) Bitmap; just return i (perhaps cast it if you need). And
perhaps add a few "using" blocks (on the Graphics, etc - ideally the
image would be disposed too, but this would need to be done by the
calling method). The constructor you have used only uses the Graphics
object to get the resolution - not the contents:
http://msdn2.microsoft.com/en-us/library/byca5y1f.aspx

Marc

<quote>
Caution:
Classes within the System.Drawing namespace are not supported for use
within a Windows or ASP.NET service. Attempting to use these classes
from within one of these application types may produce unexpected
problems, such as diminished service performance and run-time
exceptions.
</quote>
 
T

Tem

So you're saying this is the wrong way to do it.?
Is there another way to do what I'm trying to do in asp.net?

BTW I got it to work by doing what you suggested here.
However, the problem is that you are manipulating "i", then returning
a blank (new) Bitmap; just return i (perhaps cast it if you need). And
perhaps add a few "using" blocks (on the Graphics, etc - ideally the
image would be disposed too, but this would need to be done by the
calling method). The constructor you have used only uses the Graphics
object to get the resolution - not the contents:
http://msdn2.microsoft.com/en-us/library/byca5y1f.aspx

I thought .NET was supposed to dispose unused object automatically?
 
M

Marc Gravell

I thought .NET was supposed to dispose unused object automatically?

No; it performs garbage collection, but that isn't the same as
disposal... in short, if your code is responsable for something that
implements IDisposable, then your code should also ideally ensure that
Dispose() is called, commonly via "using".

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top