使用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

posted @ 2019-02-27 19:43  胡椒面  阅读(676)  评论(0编辑  收藏  举报