zoukankan      html  css  js  c++  java
  • 使用ImageProcessor、CodeCarvings.Piczard组件生成缩略图和添加水印

    技术栈:

    1.ImageProcessor(专业图像处理,不能合成水印,NetCore中有它的升级版ImageSharp目前是预览包)

    2.CodeCarvings.Piczard(缩略图,水印都能搞得定)

    代码:

    -----------------------------------------------------------

    #region 图片裁剪(缩略图/等比缩放/固定宽高)
    var path = "E:\test.jpg";
    var outpath = "E:\test_out400X400.jpg";
    var outpath2 = "E:\test_out2_400X400.jpg";
    var waterpath = "E:\watermark.jpg";
    var wateroutpath = "E:\test_out_watermark.jpg";
    var bytes = File.ReadAllBytes(path);
    using (Stream inStream = new MemoryStream(bytes))
    {
    //方式一,CodeCarvings.Piczard
    ImageProcessingJob jobThumb = new ImageProcessingJob();
    jobThumb.Filters.Add(new FixedResizeConstraint(400, 400));
    jobThumb.SaveProcessedImageToFileSystem(inStream, outpath);
    inStream.Position = 0;//指针复位

    //方式二,ImageProcessor
    using (ImageFactory imageFactory = new ImageFactory(true))
    {
    imageFactory.Load(inStream)
    .Resize(new ResizeLayer(new Size(400, 400), ResizeMode.BoxPad, AnchorPosition.Center))
    .BackgroundColor(Color.White)
    .Quality(100)
    .Save(outpath2);
    }
    inStream.Position = 0;//指针复位
    }
    #endregion

    #region 图片水印
    ImageWatermark imgWatermark = new ImageWatermark(waterpath);//水印图片位置
    imgWatermark.ContentAlignment = ContentAlignment.MiddleCenter;//水印位置
    imgWatermark.Alpha = 100;//透明度,需要水印图片是背景透明的png图片
    ImageProcessingJob jobNormal = new ImageProcessingJob();
    jobNormal.Filters.Add(imgWatermark);//添加水印
    //jobNormal.Filters.Add(new FixedResizeConstraint(600, 600));//添加图片缩放,水印可能会不清晰
    jobNormal.SaveProcessedImageToFileSystem(bytes, wateroutpath);
    #endregion

    -----------------------------------------------------------

    参考:https://www.cnblogs.com/pengze0902/p/6569360.html

    https://www.cnblogs.com/viplued/p/9289579.html

    跨平台方案:https://www.cnblogs.com/chenug/p/6655745.html

    http://www.cnblogs.com/AnkerZhang/p/9447815.html

  • 相关阅读:
    Linux系统 虚拟化篇之KVM
    Linux系统 Top命令
    Linux系统 日常优化
    模拟浏览器多文件上传
    51nod 1315 合法整数集 (位操作理解、模拟、进制转换)
    51nod 1138 连续整数的和(等差数列)
    51nod 1042 数字0-9的数量 (数位dp、dfs、前导0)
    51nod 1136 欧拉函数(数论,用定义解题)
    51nod 1106 质数检测(数论)
    51nod 1006 最长公共子序列Lcs(dp+string,无标记数组实现)
  • 原文地址:https://www.cnblogs.com/langhaoabcd/p/10446185.html
Copyright © 2011-2022 走看看