//public static void LoadBmpWinApi(string fileName, string bmpPath) //{ // System.Drawing.Bitmap bmp = UIRibbonTools.AlphaBitmap.ImageFromFile(fileName); // System.Drawing.Imaging.PixelFormat f = bmp.PixelFormat; // BmpWriter writer = new BmpWriter(); // writer.Encode(bmp, Path.ChangeExtension(fileName, "v5bmp"), BmpEncoding.Encoding32BPP); // bmp.Save(Path.ChangeExtension(fileName, "bmp1"), System.Drawing.Imaging.ImageFormat.Bmp); //} protected override void OpenSave(string fileName) { Bitmap bmp; if (_inputIgnoreAlpha) { bmp = new Bitmap(fileName); if (bmp.PixelFormat == PixelFormat.Format32bppRgb) { bmp = bmp.Clone(new Rectangle(new Point(), bmp.Size), PixelFormat.Format24bppRgb); bmp.MakeTransparent(); } } else { bmp = AlphaBitmap.TryAlphaBitmapFromFile(fileName); } string bmpFile = Path.Combine(_outputPath, _fileNameWithoutExtension + ".bmp"); if (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb && _outputSelected == OutputSelector.Bitmap) { AlphaBitmap.SetTransparentRGB(bmp, TransparentRGBColor); } Save(bmp, bmpFile); }
protected override void OpenSave(string fileName) { System.Drawing.Icon ico = new System.Drawing.Icon(fileName); System.Drawing.Bitmap bmp = ico.ToBitmap(); string bmpFile = Path.Combine(_outputPath, _fileNameWithoutExtension + ".bmp"); if (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb && _outputSelected == OutputSelector.Bitmap) { AlphaBitmap.SetTransparentRGB(bmp, TransparentRGBColor); } Save(bmp, bmpFile); }
private void SaveImage(Canvas control, string path) { using (MemoryStream stream = new MemoryStream()) { GenerateImage(control, stream); System.Drawing.Bitmap img = AlphaBitmap.TryCreateAlphaBitmap(stream); if (_outputSelected == OutputSelector.Bitmap) { AlphaBitmap.SetTransparentRGB(img, TransparentRGBColor); } Save(img, path); } }
public ImageEditForm(TRibbonImage image, ImageFlags flags) : this() { Bitmap uIImage; _image = image; _flags = flags; _bitmap = new Bitmap(64, 64, PixelFormat.Format32bppArgb); ClearBitmap(_bitmap); _filename = image.Owner.BuildAbsoluteFilename(image.Source); if (File.Exists(_filename)) { uIImage = AlphaBitmap.TryAlphaBitmapFromFile(_filename); try { Graphics canvas = Graphics.FromImage(_bitmap); canvas.DrawImage(uIImage, new Point((64 - uIImage.Width) / 2, (64 - uIImage.Height) / 2)); canvas.Dispose(); } finally { uIImage.Dispose(); } } EditImageFile.Text = _image.Source; if (_image.MinDpi == 0) { ComboBoxMinDpi.SelectedIndex = 0; } else if (_image.MinDpi < 108) { ComboBoxMinDpi.SelectedIndex = 1; } else if (_image.MinDpi < 132) { ComboBoxMinDpi.SelectedIndex = 2; } else if (_image.MinDpi < 156) { ComboBoxMinDpi.SelectedIndex = 3; } else { ComboBoxMinDpi.SelectedIndex = 4; } EditResourceId.Value = _image.Id; EditSymbol.Text = _image.Symbol; UpdateControls(); }
private void SetImageItem(ListViewItem item, TRibbonImage image) { string filename; Bitmap uIImage; int imageIndex; Bitmap bitmap; filename = image.Owner.BuildAbsoluteFilename(image.Source); if (File.Exists(filename)) { uIImage = AlphaBitmap.BitmapFromFile(filename); try { if ((uIImage.Width == _imageList.ImageSize.Width) && (uIImage.Height == _imageList.ImageSize.Height)) { bitmap = new Bitmap(_imageList.ImageSize.Width, _imageList.ImageSize.Height, PixelFormat.Format32bppArgb); Graphics canvas = Graphics.FromImage(bitmap); canvas.DrawImage(uIImage, 0, 0, _imageList.ImageSize.Width, _imageList.ImageSize.Height); canvas.Dispose(); imageIndex = _imageList.Images.Add(bitmap, Color.Transparent); } else { bitmap = new Bitmap(_imageList.ImageSize.Width, _imageList.ImageSize.Height, PixelFormat.Format32bppArgb); try { if ((uIImage.Width <= _imageList.ImageSize.Width) && (uIImage.Height <= _imageList.ImageSize.Height)) { Graphics canvas = Graphics.FromImage(bitmap); canvas.DrawImage(uIImage, (_imageList.ImageSize.Width - uIImage.Width) / 2, (_imageList.ImageSize.Height - uIImage.Height) / 2); canvas.Dispose(); } else { Graphics canvas = Graphics.FromImage(bitmap); canvas.DrawImage(uIImage, 0, 0, _imageList.ImageSize.Width, _imageList.ImageSize.Height); canvas.Dispose(); } imageIndex = _imageList.Images.Add(bitmap, Color.Transparent); } finally { bitmap.Dispose(); } } } finally { uIImage.Dispose(); } } else { imageIndex = -1; } item.ImageIndex = imageIndex; item.SubItems.Clear(); item.SubItems.Add(image.MinDpi.ToString()); item.SubItems.Add(image.Id.ToString()); item.SubItems.Add(image.Symbol); item.SubItems.Add(image.Source); item.Tag = image; }
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(); } } }