/// <summary> /// Converts to black & white pixel format using memory-friendly Pipeline API /// </summary> private static void ConvertToBlackAndWhiteMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Copenhagen_RGB.jpg")) using (var converter = new ColorConverter(PixelFormat.Format1bppIndexed)) using (var writer = ImageWriter.Create("../../../../_Output/PF_ConvertToBlackAndWhiteMemoryFriendly.png")) { Pipeline.Run(reader + converter + 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> /// Rotates image using memory-friendly Pipeline API /// </summary> private static void Rotate() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var rotate = new Rotate(5, RgbColor.Yellow, InterpolationMode.High)) using (var writer = ImageWriter.Create("../../../../_Output/PipelineRotate.jpg")) { Pipeline.Run(reader + rotate + 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> /// 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> /// 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> /// Resizes image and maintains original sharpness using memory-friendly Pipeline API (like Bicubic Sharper mode in Photoshop) /// </summary> private static void ResizeSharpMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var resize = new Resize(320, 0, ResizeInterpolationMode.High)) using (var unsharpMask = new UnsharpMask(0.35f, 1, 0)) using (var writer = ImageWriter.Create("../../../../_Output/ResizeSharpMemoryFriendly.jpg")) { Pipeline.Run(reader + resize + unsharpMask + writer); } }
/// <summary> /// Converts color space from CMYK to RGB with color management using memory-friendly Pipeline API /// </summary> private static void CmykToRgbWithColorManagementMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Copenhagen_CMYK.jpg")) using (var converter = new ColorConverter(PixelFormat.Format24bppRgb)) using (var writer = ImageWriter.Create("../../../../_Output/CmykToRgbWithColorManagementMemoryFriendly.jpg")) { converter.DestinationProfile = ColorProfile.FromSrgb(); Pipeline.Run(reader + converter + writer); } }
/// <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> /// Resizes image to bitmap using memory-friendly Pipeline API /// </summary> private static void ResizeToBitmapMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var resize = new Resize(320, 0, ResizeInterpolationMode.High)) using (var bitmap = new Bitmap()) { Pipeline.Run(reader + resize + bitmap); bitmap.Save("../../../../_Output/ResizeToBitmapMemoryFriendly.jpg"); } }
/// <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 to RGB with color management /// </summary> private static void ConvertCmykToRgbMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Copenhagen_CMYK.jpg")) using (var converter = new ColorConverter(PixelFormat.Format24bppRgb)) using (var writer = ImageWriter.Create("../../../../_Output/PF_ConvertCmykToRgbMemoryFriendly.jpg")) { converter.DefaultSourceProfile = new ColorProfile("../../../../_Input/ColorProfiles/ISOcoated_v2_eci.icc"); converter.DestinationProfile = ColorProfile.FromSrgb(); Pipeline.Run(reader + converter + writer); } }
/// <summary> /// Adds alpha channel using memory-friendly Pipeline API /// </summary> private static void AddAlphaChannelMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/GreenScreen.jpg")) // Use PixelFormat.Format40bppAcmyk for CMYK images using (var converter = new ColorConverter(PixelFormat.Format32bppArgb)) // Remove background just to demonstrate alpha channel using (var greenScreenRemoval = new GreenScreenRemoval()) using (var writer = ImageWriter.Create("../../../../_Output/PF_AddAlphaChannelMemoryFriendly.png")) { Pipeline.Run(reader + converter + greenScreenRemoval + writer); } }
/// <summary> /// Adjusts brightness automatically using memory-friendly Pipeline API /// </summary> private static void AutoBrightnessMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var brightness = new Brightness() { Auto = true }) using (var writer = ImageWriter.Create("../../../../_Output/AutoBrightnessMemoryFriendly.jpg")) { Pipeline.Run(reader + brightness + writer); } }
/// <summary> /// Adjusts contrast automatically using memory-friendly Pipeline API /// </summary> private static void AutoContrastMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var contrast = new Contrast() { Auto = true }) using (var writer = ImageWriter.Create("../../../../_Output/AutoContrastMemoryFriendly.jpg")) { Pipeline.Run(reader + contrast + writer); } }
/// <summary> /// Converts colors using LittleCMS and memory-friendly Pipeline API /// </summary> private static void ConvertColorsLittleCmsMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Copenhagen_CMYK.jpg")) using (var converter = new ColorConverter()) using (var writer = ImageWriter.Create("../../../../_Output/ConvertColorsLittleCmsMemoryFriendly.jpg")) { // LittleCMS is a default color management engine, so no need to specify it // converter.ColorManagementEngine = ColorManagementEngine.LittleCms; converter.DestinationProfile = ColorProfile.FromSrgb(); Pipeline.Run(reader + converter + writer); } }
/// <summary> /// Converts color space from RGB to CMYK with color management using memory-friendly Pipeline API /// </summary> private static void RgbToCmykMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Copenhagen_RGB.jpg")) using (var converter = new ColorConverter()) using (var writer = ImageWriter.Create("../../../../_Output/RgbToCmykWithColorManagementMemoryFriendly.jpg")) { converter.DestinationPixelFormat = PixelFormat.Format32bppCmyk; converter.DefaultSourceProfile = ColorProfile.FromSrgb(); converter.DestinationProfile = new ColorProfile("../../../../_Input/ColorProfiles/ISOcoated_v2_eci.icc"); converter.TransformationIntent = ColorTransformationIntent.Perceptual; Pipeline.Run(reader + converter + writer); } }
private static void ResizeSimpleSyntax() { // reader ---> resize ---> writer using (var reader = ImageReader.Create("../../../../_Input/Venice.jpg")) using (var resize = new Resize(320, 0, ResizeInterpolationMode.High)) using (var writer = ImageWriter.Create("../../../../_Output/PipelineBasics3.jpg")) { // We use plus operator to create a Pipeline collection and chain reader, resize, and writer elements (reader + resize + writer).Run(); // You can use the alternative syntax for better readability //Pipeline.Run(reader + resize + writer); } }
/// <summary> /// Color proofing - CMYK to RGB using destination (screen) and target device profile /// </summary> private static void ColorProofing() { using (var reader = ImageReader.Create("../../../../_Input/Copenhagen_CMYK.jpg")) using (var converter = new ColorConverter()) using (var writer = ImageWriter.Create("../../../../_Output/ColorProofing.jpg")) { // LittleCMS is a default color management engine, so no need to specify it // converter.ColorManagementEngine = ColorManagementEngine.LittleCms; converter.DestinationProfile = ColorProfile.FromSrgb(); converter.TargetDeviceProfile = new ColorProfile(@"../../../../_Input/ColorProfiles/ISOnewspaper26v4_gr.icc"); Pipeline.Run(reader + converter + writer); } }
/// <summary> /// Resizes an image and gets a result as a memory stream /// </summary> private static MemoryStream ResizeImageToMemoryStream(Stream stream, float scale, WriterSettings settings) { var ms = new MemoryStream(); using (var reader = ImageReader.Create(stream)) using (var writer = ImageWriter.Create(ms, settings)) using (var resize = new Resize((int)(reader.Width * scale), (int)(reader.Height * scale))) { Pipeline.Run(reader + resize + writer); writer.Close(); } return(ms); }
/// <summary> /// Converts pixel format to Format24bppRgb (RGB, 8-bit per channel, no alpha channel) using memory-friendly Pipeline API /// </summary> private static void ConvertTo24bppRgbMemoryFriendly(string inputPath, string outputPath) { using (var reader = ImageReader.Create(inputPath)) using (var converter = new ColorConverter(PixelFormat.Format24bppRgb)) using (var writer = ImageWriter.Create(outputPath)) { if (reader.PixelFormat.IsRgb && reader.PixelFormat != PixelFormat.Format24bppRgb) { converter.BackgroundColor = RgbColor.White; //Assign some default color profile converter.DefaultSourceProfile = ColorProfile.FromSrgb(); converter.DestinationProfile = ColorProfile.FromSrgb(); Pipeline.Run(reader + converter + writer); return; } if (reader.PixelFormat.IsGrayscale) { converter.BackgroundColor = new GrayscaleColor(255); //Assign some default color profile converter.DefaultSourceProfile = new ColorProfile("../../../../_Input/ColorProfiles/ISOnewspaper26v4_gr.icc"); converter.DestinationProfile = ColorProfile.FromSrgb(); Pipeline.Run(reader + converter + writer); return; } if (reader.PixelFormat.IsCmyk) { converter.BackgroundColor = new CmykColor(0, 0, 0, 0, 255); //Assign some default color profile converter.DefaultSourceProfile = new ColorProfile("../../../../_Input/ColorProfiles/ISOcoated_v2_eci.icc"); converter.DestinationProfile = ColorProfile.FromSrgb(); Pipeline.Run(reader + converter + writer); return; } //Or just copy file from inputPath to outputPath Pipeline.Run(reader + writer); } }
/// <summary> /// Generates an RGB preview of CMYK color conversion using memory-friendly Pipeline API /// </summary> private static void PreviewRgbToCmykOnScreenMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Copenhagen_RGB.jpg")) using (var converter = new ColorConverter()) using (var writer = ImageWriter.Create("../../../../_Output/PreviewRgbToCmykOnScreenMemoryFriendly.jpg")) { converter.DestinationPixelFormat = PixelFormat.Format24bppRgb; converter.DefaultSourceProfile = ColorProfile.FromSrgb(); converter.TargetDeviceProfile = new ColorProfile("../../../../_Input/ColorProfiles/ISOcoated_v2_eci.icc"); converter.DestinationProfile = ColorProfile.FromScreen(); converter.TransformationIntent = ColorTransformationIntent.Perceptual; Pipeline.Run(reader + converter + writer); } }
/// <summary> /// Modifies channel balance using memory-friendly Pipeline API /// </summary> private static void ChannelBalanceMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var channelBalance = new ChannelBalance() { // Pixel format specifies the order of channels // Example: Format24bppRgb - [Blue]|[Green]|[Red] // http://www.graphicsmill.com/docs/gm/accessing-pixel-data.htm#PixelsInMemory Addends = new float[] { 0.0f, 0.0f, 0.3f } }) using (var writer = ImageWriter.Create("../../../../_Output/ChannelBalanceMemoryFriendly.jpg")) { Pipeline.Run(reader + channelBalance + writer); } }
/// <summary> /// Applies LUT (look-up table) correction using memory-friendly Pipeline API /// </summary> private static void ApplyLutMemoryFriendly() { using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var lutTransform = new LutTransform()) using (var lut = new Lut()) using (var writer = ImageWriter.Create("../../../../_Output/ApplyLutMemoryFriendly.jpg")) { lut.SetPoint(50, 70); lut.SetPoint(100, 150); lut.InterpolationMode = LutInterpolationMode.Cubic; lutTransform.Lut = lut; Pipeline.Run(reader + lutTransform + writer); } }
/// <summary> /// Calls FitJpegQualityToFileSize method and writes result info to console /// </summary> private static void CallFitJpegQualityToFileSize(string srcPath, long maxAllowedSize, string dstPath) { Console.WriteLine("Fit jpeg quality to {0}", FormatFileSize(maxAllowedSize)); using (var reader = ImageReader.Create(srcPath)) Console.WriteLine("Source:\n - Dimensions {0} x {1}\n - Format: {2}", reader.Width, reader.Height, reader.FileFormat); FitJpegQualityToFileSize(srcPath, maxAllowedSize, dstPath); Console.WriteLine("Destination:\n - File size: {0}", FormatFileSize(new FileInfo(dstPath).Length)); using (var reader = ImageReader.Create(dstPath)) Console.WriteLine(" - Dimensions: {0} x {1}", reader.Width, reader.Height); Console.WriteLine(); }
private static void ResizePipelineSyntax() { // reader ---> resize ---> writer using (var reader = ImageReader.Create("../../../../_Input/Venice.jpg")) using (var resize = new Resize(320, 0, ResizeInterpolationMode.High)) using (var writer = ImageWriter.Create("../../../../_Output/PipelineBasics2.jpg")) { // We use Pipeline class to chain (specify Receivers) reader, resize, and writer elements var pipeline = new Pipeline(); pipeline.Add(reader); pipeline.Add(resize); pipeline.Add(writer); pipeline.Run(); } }
/// <summary> /// Draws inside pipeline with AdvancedDrawing /// </summary> private static void DrawUsingAdvancedGraphics() { using (var reader = ImageReader.Create("../../../../_Input/Venice.jpg")) using (var drawer = new Drawer()) using (var writer = ImageWriter.Create("../../../../_Output/DrawUsingAdvancedGraphics.jpg")) { drawer.Draw += (sender, e) => { var pen = new Pen(System.Drawing.Color.GreenYellow, 30); e.Graphics.DrawRectangle(pen, new System.Drawing.RectangleF(15, 15, e.Width - 30, e.Height - 30)); }; Aurigma.GraphicsMill.Pipeline.Run(reader + drawer + writer); } }
/// <summary> /// Splits image channels into multiple receivers /// </summary> private static void SplitChannels() { using (var reader = ImageReader.Create("../../../../_Input/Copenhagen_CMYK.jpg")) using (var splitter = new CmykChannelSplitter()) using (var writerC = new TiffWriter("../../../../_Output/Copenhagen_C.tif")) using (var writerM = new TiffWriter("../../../../_Output/Copenhagen_M.tif")) using (var writerY = new TiffWriter("../../../../_Output/Copenhagen_Y.tif")) using (var writerK = new TiffWriter("../../../../_Output/Copenhagen_K.tif")) { splitter.C = writerC; splitter.M = writerM; splitter.Y = writerY; splitter.K = writerK; Pipeline.Run(reader + splitter); } }
private static void ResizeBasicSyntax() { // reader ---> resize ---> writer using (var reader = ImageReader.Create("../../../../_Input/Venice.jpg")) using (var resize = new Resize(320, 0, ResizeInterpolationMode.High)) using (var writer = ImageWriter.Create("../../../../_Output/PipelineBasics1.jpg")) { // Reader element sends data to resize element reader.Receivers.Add(resize); // Resize element sends data to writer element resize.Receivers.Add(writer); // Pass first element Pipeline.Run(reader); } }
/// <summary> /// Tints using alpha channel and memory-friendly Pipeline API /// </summary> private static void TintUsingAlphaMemoryFriendly(RgbColor color) { // reader ---> converter ---> invert -----\ // generator ---> setAlpha ---> removeAlpha ---> writer using (var reader = ImageReader.Create("../../../../_Input/Chicago.jpg")) using (var converter = new ColorConverter(PixelFormat.Format8bppGrayscale)) using (var invert = new Invert()) using (var generator = new ImageGenerator(reader.Width, reader.Height, PixelFormat.Format24bppRgb, color)) using (var setAlpha = new SetAlpha()) using (var removeAlpha = new RemoveAlpha(RgbColor.White)) using (var writer = ImageWriter.Create("../../../../_Output/TintUsingAlphaMemoryFriendly_" + color.ToString() + ".jpg")) { setAlpha.AlphaSource = reader + converter + invert; Pipeline.Run(generator + setAlpha + removeAlpha + writer); } }