public static void CreateThumbnail(string imageFilePath, string thumbnailFilePath, System.Drawing.Size size, bool removeTransparency) { if (string.IsNullOrEmpty(imageFilePath)) { throw new ArgumentNullException("imageFilePath"); } if (string.IsNullOrEmpty(thumbnailFilePath)) { throw new ArgumentNullException("thumbnailFilePath"); } if (size.IsEmpty) { throw new ArgumentException("Thumbnail size can not be empty", "size"); } if (File.Exists(imageFilePath)) { var pipeline = new Pipeline(); var reader = ImageReader.Create(imageFilePath); pipeline.Add(reader); var resizer = new Resize(size.Width, size.Height) { InterpolationMode = ResizeInterpolationMode.High, ResizeMode = ResizeMode.Fit }; pipeline.Add(resizer); if (removeTransparency && reader.PixelFormat.HasAlpha) { var cc = new ColorConverter(PixelFormat.DiscardAlpha(reader.PixelFormat)); cc.BackgroundColor = cc.ConvertColor(new RgbColor(255, 255, 255)); pipeline.Add(cc); } var writer = ImageWriter.Create(thumbnailFilePath); if (writer is GifWriter) { ColorPalette palette = null; using (var bitmap = reader.Frames[0].GetBitmap()) palette = ColorPalette.Create(bitmap); pipeline.Add(new ColorConverter(PixelFormat.Format8bppIndexed) { Palette = palette }); } pipeline.Add(writer); pipeline.Run(); pipeline.DisposeAllElements(); } }
private Pipeline CreateTilePipeline(Rectangle boundsOnResizedImage) { var tilePipeline = new Pipeline(); var fileCache = FileCache.GetInstance(); var fileName = _filePrefix + "_" + _tiles.Count + _fileExtension; fileCache.RegisterPublicTempFileName(fileName, false); tilePipeline.Add(new Crop(boundsOnResizedImage)); if ((_bitmapViewer.SourceImageParams.ColorProfile != null && _bitmapViewer.SourceImageParams.ColorProfile.ColorSpace == ColorSpace.Cmyk) || _bitmapViewer.SourceImageParams.PixelFormat.HasAlpha) { var cc = new Transforms.ColorConverter(PixelFormat.Format24bppRgb) { ColorManagementEngine = ColorManagementEngine.LittleCms, DestinationProfile = ColorProfile.FromSrgb() }; tilePipeline.Add(cc); } tilePipeline.Add(ImageWriter.Create(fileCache.GetAbsolutePublicCachePath(fileName), _encoderOptions)); return(tilePipeline); }
/// <summary> /// Adjusts brightness modifying lightness channel of Lab color space /// </summary> private static void AdjustLightnessLab() { using (var lChannel = new Bitmap()) using (var aChannel = new Bitmap()) using (var bChannel = new Bitmap()) { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var labConverter = new ColorConverter(PixelFormat.Format24bppLab)) using (var splitter = new LabChannelSplitter()) { splitter.L = lChannel; splitter.A = aChannel; splitter.B = bChannel; Pipeline.Run(reader + labConverter + splitter); } lChannel.ColorAdjustment.Brightness(brightnessAmount); using (var combiner = new LabChannelCombiner()) using (var rgbConverter = new ColorConverter(PixelFormat.Format24bppRgb)) using (var writer = ImageWriter.Create("../../../../_Output/AdjustLightnessLab.jpg")) { combiner.L = lChannel; combiner.A = aChannel; combiner.B = bChannel; rgbConverter.DestinationProfile = ColorProfile.FromSrgb(); Pipeline.Run(combiner + rgbConverter + writer); } } }
internal void WriteThumbnail(Stream stream, IResizeOptions options) { if (stream == null) { throw new ArgumentNullException("stream"); } if (options == null) { throw new ArgumentNullException("options"); } Pipeline pipeline = null; try { using (var readStream = options.GetSourceImageReadStream()) using (var reader = ImageReader.Create(readStream)) { IImageParams firstElement; var psdReader = reader as PsdReader; if (psdReader != null) { if (psdReader.MergedImageFrame == null) { throw new MediaUnsupportedException("Cannot read merged image frame from from PSD."); } firstElement = psdReader.MergedImageFrame; } else { firstElement = reader; } pipeline = new Pipeline((PipelineElement)firstElement); if (firstElement.Width != options.Size.Width || firstElement.Height != options.Size.Height) { pipeline.Add(new Resize(options.Size, ResizeInterpolationMode.Anisotropic9, options.ResizeMode)); } var cc = ColorManagement.GetColorConverter(options.ColorManagement, firstElement, options.PixelFormat, true); if (cc != null) { pipeline.Add(cc); } pipeline.Add(ImageWriter.Create(stream, Common.GetWriterSettings(options.FileFormat))); pipeline.Run(); } } finally { if (pipeline != null) { pipeline.DisposeAllElements(); } } }
public void LoadImage(PipelineElement reader, float opacity = 1, FileFormat fileFormat = FileFormat.Tiff, bool actualSize = false, bool preserveAspectRatio = false) { string storageId; using (var stream = new MemoryStream()) using (var writer = ImageWriter.Create(fileFormat, stream)) { if (writer is TiffWriter) { (writer as TiffWriter).Compression = CompressionType.Zip; } var pipeline = new Pipeline(reader); if (!Utils.EqualsOfFloatNumbers(opacity, 1f)) { var scaleAlpha = new ScaleAlpha(opacity); pipeline.Add(scaleAlpha); } pipeline.Add(writer); pipeline.Run(); pipeline.Remove(reader); pipeline.DisposeAllElements(); stream.Position = 0; storageId = Configuration.FileCache.AddFile(Common.GetImageExtension(fileFormat), stream, true); } LoadImage(storageId, actualSize, preserveAspectRatio); }
private static void TrackProgressAndCancel() { Console.WriteLine("ProgressAndCancel Sample\n"); Console.WriteLine("Press enter to start pipeline processing "); Console.WriteLine("After start press \"c\" to cancel pipeline processing."); Console.Read(); var cancelTokenSource = new CancellationTokenSource(); var cancelToken = cancelTokenSource.Token; var task = Task.Factory.StartNew(() => { // reader ---> resize ---> progress ---> writer using (var reader = ImageReader.Create("../../../../_Input/Venice.jpg")) using (var resize = new Resize(16000, 0, ResizeInterpolationMode.High)) using (var progress = new ProgressReporter()) using (var writer = ImageWriter.Create("../../../../_Output/TrackProgressAndCancel.jpg")) { progress.Progress += (s, e) => { Console.WriteLine("{0:F2}%", e.Progress); if (cancelToken.IsCancellationRequested == true) { Console.WriteLine("Pipeline processing was cancelled"); Console.WriteLine("\nPress \"x\" to quit"); cancelToken.ThrowIfCancellationRequested(); } }; Console.WriteLine("Start cancellable pipeline processing...\n"); Pipeline.Run(reader + resize + progress + writer); Console.WriteLine("\nEnd cancellable pipeline processing..."); Console.WriteLine("\nPress \"x\" to quit"); } }, cancelToken); var flag = true; while (flag) { switch (Console.ReadKey().KeyChar) { case 'c': cancelTokenSource.Cancel(); Console.WriteLine("Task cancellation requested."); break; case 'x': flag = false; break; } } }
/// <summary> /// Loads and saves image to file using memory-friendly Pipeline API /// </summary> private static void LoadSaveFileMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var writer = ImageWriter.Create("../../../../_Output/LoadSaveFileMemoryFriendly.jpg")) { Pipeline.Run(reader + writer); } }
/// <summary> /// Flips image using memory-friendly Pipeline API /// </summary> private static void FlipMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var flip = new Flip(FlipType.Vertical)) using (var writer = ImageWriter.Create("../../../../_Output/FlipMemoryFriendly.jpg")) { Pipeline.Run(reader + flip + writer); } }
/// <summary> /// Rotates image on arbitrary angle using memory-friendly Pipeline API /// </summary> private static void RotateArbitraryMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var rotate = new Rotate(15, RgbColor.Yellow, InterpolationMode.High)) using (var writer = ImageWriter.Create("../../../../_Output/RotateArbitraryMemoryFriendly.jpg")) { Pipeline.Run(reader + rotate + writer); } }
/// <summary> /// Desaturates image using memory-friendly Pipeline API /// </summary> private static void DesaturateMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var desaturate = new Desaturate()) using (var writer = ImageWriter.Create("../../../../_Output/DesaturateMemoryFriendly.jpg")) { Pipeline.Run(reader + desaturate + writer); } }
/// <summary> /// Adjusts HSL (Hue-Saturation-Lightness) balance using memory-friendly Pipeline API /// </summary> private static void AdjustHslMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var adjustHsl = new AdjustHsl(0.2f, -0.1f, 0.1f)) using (var writer = ImageWriter.Create("../../../../_Output/AdjustHslMemoryFriendly.jpg")) { Pipeline.Run(reader + adjustHsl + writer); } }
/// <summary> /// Crops image using memory-friendly Pipeline API /// </summary> private static void Crop() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var crop = new Crop(200, 150, 360, 270)) using (var writer = ImageWriter.Create("../../../../_Output/PipelineCrop1.jpg")) { Pipeline.Run(reader + crop + writer); } }
/// <summary> /// Warps image using memory-friendly Pipeline API /// </summary> private static void BezierMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var bezier = new BezierTransform(ControlPoints, RgbColor.Transparent, InterpolationMode.High)) using (var writer = ImageWriter.Create("../../../../_Output/BezierMemoryFriendly.jpg")) { Pipeline.Run(reader + bezier + writer); } }
/// <summary> /// Converts color space from CMYK to RGB without color management using memory-friendly Pipeline API /// </summary> private static void CmykToRgbNoColorManagementMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Copenhagen_CMYK.jpg")) using (var converter = new ColorConverter(PixelFormat.Format24bppRgb)) using (var writer = ImageWriter.Create("../../../../_Output/CmykToRgbNoColorManagementMemoryFriendly.jpg")) { Pipeline.Run(reader + converter + writer); } }
/// <summary> /// Converts to 16-bit precision per channel using memory-friendly Pipeline API /// </summary> private static void ConvertToExtendedMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Copenhagen_RGB.jpg")) using (var converter = new ColorConverter(PixelFormat.Format48bppRgb)) using (var writer = ImageWriter.Create("../../../../_Output/PF_ConvertToExtendedMemoryFriendly.png")) { Pipeline.Run(reader + converter + writer); } }
/// <summary> /// Applies median filter using memory-friendly Pipeline API /// </summary> private static void MedianMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var median = new Median(3)) using (var writer = ImageWriter.Create("../../../../_Output/MedianMemoryFriendly.jpg")) { Pipeline.Run(reader + median + writer); } }
/// <summary> /// Applies maximum filter using memory-friendly Pipeline API /// </summary> private static void MaximumMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var maximum = new Maximum(3)) using (var writer = ImageWriter.Create("../../../../_Output/MaximumMemoryFriendly.jpg")) { Pipeline.Run(reader + maximum + writer); } }
/// <summary> /// Adjusts contrast using memory-friendly Pipeline API /// </summary> private static void BrightnessContrastMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var brightnessContrast = new BrightnessContrast(0.1f, 0.2f)) using (var writer = ImageWriter.Create("../../../../_Output/BrightnessContrastMemoryFriendly.jpg")) { Pipeline.Run(reader + brightnessContrast + writer); } }
/// <summary> /// Crops image using memory-friendly Pipeline API /// </summary> private static void CropMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var crop = new Crop(230, 170, 320, 240)) using (var writer = ImageWriter.Create("../../../../_Output/CropMemoryFriendly.jpg")) { Pipeline.Run(reader + crop + writer); } }
/// <summary> /// Resizes image using memory-friendly Pipeline API /// </summary> private static void Resize() { using (var reader = ImageReader.Create("../../../../_Input/Venice.jpg")) using (var resize = new Resize(1048, 0, ResizeInterpolationMode.High)) using (var writer = ImageWriter.Create("../../../../_Output/PipelineResize.jpg")) { Pipeline.Run(reader + resize + writer); } }
/// <summary> /// Rotates image on 90, 180, 270 degrees image using memory-friendly Pipeline API /// </summary> private static void RotateMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var rotate = new Rotate(90)) using (var writer = ImageWriter.Create("../../../../_Output/RotateMemoryFriendly.jpg")) { Pipeline.Run(reader + rotate + writer); } }
/// <summary> /// Loads and saves image to file with specified encoder options using alternative syntax and memory-friendly Pipeline API /// </summary> private static void LoadSaveFileAltSyntaxMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) // Set value of JPEG quality to 85 using (var writer = ImageWriter.Create("../../../../_Output/LoadSaveFileAltSyntaxMemoryFriendly.jpg", new JpegSettings(85))) { Pipeline.Run(reader + writer); } }
/// <summary> /// Removes alpha channel using memory-friendly Pipeline API /// </summary> private static void RemoveAlphaChannelMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Stamp.png")) using (var removeAlpha = new RemoveAlpha()) using (var writer = ImageWriter.Create("../../../../_Output/RemoveAlphaChannelMemoryFriendly.png")) { Pipeline.Run(reader + removeAlpha + writer); } }
/// <summary> /// Resizes image using memory-friendly Pipeline API /// </summary> private static void ResizeMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var resize = new Resize(320, 0, ResizeInterpolationMode.High)) using (var writer = ImageWriter.Create("../../../../_Output/ResizeMemoryFriendly.jpg")) { Pipeline.Run(reader + resize + writer); } }
/// <summary> /// Adjusts brightness modifying all channels of RGB color space /// </summary> private static void AdjustLightnessRgb() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var brightness = new Brightness(brightnessAmount)) using (var writer = ImageWriter.Create("../../../../_Output/AdjustLightnessRgb.jpg")) { Pipeline.Run(reader + brightness + writer); } }
/// <summary> /// Resizes and preserves embedded profile on saving /// </summary> private static void RetainEmbeddedProfileMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Copenhagen_CMYK.jpg")) using (var resize = new Resize(640, 0, ResizeInterpolationMode.High)) using (var writer = ImageWriter.Create("../../../../_Output/RetainEmbeddedProfileMemoryFriendly.jpg")) { // Color profile is automatically retained from reading to writing Pipeline.Run(reader + resize + writer); } }
/// <summary> /// Merges PSD layers /// </summary> private static void MergeAndResize() { using (var psdReader = new PsdReader("../../../../_Input/BusinessCard.psd")) using (var resize = new Resize(400, 0, ResizeInterpolationMode.High)) using (var writer = ImageWriter.Create("../../../../_Output/BusinessCard_Resize.png")) { resize.Receivers.Add(writer); psdReader.MergeLayers(resize); } }
/// <summary> /// Converts to indexed pixel format (optimized for Web) /// </summary> private static void ConvertToIndexedMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Copenhagen_RGB.jpg")) using (var converter = new ColorConverter(PixelFormat.Format8bppIndexed)) using (var writer = ImageWriter.Create("../../../../_Output/PF_ConvertToIndexedMemoryFriendly.png")) { converter.Dithering = DitheringType.FloydSteinberg; Pipeline.Run(reader + converter + writer); } }
/// <summary> /// Sets alpha values of pixels with a given color to transparent using memory-friendly Pipeline API /// </summary> private static void TransparentizeMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var transparentize = new Transparentize()) using (var writer = ImageWriter.Create("../../../../_Output/TransparentizeMemoryFriendly.png")) { transparentize.BackgroundColor = new RgbColor(0x8a, 0xb9, 0xed); transparentize.Threshold = 0.1f; Pipeline.Run(reader + transparentize + writer); } }
/// <summary> /// Converts color space from CMYK (no assigned color profile) to RGB with color management /// using memory-friendly Pipeline API /// </summary> private static void CmykNoProfileToRgbWithColorManagementMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Copenhagen_CMYK.jpg")) using (var converter = new ColorConverter(PixelFormat.Format24bppRgb)) using (var writer = ImageWriter.Create("../../../../_Output/CmykNoProfileToRgbWithColorManagementMemoryFriendly.jpg")) { converter.DefaultSourceProfile = new ColorProfile("../../../../_Input/ColorProfiles/ISOcoated_v2_eci.icc"); converter.DestinationProfile = ColorProfile.FromSrgb(); Pipeline.Run(reader + converter + writer); } }