private void ActionAddMultipleExecute(object sender, EventArgs e) { string filename; bool usePngFile; Bitmap bitmap; Bitmap uIImage; TRibbonImage image; int size; ListViewItem item; OpenFileDialog openDialog = new OpenFileDialog(); openDialog.Filter = "BMP and PNG files|*.bmp;*.png|BMP files|*.bmp|PNG files|*.png"; openDialog.Title = "Open Image File"; openDialog.CheckFileExists = true; openDialog.ReadOnlyChecked = false; openDialog.Multiselect = true; if (openDialog.ShowDialog() == DialogResult.OK) { foreach (string s in openDialog.FileNames) { bool saveToBmp = false; filename = s; if ((ImageFlags.Large & _flags) != 0) { if ((ImageFlags.HighContrast & _flags) != 0) { image = _command.AddLargeHighContrastImage(); } else { image = _command.AddLargeImage(); } } else if ((ImageFlags.HighContrast & _flags) != 0) { image = _command.AddSmallHighContrastImage(); } else { image = _command.AddSmallImage(); } usePngFile = Settings.Instance.AllowPngImages && Path.GetExtension(s).Equals(".png", StringComparison.OrdinalIgnoreCase); // UIImage will automatically convert to 32 - bit alpha image bitmap = null; uIImage = AlphaBitmap.BitmapFromFile(filename, (ImageFlags.HighContrast & _flags) != 0); try { bitmap = uIImage.Clone(new Rectangle(new Point(), uIImage.Size), uIImage.PixelFormat); if (!Addons.StartsText(image.Owner.Directory, filename)) { filename = Path.Combine(image.Owner.Directory, "Res"); Addons.ForceDirectories(filename); filename = Path.Combine(filename, Path.GetFileName(s)); if (usePngFile) { File.Copy(s, filename, true); } else { saveToBmp = true; } } if (!usePngFile && !Path.GetExtension(filename).Equals("bmp", StringComparison.OrdinalIgnoreCase)) { saveToBmp = true; filename = Path.ChangeExtension(filename, ".bmp"); } if (saveToBmp) { bitmap.Save(filename, ImageFormat.Bmp); //@ changed, don't override the same file } image.Source = image.Owner.BuildRelativeFilename(filename); size = Math.Max(bitmap.Width, bitmap.Height); if ((ImageFlags.Large & _flags) != 0) { size = size / 2; } if (size <= 16) { image.MinDpi = 0; } else if (size <= 20) { image.MinDpi = 120; } else if (size <= 24) { image.MinDpi = 144; } else { image.MinDpi = 192; } } finally { uIImage.Dispose(); bitmap.Dispose(); } item = listView.Items.Add(new ListViewItem()); SetImageItem(item, image); listView.Items[item.Index].Selected = true; item.Focused = true; MainForm.FormMain.Modified(); } } }
private void EditImageFileRightButtonClick(object sender, EventArgs e) { string newFilename; bool usePngFile; Bitmap uIImage; Bitmap bitmap; int size; OpenFileDialog openDialog = new OpenFileDialog(); openDialog.Filter = "BMP and PNG files|*.bmp;*.png|BMP files|*.bmp|PNG files|*.png"; openDialog.CheckFileExists = true; openDialog.ReadOnlyChecked = false; openDialog.Title = "Open Image File"; openDialog.InitialDirectory = Path.GetDirectoryName(_filename); openDialog.FileName = Path.GetFileName(_filename); if (openDialog.ShowDialog() == DialogResult.OK) { bool saveToBmp = false; newFilename = openDialog.FileName; usePngFile = Settings.Instance.AllowPngImages && Path.GetExtension(openDialog.FileName).Equals(".png", StringComparison.OrdinalIgnoreCase); //UIImage will automatically convert to 32 - bit alpha image bitmap = null; uIImage = AlphaBitmap.TryAlphaBitmapFromFile(newFilename, (_flags & ImageFlags.HighContrast) != 0); try { bitmap = uIImage.Clone(new Rectangle(new Point(), uIImage.Size), uIImage.PixelFormat); //bitmap = Bitmap.FromHbitmap(uIImage.GetHbitmap()); // this code do not work, because it will not produce a ARGB bitmap if (!Addons.StartsText(_image.Owner.Directory, newFilename)) { newFilename = Path.Combine(_image.Owner.Directory, "Res"); Addons.ForceDirectories(newFilename); newFilename = Path.Combine(newFilename, Path.GetFileName(openDialog.FileName)); if (usePngFile) { File.Copy(openDialog.FileName, newFilename, true); } else { saveToBmp = true; } //newFilename = Path.ChangeExtension(newFilename, ".bmp"); //bitmap.Save(newFilename, ImageFormat.Bmp); //@ changed, don't override the same file //It seems to be a better solution when we copy the file when it is a .bmp with BM Header than bitmap.Save //If the file from openDialog is the same as newFilename we can delete the openDialog file first //or jump around the bitmap.Save //we have to do some checks if the choosen file has a BM Header //or is it possible for the WindowsRibbon, that we can use png files ? } if (!usePngFile && !Path.GetExtension(newFilename).Equals(".bmp", StringComparison.OrdinalIgnoreCase)) { saveToBmp = true; newFilename = Path.ChangeExtension(newFilename, ".bmp"); } if (saveToBmp) { AlphaBitmap.SetTransparentRGB(bitmap, Color.LightGray.ToArgb() & 0xffffff); bitmap.Save(newFilename, ImageFormat.Bmp); //@ changed, don't override the same file } EditImageFile.Text = _image.Owner.BuildRelativeFilename(newFilename); _image.Source = EditImageFile.Text; ClearBitmap(_bitmap); Graphics canvas = Graphics.FromImage(_bitmap); if (uIImage.PixelFormat != PixelFormat.Format32bppArgb) { uIImage.MakeTransparent(); } canvas.DrawImage(uIImage, new Point((64 - uIImage.Width) / 2, (64 - uIImage.Height) / 2)); canvas.Dispose(); PaintBox.Invalidate(); size = Math.Max(bitmap.Width, bitmap.Height); if ((_flags & ImageFlags.Large) != 0) { size = size / 2; } if (size <= 16) { ComboBoxMinDpi.SelectedIndex = 0; } else if (size <= 20) { ComboBoxMinDpi.SelectedIndex = 2; } else if (size <= 24) { ComboBoxMinDpi.SelectedIndex = 3; } else { ComboBoxMinDpi.SelectedIndex = 4; } } finally { uIImage.Dispose(); bitmap.Dispose(); } } }