加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 06:29:07 所属栏目:asp.Net 来源:网络整理
导读:我见过很多关于 adding watermark on images with php的问题和答案 我想这样做,这次是用ASP.NET 所以这里有几个问题. 我怎么能用ASP做到这一点? 这个过程对服务器来说是否会过载? 我可以使用图像作为水印而不是简单的文字吗? 解决方法 这是来自codeprojec
我见过很多关于 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();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读