예제 #1
0
        private byte[] TransformImage(TransformationsViewModel viewModel)
        {
            var transformations = LoadImageTransformations(viewModel);

            Bitmap bmp = null;

            using (MemoryStream stm = new MemoryStream())
            {
                viewModel.File.CopyTo(stm);

                bmp = ImageTransformer.Apply(stm, transformations);

                // clear memory stream
                stm.Position = 0;
                stm.SetLength(0);

                bmp.Save(stm, System.Drawing.Imaging.ImageFormat.Png);
                return(stm.ToArray());
            }
        }
        private void ApplyButton_Click(object sender, EventArgs e)
        {
            if (_file == null)
            {
                return;
            }


            List <IImageTransformation> transformations = new List <IImageTransformation>(4);

            if (this.AngleNumericUpDown.Value != 0)
            {
                transformations.Add(
                    new RotationImageTransformation((double)this.AngleNumericUpDown.Value));
            }

            if (this.StretchHorizNumericUpDown.Value != 0 || this.StretchVertNumericUpDown.Value != 0)
            {
                transformations.Add(
                    new StretchImageTransformation(
                        (double)this.StretchHorizNumericUpDown.Value / 100,
                        (double)this.StretchVertNumericUpDown.Value / 100));
            }

            if (this.FlipHorizontalCheckBox.Checked || this.FlipVerticalCheckBox.Checked)
            {
                transformations.Add(
                    new FlipImageTransformation(this.FlipHorizontalCheckBox.Checked, this.FlipVerticalCheckBox.Checked));
            }

            if (new decimal[]
            {
                this.DensityAlphaNumericUpDown.Value,
                this.DensityRedNumericUpDown.Value,
                this.DensityGreenNumericUpDown.Value,
                this.DensityBlueNumericUpDown.Value
            }.Any(a => a != 100))
            {
                transformations.Add(
                    new DensityImageTransformation(
                        (double)this.DensityAlphaNumericUpDown.Value / 100,
                        (double)this.DensityRedNumericUpDown.Value / 100,
                        (double)this.DensityGreenNumericUpDown.Value / 100,
                        (double)this.DensityBlueNumericUpDown.Value / 100
                        ));
            }

            DisposePreviousImage();
            StartStopwatch();


            if (transformations.Any())
            {
                var bmp = ImageTransformer.Apply(_file, transformations.ToArray());

                this.ImagePictureBox.Image = bmp;
            }
            else
            {
                LoadImageFile();
            }

            StopStopwatch();
        }