private async void UpdateImage()
        {
            var diff = _optimizer.ColorsCount - numericUpDown1.Value.ToInt();

            if (diff > 0)
            {
                _optimizer.Progress.ProgressChanged += Progress_ProgressChanged;
                GUIDisable();
                await _optimizer.DeleteColorsAsync(diff);

                _optimizer.Progress.ProgressChanged -= Progress_ProgressChanged;
                GUIReady();
                _area.Redraw();
            }
            else if (diff < 0)
            {
                _optimizer.Progress.ProgressChanged += Progress_ProgressChanged;
                GUIDisable();
                await _optimizer.RestoreColorsAsync(-diff);

                _optimizer.Progress.ProgressChanged -= Progress_ProgressChanged;
                GUIReady();
                _area.Redraw();
            }
        }
Exemplo n.º 2
0
        private async void btnLoad_Click(object sender, EventArgs e)
        {
            try {
                string fileName = "";
                using (OpenFileDialog ofd = new OpenFileDialog()) {
                    ofd.CheckFileExists = true;
                    ofd.CheckPathExists = true;
                    ofd.Multiselect     = false;
                    ofd.ReadOnlyChecked = true;
                    ofd.Filter          = "Images|*.jpg;*.png;*.jpeg;*.JPG;*.JPEG;*.bmp;*.BMP;*.PNG;*.TIFF;*.tiff;*.tif;*.TIF|All files|*";
                    if (ofd.ShowDialog() == DialogResult.Cancel)
                    {
                        return;
                    }
                    fileName = ofd.FileName;
                }
                var hash = "N/A";
                using (var md5 = MD5.Create()) {
                    try {
                        using (FileStream fs = new FileStream(fileName, FileMode.Open)) {
                            hash = string.Join("-", md5.ComputeHash(fs).Select(x => x.ToString("X02")));
                        }
                    } finally {
                    }
                }
                var info = new FileInfo(fileName);
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                    _sourceImage = new Bitmap(fs);
                _imgProcessor.LoadImage(_sourceImage);
                GUIWait();
                _imgProcessor.Progress.ProgressChanged += Progress_ProgressChanged;
                await ReadData();

                _imgProcessor.Progress.ProgressChanged -= Progress_ProgressChanged;
                txtInfo.Text = string.Format("{0}; {1} bit/pixel; {2} x {3}; {4}; MD5: {5}", info.Name,
                                             Image.GetPixelFormatSize(_sourceImage.PixelFormat), _sourceImage.Width, _sourceImage.Height, info.Length.ToByteMetricString(), hash);
                if (_data != null)
                {
                    lblSize.Text         = _data.Length.ToByteMetricString(false);
                    numericUpDown1.Value = _imgProcessor.DataLength;
                }
                _areaSource.SetImage(_sourceImage);
                _areaSource.Redraw();
                _imageBuffer = new Bitmap(_sourceImage);
                _areaResult.SetImage(_imageBuffer);
                _areaResult.Redraw();
                UpdateMaxData();
            } catch (Exception ex) {
                MessageBox.Show(string.Format("{0}: {1}", ex.GetType().ToString(), ex.Message));
            } finally {
                if (_imgProcessor?.ImageLoaded ?? false)
                {
                    GUIReady();
                }
                btnShowData.Enabled = _data != null;
            }
        }
Exemplo n.º 3
0
 private void LoadImage()
 {
     if (_image == null)
     {
         try {
             var buf = _data;
             if (chbEncryption.Checked)
             {
                 buf = new byte[_data.Length];
                 Array.Copy(_data, buf, _data.Length);
                 buf = Decrypt(buf, chbCompression.Checked);
             }
             using (var mem = new MemoryStream(buf)) {
                 _image = new Bitmap(mem);
                 _area.SetImage(_image);
                 _area.Redraw();
             }
         } catch (Exception ex) {
             MessageBox.Show(string.Format("{0}: {1}", ex.GetType().ToString(), ex.Message));
         }
     }
 }