GDI+之水印图片和缩略图

1、水印

1.1简单水印:

  • html:
    <form method="post" action="Upload.ashx" enctype="multipart/form-data">
        <input type="file" name="f"/>
        <br />
        <input type="submit" value="上传" />
    </form>
  • ahsx:
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            HttpPostedFile f = context.Request.Files["f"];
            //根据文件输入流,创建画布。
            Bitmap bitmap = new Bitmap(f.InputStream);

            //获取绘制工具
            Graphics g = Graphics.FromImage(bitmap);

            //读取水印文件
            Bitmap water = new Bitmap(AppDomain.CurrentDomain.BaseDirectory + "Uploads/logo.jpg");

            //绘制
            g.DrawImage(water, bitmap.Width - water.Width, bitmap.Height - water.Height, water.Width, water.Height);

            //保存
            bitmap.Save(AppDomain.CurrentDomain.BaseDirectory + "Uploads/" + f.FileName);

        }

1.2使用封装的Imageclass

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            HttpPostedFile img = context.Request.Files["img"];
            string waterImgSrc = AppDomain.CurrentDomain.BaseDirectory + "Uploads\\logo.jpg";
            string savePath = AppDomain.CurrentDomain.BaseDirectory + "Uploads/big/";
            img.SaveAs(savePath + img.FileName);

            ImageClass.ImageWatermark(savePath + img.FileName, waterImgSrc, "");
            context.Response.Write("ok");
        }

2、缩略图

2.1简单缩略图:

  • html:
    <form method="post" action="Thumbnail.ashx" enctype="multipart/form-data">
        <input type="file" name="img" />
        <br />
        <input type="submit" value="保存" />
    </form>
  • ashx:
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            HttpPostedFile img = context.Request.Files["img"];
            string path = AppDomain.CurrentDomain.BaseDirectory + "Uploads";
            img.SaveAs(path + "/big/" + img.FileName);

            Bitmap big = new Bitmap(img.InputStream);
            Bitmap small = new Bitmap((int)(big.Width * 0.1), (int)(big.Height * 0.1));

            Graphics g = Graphics.FromImage(small);
            g.DrawImage(big, 0, 0, small.Width, small.Height);
            small.Save(path + "/small/" + img.FileName,ImageFormat.Jpeg);
            context.Response.Write("ok");
        }

2.2使用封装好的Imageclass

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            HttpPostedFile img = context.Request.Files["img"];
            string pathBigimg = AppDomain.CurrentDomain.BaseDirectory + "Uploads\\big\\";
            string pathSmallimg = AppDomain.CurrentDomain.BaseDirectory + "Uploads\\small\\";

            img.SaveAs(pathBigimg + img.FileName);

            ImageClass.MakeThumbnail(pathBigimg + img.FileName, pathSmallimg+img.FileName, 300, 200, "Cut");
            context.Response.Write("原图:<br /><img src='../Uploads/big/"+img.FileName+ "'/><br />缩略图:<br /><img src='../Uploads/small/" +img.FileName+"'>");
        }

附:ImageClass

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值