/// <summary>Handles the Click event of the Process button which crops the main image and applies the filter stack to generate the output image.</summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> private void cmdProcess_Click(object sender, EventArgs e) { if (!_placed) { return; } if (pcFullImage.Image == null) { return; } Bitmap b = new Bitmap(pbSource.Width, pbSource.Height); Graphics g = Graphics.FromImage(b); g.Clear(Color.White); g.DrawImage(pcFullImage.Image, new Rectangle(0, 0, pbSource.Width, pbSource.Height), _selection.X, _selection.Y, _selection.Width, _selection.Height, GraphicsUnit.Pixel); g.Dispose(); pbSource.Image = b; if (filterStack.Count != 0) { b = filterStack.Apply(b); } var sc = new SaturationCorrection(); if (sc.FormatTranslations.ContainsKey(b.PixelFormat)) { sc.AdjustValue = hsbSaturation.Value / 100.0f; // apply the filter sc.ApplyInPlace(b); } Bitmap greyb = null; if (AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.FormatTranslations.ContainsKey(b.PixelFormat)) { greyb = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(b); } else { this.Text = "Cannot convert to greyscale"; greyb = b; } var bc = new BrightnessCorrection(); if (bc.FormatTranslations.ContainsKey(b.PixelFormat)) { bc.AdjustValue = hsbBrightness.Value; bc.ApplyInPlace(greyb); } var cc = new ContrastCorrection(); if (cc.FormatTranslations.ContainsKey(b.PixelFormat)) { cc.Factor = hsbContrast.Value; cc.ApplyInPlace(greyb); } if (filterStack.Count == 0) { var sharpen = new Sharpen(); if (sharpen.FormatTranslations.ContainsKey(b.PixelFormat)) { sharpen.ApplyInPlace(greyb); } } if (chkInvert.Checked) { var invert = new Invert(); invert.ApplyInPlace(greyb); } pbInt.Image = greyb; b = greyb; //BaseInPlacePartialFilter filter = new AForge.Imaging.Filters.FloydSteinbergDithering(); BaseInPlacePartialFilter filter = null; if (cmbAlgorithm.SelectedItem != null && cmbAlgorithm.SelectedItem is AForge.Imaging.Filters.BaseInPlacePartialFilter) { filter = cmbAlgorithm.SelectedItem as AForge.Imaging.Filters.BaseInPlacePartialFilter; } if (filter == null) { filter = new AForge.Imaging.Filters.SierraDithering(); } if (filter.FormatTranslations.ContainsKey(b.PixelFormat)) { var ditheredb = filter.Apply(b); pbDest.Image = ditheredb; this.Text = "Badger!"; } else { this.Text = "Cannot dither this image!"; } }
public static Bitmap ApplyFiliter(ImageFiliter imgFilter, Bitmap bmp, byte Value, byte Value2) { Bitmap newImage = null; //ContrastCorrection filter2 = new ContrastCorrection(1.0); //newImage = filter2.Apply(bmp); if (imgFilter != ImageFiliter.None) { IFilter filter3 = Grayscale.CommonAlgorithms.Y; newImage = filter3.Apply(bmp); if (imgFilter == ImageFiliter.Threshold) { IFilter filter = null; if (Value == 0) filter = new Threshold(); else filter = new Threshold(Value); newImage = filter.Apply(newImage); //IterativeThreshold filter = new IterativeThreshold(Value2, Value); //// apply the filter // newImage = filter.Apply(newImage); } if (imgFilter == ImageFiliter.ThresholdWithCarry) { IFilter filter = new ThresholdWithCarry(); newImage = filter.Apply(newImage); } else if (imgFilter == ImageFiliter.OrderedDithering) { IFilter filter = new OrderedDithering(); newImage = filter.Apply(newImage); } else if (imgFilter == ImageFiliter.BayerDithering) { IFilter filter = new BayerDithering(); newImage = filter.Apply(newImage); } else if (imgFilter == ImageFiliter.FloydSteinbergDithering) { IFilter filter = new FloydSteinbergDithering(); newImage = filter.Apply(newImage); } else if (imgFilter == ImageFiliter.BurkesDithering) { IFilter filter = new BurkesDithering(); newImage = filter.Apply(newImage); } else if (imgFilter == ImageFiliter.JarvisJudiceNinkeDithering) { IFilter filter = new JarvisJudiceNinkeDithering(); newImage = filter.Apply(newImage); } else if (imgFilter == ImageFiliter.SierraDithering) { IFilter filter = new SierraDithering(); newImage = filter.Apply(newImage); } else if (imgFilter == ImageFiliter.StuckiDithering) { IFilter filter = new StuckiDithering(); newImage = filter.Apply(newImage); } else if (imgFilter == ImageFiliter.Convolution) { // create filter //OtsuThreshold filter = new OtsuThreshold(); //// apply the filter //filter.ApplyInPlace(newImage); //// create filter //IterativeThreshold filter = new IterativeThreshold(0); //// apply the filter //newImage = filter.Apply(newImage); int[,] kernel = { { -2, -1, 0 }, { -1, 1, 1 }, { 0, 1, 2 } }; // create filter Convolution filter = new Convolution(kernel); // apply the filter filter.ApplyInPlace(newImage); } newImage = BitmapTo1Bpp(newImage); } else newImage = BitmapTo1Bpp(bmp); //轉換成 1bit bps return newImage; }