Ribbon image resolution issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Dear friends, I have used the post at https://forums.autodesk.com/t5/revit-api-forum/ribbonbutton-largeimage-icon-resolution-issues/td-p/8... and created a button in Ribbon but the image does not respect transparency.
However, the image is a transparent png file. See attached. What am I doing wrong? Thanks.
public void CreateSimpleButton()
{
Autodesk.Windows.RibbonControl ribbonControl = Autodesk.Windows.ComponentManager.Ribbon;
RibbonTab Tab = new RibbonTab();
Tab.Title = "Test Ribbon";
Tab.Id = "TESTRIBBON_TAB_ID";
ribbonControl.Tabs.Add(Tab);
Autodesk.Windows.RibbonPanelSource srcPanel = new RibbonPanelSource();
srcPanel.Title = "Panel1";
RibbonPanel Panel = new RibbonPanel();
Panel.Source = srcPanel;
Tab.Panels.Add(Panel);
Autodesk.Windows.RibbonButton button1 = new RibbonButton();
button1.Text = "Button";
button1.Size = RibbonItemSize.Large;
BitmapImage bmi = new BitmapImage(new Uri("../../Resources/Apps.png", UriKind.Relative));
button1.ToolTip = "Do something fantastic";
button1.LargeImage = ScaledIcon(bmi, 32, 32);
button1.Image = ScaledIcon(bmi, 16, 16);
button1.ShowText = true;
button1.CommandParameter = "._LINE ";
button1.CommandHandler = new SimpleButtonCmdHandler();
srcPanel.Items.Add(button1);
Tab.IsActive = true;
}
public class SimpleButtonCmdHandler : System.Windows.Input.ICommand
{
public bool CanExecute(object parameter) => true;
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
if (parameter is RibbonButton)
{
string esc = "";
string cmds = (string)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("CMDNAMES");
if (cmds.Length > 0)
{
int cmdNum = cmds.Split(new char[] { '\'' }).Length;
for (int i = 0; i < cmdNum; i++)
esc += '\x03';
}
RibbonButton button = parameter as RibbonButton;
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
doc.SendStringToExecute(esc + button.CommandParameter, true, false, false);
}
}
}
static Bitmap BitmapImageToBitmap(BitmapImage bitmapImage)
{
using (MemoryStream outStream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapImage));
enc.Save(outStream);
Bitmap bitmap = new Bitmap(outStream);
return new Bitmap(bitmap);
}
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
static BitmapSource BitmapToBitmapSource(Bitmap bitmap)
{
IntPtr hBitmap = bitmap.GetHbitmap();
BitmapSource retval;
try
{
retval = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
finally
{
DeleteObject(hBitmap);
}
return retval;
}
static Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
{
var destRect = new System.Drawing.Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var g = Graphics.FromImage(destImage))
{
g.CompositingMode = CompositingMode.SourceCopy;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
g.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
static BitmapSource ScaledIcon(BitmapImage large_icon, int w, int h)
{
return BitmapToBitmapSource(ResizeImage(BitmapImageToBitmap(large_icon), w, h));
}
Link copied