- 引用NuGet :CodeCarvings.Piczard
/// /// 生成缩略图 /// /// 图片宽 /// 图片高 /// 原图 /// 保存路径 private static void CreateThumbnail(int imageWidth, int imageHigh, string sourePath, string savePath) { ImageProcessingJob ipj = new ImageProcessingJob(); ipj.Filters.Add(new FixedResizeConstraint(imageWidth, imageHigh)); // ipj.SaveProcessedImageToFileSystem(sourePath, savePath, new JpegFormatEncoderParams()); } /// /// 生成水印 /// /// 生成后图片宽度 /// 生成后图片高度 /// 水印图 /// 原图*(需要添加水印的图片) /// 生成路径 private static void CreateThumbnail(int? imageWidth, int? imageHigh,string markimage, string sourePath, string savePath) { ImageWatermark mark = new ImageWatermark(markimage); mark.ContentAlignment = System.Drawing.ContentAlignment.BottomRight; mark.Alpha = 50; ImageProcessingJob job = new ImageProcessingJob(); job.Filters.Add(mark); if (!imageWidth.HasValue && !imageHigh.HasValue) { job.Filters.Add(new FixedResizeConstraint(imageWidth.Value, imageHigh.Value)); } job.SaveProcessedImageToFileSystem(sourePath, savePath, new JpegFormatEncoderParams()); }
效果:
生成验证码:
/// /// 生成验证码 /// /// 验证码字符串 /// 图片宽度 /// 图片高度 /// 字体大小 /// 模糊度 /// 保存路径 public static void GetVerificationCode(string verificationCode,int width,int hight,int Fontsize,int degree,string SavePath) { using (MemoryStream ms = ImageFactory.GenerateImage(verificationCode, width, hight, Fontsize, degree)) using (FileStream fs=File.OpenWrite(SavePath)) { ms.CopyTo(fs); } }
来自B站:逆风微笑代码狗
https://www.bilibili.com/video/BV1Wb411G7yk