在不将图像保存到服务器的情况下动态添加水印文本

问题描述:

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

I have an image stored on my server in this folder: \images\freemedia\largethumbs\test.png

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

On my default.aspx page I have an imagecontrol:

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

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

When the visitor requests the default.aspx page I want to get the test.png image from the server, add the watermark text "hello world" to it on the right bottom. I DON'T want to save the watermarked image to the server, since I want to save storage and still want to have access to the original image. From the image that is shown to the visitor, he should preferably not be able to derive the original filename, so he should not be allowed to see that the original filename is test.png.

我一直在谷歌上搜索很多,但所有例子都将水印图像保存到磁盘上,这是我不想要的。

I've been searching on Google a lot, but all examples save the watermarked image to disk, which I don't want.

我已经有了一个httphandler:

I already have a 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

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

Does anyone have a code sample on how to do this?

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

If there's another way of doing this, that is fine too. But please show how I can serve the watermarked image as part of the final HTML.

因为您正在使用asp.net ,您也可以使用 HttpHandler

Since you are working with asp.net, you might as well use an HttpHandler.

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

Here's an example, create a new generic handler, let's call it ImageHandler and the code can be as easy as this:

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 参数,如下所示: / p>

Of course you could also send in some QueryString parameters like this:

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

然后只需使用 context.Request.QueryString [filename] 在ImageHandler中。

And then just use context.Request.QueryString["filename"] in the ImageHandler.

更新:

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

After the comments, here's how you can add a watermark:

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