Example #1
0
        public void PngCorePalette()
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                PngEncoderOptions options = new PngEncoderOptions {
                    Quantizer = new PaletteQuantizer <Rgba32>(), Quality = 256
                };

                this.bmpCore.SaveAsPng(memoryStream, options);
            }
        }
Example #2
0
        public void PngCoreOctree()
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                PngEncoderOptions options = new PngEncoderOptions()
                {
                    Quantizer = new OctreeQuantizer <Color>(), Quality = 256
                };

                this.bmpCore.SaveAsPng(memoryStream, options);
            }
        }
Example #3
0
        public void PngCoreOctreeNoDIther()
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                PngEncoderOptions options = new PngEncoderOptions {
                    Quantizer = new OctreeQuantizer <Rgba32> {
                        Dither = false
                    }, Quality = 256
                };

                this.bmpCore.SaveAsPng(memoryStream, options);
            }
        }
Example #4
0
        public void PngCorePaletteNoDither()
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                PngEncoderOptions options = new PngEncoderOptions {
                    Quantizer = new PaletteQuantizer <Color> {
                        Dither = false
                    }, Quality = 256
                };

                this.bmpCore.SaveAsPng(memoryStream, options);
            }
        }
Example #5
0
        public void EncodeGeneratedPatterns <TPixel>(TestImageProvider <TPixel> provider, PngColorType pngColorType)
            where TPixel : struct, IPixel <TPixel>
        {
            using (Image <TPixel> image = provider.GetImage())
            {
                PngEncoderOptions options = new PngEncoderOptions()
                {
                    PngColorType = pngColorType
                };
                provider.Utility.TestName += "_" + pngColorType;

                provider.Utility.SaveTestOutputFile(image, "png", new PngEncoder(), options);
            }
        }
Example #6
0
        public void PngCore()
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                Quantizer <Rgba32> quantizer = this.UseOctreeQuantizer
                ? (Quantizer <Rgba32>)
                                               new OctreeQuantizer <Rgba32>()
                : new PaletteQuantizer <Rgba32>();

                PngEncoderOptions options = new PngEncoderOptions()
                {
                    Quantizer = quantizer
                };
                this.bmpCore.SaveAsPng(memoryStream, options);
            }
        }
Example #7
0
        /// <summary>
        /// Gets the <see cref="IImageEncoder" /> for the specified format.
        /// </summary>
        /// <param name="format">The image format.</param>
        /// <param name="encoderOptions">The image encoder options.</param>
        /// <returns>The <see cref="IImageEncoder" />.</returns>
        private static IImageEncoder GetEncoder(ImageFormat format, ImageEncoderOptions encoderOptions)
        {
            switch (format)
            {
            case ImageFormat.Bmp:
                if (encoderOptions == null)
                {
                    encoderOptions = new BmpEncoderOptions();
                }

                if (!(encoderOptions is BmpEncoderOptions))
                {
                    throw new ArgumentException("encoderOptions");
                }

                return(new BmpEncoder((BmpEncoderOptions)encoderOptions));

            case ImageFormat.Png:
                if (encoderOptions == null)
                {
                    encoderOptions = new PngEncoderOptions();
                }

                if (!(encoderOptions is PngEncoderOptions))
                {
                    throw new ArgumentException("encoderOptions");
                }

                return(new PngEncoder((PngEncoderOptions)encoderOptions));

            case ImageFormat.Jpeg:

                throw new NotImplementedException();

            default:
                throw new InvalidOperationException("Image format not supported");
            }
        }
