CodeCarvings.Piczard 水印和缩略图

安装:install-package CodeCarvings.Piczard

 

public ActionResult UploadPic(int houseId, HttpPostedFileBase file)
{
    HouseDTO house = houseService.GetDTOById(houseId);
    if (house == null)
    {
        //return Json(new AjaxResult { Status = "error", ErrorMsg = "获取不到房源信息" });
        throw new Exception("获取不到房源信息");
    }

    //month月,minute
    string md5 = CommonHelper.CalcMD5(file.InputStream);
    string ext = Path.GetExtension(file.FileName);
    string path = "/upload/house/" + DateTime.Now.ToString("yyyy/MM/dd") + "/" + md5 + ext;// /upload/2017/07/07/afadsfa.jpg
    string thumbPath = "/upload/house/" + DateTime.Now.ToString("yyyy/MM/dd") + "/" + md5 + "_thumb" + ext;
    string fullPath = HttpContext.Server.MapPath("~" + path);//d://22/upload/2017/07/07/afadsfa.jpg
    string thumbFullPath = HttpContext.Server.MapPath("~" + thumbPath);
    new FileInfo(fullPath).Directory.Create();//尝试创建可能不存在的文件夹

    file.InputStream.Position = 0;//指针复位
    //file.SaveAs(fullPath);//SaveAs("d:/1.jpg");
    //缩略图
    ImageProcessingJob jobThumb = new ImageProcessingJob();
    jobThumb.Filters.Add(new FixedResizeConstraint(200, 200));//缩略图尺寸200*200
    jobThumb.SaveProcessedImageToFileSystem(file.InputStream, thumbFullPath);

    file.InputStream.Position = 0;//指针复位

    //水印
    ImageWatermark imgWatermark = new ImageWatermark(HttpContext.Server.MapPath("~/Areas/WebAdmin/images/logo.png"));
    imgWatermark.ContentAlignment = System.Drawing.ContentAlignment.BottomRight;//水印位置
    imgWatermark.Alpha = 50;//透明度,需要水印图片是背景透明的png图片
    ImageProcessingJob jobNormal = new ImageProcessingJob();
    jobNormal.Filters.Add(imgWatermark);//添加水印
    jobNormal.Filters.Add(new FixedResizeConstraint(600, 600));
    jobNormal.SaveProcessedImageToFileSystem(file.InputStream, fullPath);

    housePicService.Add(new HousePicDTO { HouseId = houseId, Url = path, ThumbUrl = thumbPath });

    CreateStaticPage(houseId);//上传了新图片或者删除图片都要重新生成html页面

    return Json(new AjaxResult
    {
        Status = "ok"
    });
}

 

 

 

参考:

.Net图片加水印及缩略图开发包

 

其他组件:

开源免费的.NET图像即时处理的组件ImageProcessor

转载于:https://www.cnblogs.com/linyongqin/articles/9443627.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值