ASP.NET:动态添加 'Watermark' 到图像

标签 asp.net image image-processing watermark

我看到了有关 adding watermark on images with php 的精彩问题和答案

我也想用 ASP.NET 做同样的事情

这里有几个问题。

  1. 如何使用 ASP 做到这一点?
  2. 此过程是否会导致服务器严重重载?
  3. 我可以使用图像代替简单的文本作为水印吗?

最佳答案

这是另一个示例 http://www.codeproject.com/KB/web-image/ASPImaging1.aspx从 codeproject 可以看出,您可以对图像进行很多思考,包括从图像添加水印。

我认为这个过程需要CPU电源,以太在php上,以太在asp.net上。因此,图像缓存模式对于此类工作来说是必须的。

这是一些基本代码。在此代码中,您必须更改水印的位置和图像的大小。水印可以是透明的png图片。

    public void MakePhoto(...parametres...)
    {
        Bitmap outputImage = null;
        Graphics g = null;

        try
        {                
            // the final image
            outputImage = new Bitmap(OutWidth, OutHeight, PixelFormat.Format24bppRgb);

            g = Graphics.FromImage(outputImage);
            g.CompositingMode = CompositingMode.SourceCopy;
            Rectangle destRect = new Rectangle(0, 0, OutWidth, OutHeight);

            // the photo
            using (var BasicPhoto = new Bitmap(cBasicPhotoFileOnDisk))
            {
                g.DrawImage(BasicPhoto, destRect, 0, 0, BasicPhoto.Width, BasicPhoto.Height, GraphicsUnit.Pixel);
            }

            g.CompositingMode = CompositingMode.SourceOver;
            // the watermark
            using (var WaterMark = new Bitmap(cWaterMarkPhotoOnDisk))
            {
                Rectangle destWaterRect = new Rectangle(0, 0, OutWidth, OutHeight);

                g.DrawImage(WaterMark, destWaterRect, 0, 0, OutWidth, OutHeight, GraphicsUnit.Pixel);
            }

            outputImage.Save(TheFileNameTosaveIt, ImageFormat.Jpeg);

        }
        catch (Exception x)
        {
            Debug.Assert(false);
            ... log your error, and send an error image....                
        }
        finally
        {
            if (outputImage != null)
                outputImage.Dispose();

            if (g != null)
                g.Dispose();
        }
    }

如果您想制作自定义句柄,则上面的代码是有效的,但您只需更改保存行。类似的东西。

public void ProcessRequest (HttpContext context)    
{
    context.Response.ContentType = "image/jpeg";

    // add you cache here
    context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(200));
    context.Response.Cache.SetMaxAge(new TimeSpan(0, 200, 0));
    context.Response.BufferOutput = false;


    ..... the above code....
    outputImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
    ..... the above code....


    context.Response.End();
}

关于ASP.NET:动态添加 'Watermark' 到图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5457691/

相关文章:

asp.net - 如何使 ASP.NET Webforms 应用程序可测试?

apache-flex - Flex 4 文件引用选定的图像文件尺寸(宽度和高度)

css - 如何用CSS使图像完全变灰?

ios - 检查封闭路径内所有点的方法

java - 用于拍卖网站的框架的性能比较

asp.net - 生成文件,然后启动安全下载

image - HTML5 缓存 list : whitelisting ALL remote resources?

python - 拼接时修剪图像

c# - 流式传输到图像并返回

c# - 当我没有实例化 "Response.Redirect"时,如何从 Razor 网页使用 "Response"?