微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ASP.NET:动态地为图像添加“水印”

我见过很多关于 adding watermark on images with php的问题和答案

我想这样做,这次是用ASP.NET

所以这里有几个问题.

>我怎么能用ASP做到这一点?
>这个过程对服务器来说是否会过载?
>我可以使用图像作为水印而不是简单的文字吗?

解决方法

这是来自codeproject的另一个示例 http://www.codeproject.com/KB/web-image/ASPImaging1.aspx,您可以对图像做很多想法,包括从图像添加水印.

我认为这个过程是采取cpu power ether在PHP,ether.net上的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,OutWidth,OutHeight);

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

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

                g.DrawImage(WaterMark,destWaterRect,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();
        }
    }

如果您希望自定义句柄,则上述代码为stand,但您只更改了保存行.就像是.

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();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