Example #8
0
        /// <summary>
        /// Gets the <see cref="IImageEncoder" /> for the specified format.
        /// </summary>
        /// <param name="format">The image format.</param>
        /// <param name="encoderOptions">The image encoder options.</param>
        /// <returns>The <see cref="IImageEncoder" />.</returns>
        private static IImageEncoder GetEncoder(ImageFormat format, ImageEncoderOptions encoderOptions)
        {
            switch (format)
            {
                case ImageFormat.Bmp:
                    if (encoderOptions == null)
                    {
                        encoderOptions = new BmpEncoderOptions();
                    }

                    if (!(encoderOptions is BmpEncoderOptions))
                    {
                        throw new ArgumentException("encoderOptions");
                    }

                    return new BmpEncoder((BmpEncoderOptions)encoderOptions);

                case ImageFormat.Png:
                    if (encoderOptions == null)
                    {
                        encoderOptions = new PngEncoderOptions();
                    }

                    if (!(encoderOptions is PngEncoderOptions))
                    {
                        throw new ArgumentException("encoderOptions");
                    }

                    return new PngEncoder((PngEncoderOptions)encoderOptions);

                case ImageFormat.Jpeg:

                    throw new NotImplementedException();

                default:
                    throw new InvalidOperationException("Image format not supported");
            }
        }
Example #9
0
        protected override TaskOutput Execute(TaskInput input, ITaskContext context)
        {
            foreach (var file in input.Files)
            {
                try
                {
                    IEncoderOptions options = new JpegEncoderOptions(90, false);
                    switch (_convertFormat)
                    {
                    case ConvertFormat.Jpeg:
                        options = new JpegEncoderOptions(90, false);
                        break;

                    case ConvertFormat.Png:
                        options = new PngEncoderOptions();
                        break;

                    case ConvertFormat.Tiff:
                        options = new TiffEncoderOptions(CompressionType.Zip);
                        break;
                    }

                    var isConverted = false;
                    using (var readStream = new FileStream(file.Path, FileMode.Open))
                    {
                        using (var reader = FormatManager.CreateFormatReader(readStream))
                        {
                            if (reader.MediaFormat != options.MediaFormat)
                            {
                                using (var frame = reader.LoadFrame(0))
                                {
                                    using (var resultBitmap = new Bitmap(frame.Width, frame.Height, Aurigma.GraphicsMill.PixelFormat.Format24bppRgb))
                                    {
                                        frame.GetBitmap(resultBitmap);

                                        var conveter = new PixelFormatConverter {
                                            DestinationPixelFormat = Aurigma.GraphicsMill.PixelFormat.Format32bppArgb
                                        };
                                        conveter.ApplyTransform(resultBitmap);

                                        if (resultBitmap.HasAlpha)
                                        {
                                            resultBitmap.Channels.DiscardAlpha(RgbColor.White);
                                        }

                                        resultBitmap.Save(file.Path + ".tmp", options);
                                        isConverted = true;
                                    }
                                }
                            }
                        }
                    }

                    if (isConverted)
                    {
                        File.Delete(file.Path);
                        File.Move(file.Path + ".tmp", file.Path);
                    }
                }
                catch (System.Exception ex)
                {
                    Logger.ErrorException(string.Format("Error while converting file {1} to {0}", _convertFormat, file.Path), ex);
                }
            }
            return(new TaskOutput(input.Files, input.Params));
        }
Example #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PngEncoder" /> class.
 /// </summary>
 /// <param name="options">The options.</param>
 public PngEncoder(PngEncoderOptions options)
 {
     this.options = options;
 }
