将watermarktext动态添加到图像而不将图像保存到服务器

时间:2012-12-11 21:52:21

标签: asp.net image watermark

我的服务器中存有一个存储在此文件夹中的图像: \图像\ freemedia \ largethumbs \ test.png

在我的default.aspx页面上,我有一个imagecontrol:

<asp:Image ID="Image1" runat="server" />

当访问者请求default.aspx页面时,我想从服务器获取test.png图像,在右下角添加水印文本“hello world”。 我不想将带水印的图像保存到服务器,因为我想节省存储空间并且仍然希望能够访问原始图像。 从显示给访问者的图像中,他最好不能导出原始文件名,因此不应该让他看到原始文件名是test.png。

我一直在Google上搜索,但所有示例都将带水印的图像保存到磁盘上,这是我不想要的。

我已经有了一个httphandler:

Public Class pichandler : Implements IHttpHandler

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    Dim data As Byte()
    Dim fName As String 

    Using w As New generaltablesTableAdapters.freemediaTableAdapter
        fName = w.GetDataById(i)(0).medialink.ToString
    End Using
    data = My.Computer.FileSystem.ReadAllBytes(context.Server.MapPath("~/images/freemedia/thumbs/" & fName))

    ' --> how can I add a watermark text to the image here?!?!?!?

    context.Response.ContentType = "image/jpeg"
    context.Response.BinaryWrite(data)
End Sub


End Class

有没有人有关于如何执行此操作的代码示例?

如果有另一种方法可以做到这一点,那也没关系。但请说明如何将水印图像作为最终HTML的一部分提供。

1 个答案:

答案 0 :(得分:3)

由于您正在使用asp.net,因此您可以使用HttpHandler

这是一个示例,创建一个新的通用处理程序,让我们称之为 ImageHandler ,代码可以像这样简单:

public class ImageHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        // Clear response and set content type to image.
        context.Response.Clear();
        context.Response.ContentType = "image/jpeg";

        // Create your image, however you want it. Server.MapPath and so on.
        var image = Bitmap.FromFile(@"C:\Images\image.jpg");

        // And save it to the OutputStream.
        image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
    }

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

然后就这样使用它:

<asp:Image ID="Image1" ImageUrl="~/ImageHandler.ashx" runat="server" />

当然你也可以发送一些 QueryString 参数,如下所示:

<asp:Image ID="Image1" ImageUrl="~/ImageHandler.ashx?filename=myimage.jpg" runat="server" />

然后在ImageHandler中使用context.Request.QueryString["filename"]


<强>更新

评论之后,以下是添加水印的方法:

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    Dim watermarkText As String = "Copyright"
    Dim fName As String 

    Using w As New TopTrouwen.generaltablesTableAdapters.freemediaTableAdapter
        fName = w.GetDataById(i)(0).medialink.ToString
    End Using

    ' Create image directly from the path
    Dim image = Drawing.Image.FromFile(context.Server.MapPath("~/images/freemedia/thumbs/" & fName))
    Dim font As New Font("Tahoma", 16, FontStyle.Regular, GraphicsUnit.Pixel)

    'Adds a transparent watermark 
    Dim color As Color = Drawing.Color.FromArgb(50, 0, 0, 0)
    Dim brush As New SolidBrush(color)
    Dim gr As Graphics = Graphics.FromImage(image)

    ' Measure the watermark text so we can offset it's width and height
    Dim watermarkSize = gr.MeasureString(watermarkText, font)

    ' Create the point where the watermark text should be printed
    Dim point As New Point(image.Width - watermarkSize.Width, image.Height - watermarkSize.Height)

    gr.DrawString(watermarkText, font, brush, point)
    gr.Dispose()

    context.Response.ContentType = "image/jpeg"
    image.Save(context.Response.OutputStream, ImageFormat.Jpeg)
End Sub