If you are are playing with Andrew's VSTO Tutorial and are having with the image rendering you might have put your GetResourceBitmap function into your ImageConverterclass like i was doing issues (Thanks Kathleen McGrath for finding my mistake!!) i have pasted the completed code below
#Region "Usinng directives"
Imports SystemImports System.Collections.GenericImports System.DiagnosticsImports System.TextImports System.ReflectionImports System.Runtime.InteropServicesImports System.Windows.FormsImports Office = Microsoft.Office.Core
#End Region
#Region "service request implementation"
Partial Public Class ThisApplication
Protected Overrides Function RequestRibbonService() As Object Return New Ribbon1() End Function
End Class
<ComVisible(True)> _ Public Class Ribbon1 Implements Office.IRibbonExtensibility
#Region "Fields" Private ribbon As Office.IRibbonUI#End Region
#Region "Initialization"
Public Sub New() End Sub
Public Function GetCustomUI(ByVal ribbonID As String) As String Implements Office.IRibbonExtensibility.GetCustomUI Return GetResourceText("Ribbon1.xml") End Function
Public Sub OnLoad(ByVal ribbonUI As Office.IRibbonUI) Me.ribbon = ribbonUI End Sub
Public Sub OnToggleButton(ByVal control As Office.IRibbonControl, ByVal isPressed As Boolean) If (isPressed) Then MessageBox.Show("Hello World!") Else MessageBox.Show("Released!") End If
End Sub
Public Shared Function GetResourceBitmap( _ ByVal resourceName As String) As System.Drawing.Bitmap Dim asm As Assembly Dim resources As String() Dim image As System.Drawing.Bitmap
asm = Assembly.GetExecutingAssembly() resources = asm.GetManifestResourceNames() image = Nothing
For Each resource As String In resources If (resource.EndsWith(resourceName)) Then Dim stream As System.IO.Stream stream = asm.GetManifestResourceStream(resource) If (stream Is Nothing) Then Exit For End If
Dim extension As String extension = System.IO.Path.GetExtension(resourceName).ToLower() Select Case extension Case ".ico" image = New System.Drawing.Icon(stream).ToBitmap() Case ".jpg" Case ".bmp" Case Else image = New System.Drawing.Bitmap(stream) image.MakeTransparent() End Select
stream.Close() Exit For End If Next Return image End Function
Public Function GetImage(ByVal control As Office.IRibbonControl) As stdole.IPictureDisp Dim pictureDisp As stdole.IPictureDisp pictureDisp = Nothing
Select Case control.Id Case "toggleButton" pictureDisp = ImageConverter.Convert(GetResourceBitmap("blankcd.ico")) End Select Return pictureDisp End Function
#Region "Helpers" Public Shared Function GetResourceText(ByVal resourceName As String) As String Dim asm As Assembly = Assembly.GetExecutingAssembly() Dim resources As String() = asm.GetManifestResourceNames() For Each resource As String In resources If resource.EndsWith(resourceName) Then Dim resourceReader As System.IO.StreamReader resourceReader = New System.IO.StreamReader(asm.GetManifestResourceStream(resource)) If Not resourceReader Is Nothing Then Return resourceReader.ReadToEnd() End If End If Next Return Nothing End Function#End RegionEnd Class
Friend Class ImageConverter Inherits System.Windows.Forms.AxHost
Sub New() MyBase.New(Nothing) End Sub
Public Shared Function Convert(ByVal image As System.Drawing.Image) As stdole.IPictureDisp Return AxHost.GetIPictureDispFromPicture(image) End Function
Ribbon.xml
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="OnLoad"> <ribbon startFromScratch="false"> <tabs> <tab id="VSTO.Tab" label="Hello From Chuck" visible="1"> <group id="VSTO.Group" label="Hello Kathleen" visible="1"> <toggleButton id="toggleButton" label="Icon Here Now" screentip="Hello World Tooltip" onAction="OnToggleButton" size="large" getImage="GetImage"/>
</group> </tab> </tabs> </ribbon></customUI>