public ImageWorkUI()
        {
            InitializeComponent();

            cbFormats.DataSource    = new BindingSource(FormatPreset.ComboBoxOptions, null);
            cbFormats.DisplayMember = "Name";
            _currentFormatPreset    = FormatPreset.Original;

            cbResolutions.DataSource    = new BindingSource(ResolutionPreset.ComboBoxOptions, null);
            cbResolutions.DisplayMember = "Name";
            _currentResolutionPreset    = ResolutionPreset.Original;
        }
        public ImageWorkUI(string imageFile, XmlNode relationshipXmlNode) : this()
        {
            Original  = new ImageWithInfo(imageFile);
            Optimized = Original;

            this.RelationshipNode = relationshipXmlNode;

            // Automatic recommendation - set the upper max resolution to 1600x900 and change to 80% JPEG
            if ((Original.Width > 1600) || (Original.Height > 900))
            {
                _currentResolutionPreset = ResolutionPreset.ComboBoxOptions.First(opt => (opt.Width == 1600) && (opt.Height == 900));
                _currentFormatPreset     = FormatPreset.ComboBoxOptions.First(opt => (opt.FormatName == "JPG") && (opt.QualityLevel == 80));
            }
            cbResolutions.SelectedItem = _currentResolutionPreset;

            // Automatic recommendation - if pixel-byte density is higher than 20% and the format is PNG, convert to a 70% JPG
            if ((Original.BytesPerPixel > 0.2) && (Original.Type == ImageWithInfo.ImageTypes.Png) && (_currentFormatPreset == FormatPreset.Original))
            {
                _currentFormatPreset = FormatPreset.ComboBoxOptions.First(opt => (opt.FormatName == "JPG") && (opt.QualityLevel == 70));
            }

            // If we're scaling a JPG, try to preserve the quality (use 80% instead of 70%)
            //if ((_currentResolutionPreset != ResolutionPreset.Original) && (Original.Type == ImageWithInfo.ImageTypes.Jpg))
            //{
            //    _currentFormatPreset = FormatPreset.ComboBoxOptions.First(opt => (opt.FormatName == "JPG") && (opt.QualityLevel == 80));
            //}

            cbFormats.SelectedItem = _currentFormatPreset;

            // Set up originals
            pbOriginal.Image        = Original.Image;
            pbOptimized.Image       = Original.Image; // This will get updated with the optimized image when initialization is done
            pbOriginal.MaximumSize  = new Size(Original.Width, Original.Height);
            pbOptimized.MaximumSize = pbOriginal.MaximumSize;
            int[] scaledSizes = Original.GetScaledSizeFromWidth(pbOriginal.Size.Width);
            pbOriginal.Size  = new Size(scaledSizes[0], scaledSizes[1]);
            pbOptimized.Size = new Size(scaledSizes[0], scaledSizes[1]);

            // Populate info about the original file
            lblOriginalImage.Text = "Original Image: " + Original.GetShortInfo();

            // Finished
            Initialized = true;
        }
        public Bitmap GetScaledImage(ResolutionPreset Resolution)
        {
            // Set our maximum height/width
            float maxWidth  = Resolution.Width;
            float maxHeight = Resolution.Height;

            // Allow scaling down but prevent scaling up (we don't want to increase file size!)
            float scale = Math.Min(1, Math.Min(maxWidth / Width, maxHeight / Height));

            // If we're not scaling down, just skip the rest of the process
            if (scale == 1)
            {
                return(Image);
            }

            // If we're scaling down, calculate the new width/height
            int scaledWidth  = (int)(Width * scale);
            int scaledHeight = (int)(Height * scale);

            // Create a target, empty image with the new size
            Bitmap scaledImage = new Bitmap((int)scaledWidth, (int)scaledHeight);

            using (Graphics g = Graphics.FromImage(scaledImage))
            {
                // Use the highest quality scaling operations available
                g.SmoothingMode      = SmoothingMode.AntiAlias;
                g.InterpolationMode  = InterpolationMode.High;
                g.CompositingQuality = CompositingQuality.HighQuality;

                // Fill in our background with black
                g.FillRectangle(Brushes.Black, new RectangleF(0, 0, scaledWidth, scaledHeight));

                // Copy our scaled image into place
                g.DrawImage(Image, new Rectangle(0, 0, scaledWidth, scaledHeight));
            }

            // Return the result
            return(scaledImage);
        }
        public ImageWithInfo GenerateOptimized_PNG(long BitDepth, ResolutionPreset Resolution)
        {
            // Initialize encoders
            if (_pngEncoder == null)
            {
                _initEncoderInfo();
            }

            EncoderParameters toFormatParams = new EncoderParameters(1);

            toFormatParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, BitDepth);

            MemoryStream ms          = new MemoryStream();
            Bitmap       scaledImage = Image;

            if (Resolution.Width > 0)
            {
                scaledImage = GetScaledImage(Resolution);
            }
            scaledImage.Save(ms, _pngEncoder, toFormatParams);

            return(new ImageWithInfo(ms));
        }