private async void ChangeImageColorType() { //string mime = string.Format("image/png", fileName.Substring(fileName.LastIndexOf('.') + 1)); ImageCodecInfo codec = ColorAndImageFactory.GetEncoderInfo(cmbMime.SelectedItem.ToString()); EncoderParameters encoderParameters = new EncoderParameters(2); EncoderParameter parameterColorDepth = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, GetBitCount()); EncoderParameter parameterQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)numericUpDown1.Value.ToInt()); encoderParameters.Param[0] = parameterColorDepth; encoderParameters.Param[1] = parameterQuality; using (MemoryStream ms = new MemoryStream()) { var img = _srcImage; if (cmbMime.SelectedIndex == 1) { img = new Bitmap(_srcImage); _progress.OperationsTotal = img.Height * 2; _progress.OperationsDone = 0; BlockGUI(true); _progress.ProgressChanged += _progress_ProgressChanged; await OptimizePNG(img); _progress.ProgressChanged -= _progress_ProgressChanged; BlockGUI(false); } img.Save(ms, codec, encoderParameters); _imgBinaryData = ms.ToArray(); lblSize.Text = _imgBinaryData.Length.ToByteMetricString(false); _image = new Bitmap(ms); //OptimizePNG(); _area.SetImage(_image); _area.Redraw(); } }
private int MinimalRange(Color cl) { double res = ColorAndImageFactory.GetColorRangeQ(cl, _Colors[0]); int resId = -1; for (int i = 1; i < _Colors.Count; i++) { var dif = ColorAndImageFactory.GetColorRangeQ(cl, _Colors[i]); if (dif < res) { res = dif; resId = i; } } return(resId); }
private void OptimizePNGStep1(Bitmap image) { for (int i = 2; i < image.Height - 2; i++) { for (int j = 2; j < image.Width - 2; j++) { image.SetPixel(j, i, ColorAndImageFactory.GetMedianFilter( image.GetPixel(j - 2, i - 2), image.GetPixel(j - 1, i - 2), image.GetPixel(j, i - 2), image.GetPixel(j + 1, i - 2), image.GetPixel(j + 2, i - 2), image.GetPixel(j - 2, i - 1), image.GetPixel(j - 1, i - 1), image.GetPixel(j, i - 1), image.GetPixel(j + 1, i - 1), image.GetPixel(j + 2, i - 1), image.GetPixel(j - 2, i), image.GetPixel(j - 1, i), image.GetPixel(j, i), image.GetPixel(j + 1, i), image.GetPixel(j + 2, i), image.GetPixel(j - 2, i + 1), image.GetPixel(j - 1, i + 1), image.GetPixel(j, i + 1), image.GetPixel(j + 1, i + 1), image.GetPixel(j + 2, i + 1), image.GetPixel(j - 2, i + 2), image.GetPixel(j - 1, i + 2), image.GetPixel(j, i + 2), image.GetPixel(j + 1, i + 2), image.GetPixel(j + 2, i + 2) )); } _progress.OperationsDone++; } }
private double MinimalRange(out int color0, out int color1) { double res = double.MaxValue; color0 = 0; color1 = 0; for (int i = 0; i < _Colors.Count - 1; i++) { double range = ColorAndImageFactory.GetColorRangeQ(_Colors[i], _Colors[i + 1]); if (range < res) { res = range; color0 = i; color1 = i + 1; } } return(res); }
private void button1_Click_1(object sender, EventArgs e) { using (SaveFileDialog sfd = new SaveFileDialog()) { sfd.AddExtension = true; sfd.CheckPathExists = true; ImageCodecInfo codec = ColorAndImageFactory.GetEncoderInfo(cmbMime.SelectedItem.ToString()); sfd.Filter = string.Format("Choosen format({0})|{0}", codec.FilenameExtension); if (sfd.ShowDialog() == DialogResult.Cancel) { return; } EncoderParameters encoderParameters = new EncoderParameters(2); EncoderParameter parameterColorDepth = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, GetBitCount()); EncoderParameter parameterQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)numericUpDown1.Value.ToInt()); encoderParameters.Param[0] = parameterColorDepth; encoderParameters.Param[1] = parameterQuality; using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create)) { fs.Write(_imgBinaryData, 0, _imgBinaryData.Length); } } }
private void OptimizePNGStep0(Bitmap image) { Color prev = Color.White; double difference = 1.0 - (double)numericUpDown1.Value / 100.0; for (int i = 1; i < image.Height - 1; i++) { for (int j = 1; j < image.Width - 1; j++) { var color = image.GetPixel(j, i); if (ColorAndImageFactory.GetColorRangeQ(prev, color) < difference) { image.SetPixel(j, i, prev); } else { prev = color; } } _progress.OperationsDone++; } }