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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....