Example #11
0
        protected override TaskOutput Execute(TaskInput input, ITaskContext context)
        {
            foreach (var taskFile in input.Files)
            {
                var textNote     = taskFile.Params[_textKey];
                var textPosition = taskFile.Params[_positionKey];

                using (var image = new Bitmap(taskFile.Path))
                {
                    if (!image.IsRgb || image.IsIndexed || image.IsGrayScale)
                    {
                        image.ColorManagement.ConvertToContinuous(ColorSpace.Rgb, false, false);
                    }

                    var renderingSize = new Size(image.Width, image.Height);

                    double materialWidth  = (double)renderingSize.Width / 100;
                    double materialHeight = (double)renderingSize.Height / 100;

                    double lineSize = 0;
                    if (textPosition == "Left" || textPosition == "Right")
                    {
                        lineSize       = materialWidth / 15;
                        materialWidth -= lineSize;
                    }
                    else
                    {
                        lineSize        = materialHeight / 15;
                        materialHeight -= lineSize;
                    }

                    int textWidth  = 0;
                    int textHeight = (int)Math.Round(lineSize * image.Height / materialHeight);

                    if (textPosition == "Left" || textPosition == "Right")
                    {
                        textWidth = image.Height;
                    }
                    else
                    {
                        textWidth = image.Width;
                    }

                    using (Bitmap text = new Bitmap(System.Drawing.Color.FromArgb(240, 240, 240), textWidth, textHeight, PixelFormat.Format32bppArgb))
                    {
                        using (Graphics graphics = text.GetGdiplusGraphics())
                        {
                            System.Drawing.Color sourceColor = System.Drawing.Color.White;
                            System.Drawing.Color destColor   = System.Drawing.Color.White;

                            if (textPosition == "Left" || textPosition == "Top")
                            {
                                sourceColor = System.Drawing.Color.FromArgb(200, 200, 200);
                            }
                            else
                            {
                                destColor = System.Drawing.Color.FromArgb(200, 200, 200);
                            }

                            var gradient = new LinearGradientBrush(new Rectangle(0, 0, text.Width, text.Height), sourceColor, destColor, LinearGradientMode.Vertical);
                            graphics.FillRectangle(gradient, 0, 0, text.Width, text.Height);

                            using (var drawFont = new Aurigma.GraphicsMill.Drawing.Font("Trebuchet MS", textHeight / 2))
                            {
                                drawFont.Antialiased = true;
                                drawFont.Unit        = Unit.Point;
                                drawFont.ClearType   = true;

                                using (var drawBrush = new SolidBrush(System.Drawing.Color.Black))
                                {
                                    var drawFormat = new StringFormat();
                                    drawFormat.Alignment     = StringAlignment.Center;
                                    drawFormat.LineAlignment = StringAlignment.Center;

                                    graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                                    graphics.DrawString(textNote, drawFont, drawBrush, new RectangleF(0, 0, textWidth, textHeight), drawFormat);
                                }
                            }
                        }

                        int resultWidth  = image.Width;
                        int resultHeight = image.Height;

                        if (textPosition == "Left" || textPosition == "Right")
                        {
                            text.Transforms.RotateAndFlip(RotateFlipType.Rotate270FlipNone);
                            resultWidth += text.Width;
                        }
                        else
                        {
                            resultHeight += text.Height;
                        }

                        using (var result = new Bitmap(resultWidth, resultHeight, PixelFormat.Format32bppArgb))
                        {
                            switch (textPosition)
                            {
                            case "Left":
                                text.Draw(result, 0, 0, CombineMode.AlphaOverlapped, 1.0f);
                                image.Draw(result, text.Width, 0, CombineMode.AlphaOverlapped, 1.0f);
                                break;

                            case "Right":
                                text.Draw(result, image.Width, 0, CombineMode.AlphaOverlapped, 1.0f);
                                image.Draw(result, 0, 0, CombineMode.AlphaOverlapped, 1.0f);
                                break;

                            case "Top":
                                text.Draw(result, 0, 0, CombineMode.AlphaOverlapped, 1.0f);
                                image.Draw(result, 0, text.Height, CombineMode.AlphaOverlapped, 1.0f);
                                break;

                            default:
                                text.Draw(result, 0, image.Height, CombineMode.AlphaOverlapped, 1.0f);
                                image.Draw(result, 0, 0, CombineMode.AlphaOverlapped, 1.0f);
                                break;
                            }

                            var pngEncoder = new PngEncoderOptions();
                            result.Save(taskFile.Path, pngEncoder);
                        }
                    }
                }
            }
            return(new TaskOutput(input.Files, input.Params));
        }
Example #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PngEncoder" /> class.
 /// </summary>
 /// <param name="options">The options.</param>
 public PngEncoder(PngEncoderOptions options)
 {
     this.options = options;
 }