0

我在我的服务器上的这个文件夹中有一张图片:\images\freemedia\largethumbs\test.png

在我的 default.aspx 页面上,我有一个图像控件:

<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 的一部分提供。

4

1 回答 1

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

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


更新:

在评论之后,这是添加水印的方法:

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
于 2012-12-11T22:28:05.730 回答