public static void SaveImg(ParallObj obj) { if (obj.ImgQualityObj < 0 || obj.ImgQualityObj > 100) { throw new ArgumentOutOfRangeException("quality must be between 0 and 100."); } // Encoder parameter for image quality EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, obj.ImgQualityObj); // JPEG image codec ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg"); EncoderParameters encoderParams = new EncoderParameters(1); encoderParams.Param[0] = qualityParam; Image imgTemp = Image.FromFile(obj.ImgPathObj); imgTemp.Save(obj.pathDestinationObj + @"\" + Path.GetFileName(obj.ImgPathObj), jpegCodec, encoderParams); }
public static void SaveImgAndGray(ParallObj obj) { if (obj.ImgQualityObj < 0 || obj.ImgQualityObj > 100) { throw new ArgumentOutOfRangeException("quality must be between 0 and 100."); } // Encoder parameter for image quality EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, obj.ImgQualityObj); // JPEG image codec ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg"); EncoderParameters encoderParams = new EncoderParameters(1); encoderParams.Param[0] = qualityParam; Image imgTemp = Image.FromFile(obj.ImgPathObj); string tempPath = obj.pathDestinationObj + @"\" + Path.GetFileName(obj.ImgPathObj); imgTemp.Save(tempPath, jpegCodec, encoderParams); Bitmap image = new Bitmap(obj.pathDestinationObj + @"\" + Path.GetFileName(obj.ImgPathObj)); Bitmap grayScale = new Bitmap(image.Width, image.Height); for (Int32 y = 0; y < grayScale.Height; y++) { for (Int32 x = 0; x < grayScale.Width; x++) { System.Drawing.Color c = image.GetPixel(x, y); Int32 gs = (Int32)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11); grayScale.SetPixel(x, y, System.Drawing.Color.FromArgb(gs, gs, gs)); } } string tempPath1 = obj.pathDestinationObj + @"\_Gray_" + Path.GetFileName(obj.ImgPathObj); grayScale.Save(tempPath1, ImageFormat.Jpeg); }
private void LetsDoItButtonClick(object sender, EventArgs e) { myParallObj = new List <ParallObj>(); bool processFulfilled = false; try { foreach (var imgPath in pathsList) { ParallObj temp = new ParallObj(); temp.ImgPathObj = imgPath; temp.ImgQualityObj = ImgQtyDecreasing; temp.pathDestinationObj = setFilesDirPath; myParallObj.Add(temp); } } catch (Exception ex) { MessageBox.Show("Please, put correct data!"); return; } //Black&White and Decreased quality, synch processing if (getFilesDirPath != "" && setFilesDirPath != "" && pathsList != null && ImgQtyDecreasing != 0 && CheckBoxBlackWhite.IsChecked == true && synch.IsChecked == true && asynch.IsChecked == false) { var stopWatch = Stopwatch.StartNew(); foreach (var imageData in myParallObj) { Operating.SaveImgAndGray(imageData); } MessageBox.Show("Processing time is " + stopWatch.Elapsed.TotalSeconds.ToString() + " sec."); processFulfilled = true; } //Decreased quality, synch processing if (getFilesDirPath != "" && setFilesDirPath != "" && pathsList != null && ImgQtyDecreasing != 0 && CheckBoxBlackWhite.IsChecked == false && synch.IsChecked == true && asynch.IsChecked == false) { var stopWatch = Stopwatch.StartNew(); foreach (var imageData in myParallObj) { Operating.SaveImg(imageData); } MessageBox.Show("Processing time is " + stopWatch.Elapsed.TotalSeconds.ToString() + " sec."); processFulfilled = true; } //Decreased quality, asynch processing, ThreadPool if (getFilesDirPath != "" && setFilesDirPath != "" && pathsList != null && ImgQtyDecreasing != 0 && CheckBoxBlackWhite.IsChecked == false && synch.IsChecked == false && asynch.IsChecked == true && ThreadPoolBtn.IsChecked == true) { var stopWatch = Stopwatch.StartNew(); foreach (var element in myParallObj) { ThreadPool.QueueUserWorkItem(state => Operating.SaveImg(element)); } MessageBox.Show("Processing time is " + stopWatch.Elapsed.TotalSeconds.ToString() + " sec."); processFulfilled = true; } //Decreased quality, asynch processing, Parallel if (getFilesDirPath != "" && setFilesDirPath != "" && pathsList != null && ImgQtyDecreasing != 0 && CheckBoxBlackWhite.IsChecked == false && synch.IsChecked == false && asynch.IsChecked == true && ParallelBtn.IsChecked == true) { var stopWatch = Stopwatch.StartNew(); ParallelLoopResult result = Parallel.ForEach <ParallObj>(myParallObj, Operating.SaveImg); MessageBox.Show("Processing time is " + stopWatch.Elapsed.TotalSeconds.ToString() + " sec."); processFulfilled = true; } //Black&White and Decreased quality, asynch processing, ThreadPool if (getFilesDirPath != "" && setFilesDirPath != "" && pathsList != null && ImgQtyDecreasing != 0 && CheckBoxBlackWhite.IsChecked == true && asynch.IsChecked == true && ThreadPoolBtn.IsChecked == true) { var stopWatch = Stopwatch.StartNew(); foreach (var element in myParallObj) { ThreadPool.QueueUserWorkItem(state => Operating.SaveImgAndGray(element)); } MessageBox.Show("Processing time is " + stopWatch.Elapsed.TotalSeconds.ToString() + " sec."); processFulfilled = true; } //Black&White and Decreased quality, asynch processing, Parallel if (getFilesDirPath != "" && setFilesDirPath != "" && pathsList != null && ImgQtyDecreasing != 0 && CheckBoxBlackWhite.IsChecked == true && asynch.IsChecked == true && ParallelBtn.IsChecked == true) { var stopWatch = Stopwatch.StartNew(); ParallelLoopResult resultBandW = Parallel.ForEach <ParallObj>(myParallObj, Operating.SaveImgAndGray); MessageBox.Show("Processing time is " + stopWatch.Elapsed.TotalSeconds.ToString() + " sec."); processFulfilled = true; } if (processFulfilled == false) { MessageBox.Show("Please, put correct data! \nChoose all options again!"); } setFilesDirPath = ""; getFilesDirPath = ""; pathsList = null; myParallObj = null; }