static void Main(string[] args) { // 1. Simple image resize: Simplicode.Imaging.ImageProcessor processor = new Imaging.ImageProcessor(); var resizer = new ResizeFilter(400, 400, ResizeMethod.KeepAspectRatio); processor.AddFilter(resizer); processor.ProcessImage("testimage.png", "testimage_resized.png"); // 2. Image conversion processor = new Imaging.ImageProcessor(); //processor.OutputFormat = Imaging.OutputFormat.Jpeg; processor.ProcessImage("testimage.png", "testimage_converted.jpg"); // 2.1 Image processing using streams processor = new Imaging.ImageProcessor(); processor.OutputFormat = Imaging.ImageFormatType.Jpeg; processor.JpegCompression = 90L; using (var inputStream = new FileStream("testimage.png", FileMode.Open)) { using (var outputStream = new FileStream("testimage_converted_stream.jpg", FileMode.Create)) { processor.ProcessImage(inputStream, outputStream); } } // 2.2 GIF support processor = new Imaging.ImageProcessor(); processor.GifColours = 128; processor.OutputFormat = Imaging.ImageFormatType.Gif; processor.ProcessImage("testimage.png", "testimage.gif"); // 3. Cropping processor = new Imaging.ImageProcessor(); processor.Filters.Add(new ResizeFilter(400, 300, ResizeMethod.Crop, AnchorLocation.TopMiddle)); processor.ProcessImage("testimage.png", "testimage_cropped_top.png"); // 4. Edge detection processor = new Imaging.ImageProcessor(new EdgeDetectionFilter(10, Color.Red, Color.Yellow)); processor.ProcessImage("testimage.png", "testimage_edge.png"); // 5. Chaining processor = new Imaging.ImageProcessor(); processor.Filters.Add(new EdgeDetectionFilter(7)); processor.Filters.Add(new ResizeFilter(200, 200)); processor.ProcessImage("testimage.png", "testimage_chained.jpg"); // 6. Quality changing processor = new Imaging.ImageProcessor(); resizer = new ResizeFilter(400, 400); resizer.InterpoliationMode = System.Drawing.Drawing2D.InterpolationMode.Default; resizer.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.Default; processor.Filters.Add(resizer); processor.ProcessImage("testimage.png", "testimage_lowquality.jpg"); // 7. Aspect Ratio Fill processor = new Imaging.ImageProcessor(); resizer = new ResizeFilter(200, 50, ResizeMethod.AspectRatioFill); processor.AddFilter(resizer); processor.ProcessImage("testimage.png", "testimage_fill.png"); // 8. Image watermark processor = new Imaging.ImageProcessor(); var watermark = new ImageWatermarkFilter("watermark.png", AnchorLocation.BottomRight); watermark.Offset = new Point(-20, -20); watermark.Opacity = .3f; processor.AddFilter(watermark); processor.ProcessImage("testimage.png", "testimage_image_watermarked.png"); // 9. Text watermark processor = new Imaging.ImageProcessor(); var textWatermark = new TextWatermarkFilter("Copyright (C) Some Company 2011") { FontStyle = FontStyle.Bold, FontName = "Arial", FontSize = 24, Offset = new Point(-20, -20) }; textWatermark.Opacity = .5f; processor.AddFilter(textWatermark); processor.ProcessImage("testimage.png", "testimage_text_watermarked.png"); }
/// <summary> /// 创建附件 /// </summary> /// <param name="attachment">附件</param> /// <param name="contentStream">文件流</param> public void Create(T attachment, Stream contentStream) { if (contentStream == null) { return; } if (attachment.MediaType == MediaType.Image) { //检查是否需要缩放原图 if (TenantAttachmentSettings.MaxImageWidth > 0 || TenantAttachmentSettings.MaxImageHeight > 0) { int maxWidth = TenantAttachmentSettings.MaxImageWidth > 0 ? TenantAttachmentSettings.MaxImageWidth : attachment.Width; int maxHeight = TenantAttachmentSettings.MaxImageHeight > 0 ? TenantAttachmentSettings.MaxImageHeight : attachment.Height; if (attachment.Width > maxWidth || attachment.Height > maxHeight) { Stream resizedStream = ImageProcessor.Resize(contentStream, maxWidth, maxHeight, ResizeMethod.KeepAspectRatio); if (resizedStream != contentStream) { contentStream.Dispose(); } contentStream = resizedStream; } } IAttachmentSettingsManager attachmentSettingsManager = DIContainer.Resolve <IAttachmentSettingsManager>(); AttachmentSettings attachmentSettings = attachmentSettingsManager.Get(); Image image = Image.FromStream(contentStream); bool isGif = ImageProcessor.IsGIFAnimation(image); //检查是否需要打水印 if (!isGif && TenantAttachmentSettings.EnableWatermark && attachmentSettings.WatermarkSettings.WatermarkType != WatermarkType.None && image.Width >= attachmentSettings.WatermarkSettings.WatermarkMinWidth && image.Height >= attachmentSettings.WatermarkSettings.WatermarkMinHeight) { ImageProcessor imageProcessor = new ImageProcessor(); if (attachmentSettings.WatermarkSettings.WatermarkType == WatermarkType.Text) { TextWatermarkFilter watermarkFilter = new TextWatermarkFilter(attachmentSettings.WatermarkSettings.WatermarkText, attachmentSettings.WatermarkSettings.WatermarkLocation, attachmentSettings.WatermarkSettings.WatermarkOpacity); imageProcessor.Filters.Add(watermarkFilter); } else if (attachmentSettings.WatermarkSettings.WatermarkType == WatermarkType.Image) { ImageWatermarkFilter watermarkFilter = new ImageWatermarkFilter(attachmentSettings.WatermarkSettings.WatermarkImagePhysicalPath, attachmentSettings.WatermarkSettings.WatermarkLocation, attachmentSettings.WatermarkSettings.WatermarkOpacity); imageProcessor.Filters.Add(watermarkFilter); } //如果需要添加水印,则除水印图片以外,还需要保留原图 StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), attachment.GetOriginalFileName(), contentStream); //给原始尺寸的图添加水印 using (Stream watermarkImageStream = imageProcessor.Process(contentStream)) { StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), attachment.FileName, watermarkImageStream); } //根据设置生成不同尺寸的图片,并添加水印 if (TenantAttachmentSettings.ImageSizeTypes != null && TenantAttachmentSettings.ImageSizeTypes.Count > 0) { foreach (var imageSizeType in TenantAttachmentSettings.ImageSizeTypes) { Stream resizedStream = ImageProcessor.Resize(contentStream, imageSizeType.Size.Width, imageSizeType.Size.Height, imageSizeType.ResizeMethod); image = Image.FromStream(resizedStream); if (image.Width >= attachmentSettings.WatermarkSettings.WatermarkMinWidth && image.Height >= attachmentSettings.WatermarkSettings.WatermarkMinHeight) { using (Stream watermarkImageStream = imageProcessor.Process(resizedStream)) { StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), StoreProvider.GetSizeImageName(attachment.FileName, imageSizeType.Size, imageSizeType.ResizeMethod), watermarkImageStream); } } else { StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), StoreProvider.GetSizeImageName(attachment.FileName, imageSizeType.Size, imageSizeType.ResizeMethod), resizedStream); } if (resizedStream != contentStream) { resizedStream.Dispose(); } } } } else { StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), attachment.FileName, contentStream); if (!isGif) { //根据设置生成不同尺寸的图片 if (TenantAttachmentSettings.ImageSizeTypes != null && TenantAttachmentSettings.ImageSizeTypes.Count > 0) { foreach (var imageSizeType in TenantAttachmentSettings.ImageSizeTypes) { Stream resizedStream = ImageProcessor.Resize(contentStream, imageSizeType.Size.Width, imageSizeType.Size.Height, imageSizeType.ResizeMethod); StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), StoreProvider.GetSizeImageName(attachment.FileName, imageSizeType.Size, imageSizeType.ResizeMethod), resizedStream); if (resizedStream != contentStream) { resizedStream.Dispose(); } } } } } } else { StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), attachment.FileName, contentStream); } if (contentStream != null) { contentStream.Dispose(); } EventBus <T> .Instance().OnBefore(attachment, new CommonEventArgs(EventOperationType.Instance().Create())); attachmentRepository.Insert(attachment); EventBus <T> .Instance().OnAfter(attachment, new CommonEventArgs(EventOperationType.Instance().Create())); }