Watermark/label custom http handler
Watermark/label custom http handler
am 03.01.2008 02:24:50 von 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.NoCa che);
context.Response.BufferOutput = false;
context.Response.WriteFile("/photos/1.jpg");
}
public bool IsReusable {
get {
return false;
}
}
}
Your help is greatly appreciated,
Tem
Re: Watermark/label custom http handler
am 03.01.2008 08:08:37 von sloan
http://www.c-sharpcorner.com/UploadFile/scottlysle/Watermark CS05072007024947AM/WatermarkCS.aspx
http://geekswithblogs.net/aguest/articles/58795.aspx
I think if you merge those 2 articles together, you can get what you want.
"Tem" wrote in message
news:%23o8P6daTIHA.5524@TK2MSFTNGP05.phx.gbl...
>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.NoCa che);
> context.Response.BufferOutput = false;
>
> context.Response.WriteFile("/photos/1.jpg");
>
> }
>
> public bool IsReusable {
> get {
> return false;
> }
> }
> }
>
> Your help is greatly appreciated,
> Tem
Re: Watermark/label custom http handler
am 04.01.2008 09:27:22 von 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.NoCa che);
context.Response.BufferOutput = false;
GenerateText(@"C:\photo1.jpg").Save(context.Response.OutputS tream,
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);
}
}
Re: Watermark/label custom http handler
am 04.01.2008 12:08:04 von 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
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.
Re: Watermark/label custom http handler
am 04.01.2008 19:03:21 von 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?
Re: Watermark/label custom http handler
am 04.01.2008 20:51:31 von 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