【问题标题】:Add watermarktext to image dynamically without saving image to server动态添加水印文本到图像而不将图像保存到服务器
【发布时间】:2012-12-11 21:52:21
【问题描述】:

我的服务器上的这个文件夹中存储了一张图像: \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 的一部分提供。

【问题讨论】:

    标签: asp.net image watermark


    【解决方案1】:

    由于您使用的是 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"]


    更新:

    在 cmets 之后,添加水印的方法如下:

    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
    

    【讨论】:

    • 有趣的是,我已经有了一个 http 处理程序。但我不知道如何在提供的图像中添加水印文本。我用我当前的 httphandler 代码更新了我的原始帖子。你知道如何添加水印文字吗?谢谢!
    • 我在这里使用了代码:magic-dev.com/upload-images-watermark.htm 但这会将实际图像保存到磁盘,并且似乎不允许我将水印文本以特定字体大小保存到图像的所需区域,例如字体大小为 16px 的右下角。示例代码在整个图像中重复文本,我也不想要。
    • @Floran 好吧,看起来有很多你不需要的代码。我已经在我的答案中添加了一个示例,说明如何在 VB 中做到这一点。
    • 感谢代码示例!完美运行!我有另一个相关的问题,因为我想在 httphandler 提供的图像上启用 fancybox 灯箱。如果你想看看,就在这里:stackoverflow.com/questions/13850669/…
    • 还有一件事:如何让水印的背景颜色变成透明的灰色?那么一个带有纯白色文本的透明灰色块?再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 2012-12-03
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多