C#上传图片以及级图片打水印

本文介绍如何使用ASP.NET实现文件上传功能,并演示了如何对上传的图片添加水印。通过具体的代码示例,展示了从接收文件到保存以及图片处理的完整过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  
        if (!string.IsNullOrEmpty(context.Request.Form["ispostback"]))//不为空,说明是点击上传
        {
            if (context.Request.Files.Count > 0)
            { 
                for(int i=0;i<context.Request.Files.Count;i++)
                {
                    HttpPostedFile file = context.Request.Files[i];
                    file.SaveAs(context.Server.MapPath("upload\\" + file.FileName));
                    context.Response.Write("上传成功");
                       
                }
            }
        }
        else
        {
            string path = context.Server.MapPath("up.htm");
            string strHTML = System.IO.File.ReadAllText(path);
            context.Response.Write(strHTML);
        }
        

ASP.net中对文件的上传非常方便。

如果对图片进行操作,例如打水印;

 context.Response.ContentType="image/jpg";
        string path=context.Server.MapPath("upload\\1.jpg");//转成物理路径
        using (Image img = Bitmap.FromFile(path))//Bitmap图像操作类,可以剪切,缩放,打水印
        {
            using (Graphics g = Graphics.FromImage(img))//从图像上面创建新的画笔
            { 
              g.DrawString("图片水印",new Font("黑体",16),Brushes.Black,new Point(0,0));
                //文字,字体和大小,画笔颜色,位置
                img.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }

        


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值