Advertisement
Guest User

PictureDispConverter.vb

a guest
Apr 11th, 2013
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.29 KB | None | 0 0
  1. ' Microsoft Office Outlook 2007 Add-in Sample Code
  2. '
  3. ' THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
  4. ' KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. ' IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  6. '
  7. Imports System
  8. Imports System.Drawing
  9. Imports System.Collections.Generic
  10. Imports System.Runtime.InteropServices
  11.  
  12. Public Module PictureDispConverter
  13.  
  14.     'IPictureDisp guid
  15.     Public iPictureDispGuid As Guid = GetType(stdole.IPictureDisp).GUID
  16.  
  17.     'Converts an Icon into a IPictureDisp
  18.     Public Function ToIPictureDisp(ByVal ico As Icon) As stdole.IPictureDisp
  19.  
  20.         Dim pictIcon As New PICTDESC.Icon(ico)
  21.         Return PictureDispConverter.OleCreatePictureIndirect(pictIcon, iPictureDispGuid, True)
  22.     End Function
  23.  
  24.     'Converts an image into a IPictureDisp
  25.     Public Function ToIPictureDisp(ByVal picture As Image) As stdole.IPictureDisp
  26.  
  27.         Dim bm As Bitmap
  28.         If TypeOf picture Is Bitmap Then
  29.             bm = picture
  30.         Else
  31.             bm = New Bitmap(picture)
  32.         End If
  33.         Dim pictBit As New PICTDESC.Bitmap(bm)
  34.         Return PictureDispConverter.OleCreatePictureIndirect(pictBit, iPictureDispGuid, True)
  35.     End Function
  36.  
  37.  
  38.     <DllImport("OleAut32.dll", EntryPoint:="OleCreatePictureIndirect", ExactSpelling:=True, PreserveSig:=False)> _
  39.     Private Function OleCreatePictureIndirect(<MarshalAs(UnmanagedType.AsAny)> ByVal picdesc As Object, ByRef iid As Guid, ByVal fOwn As Boolean) As stdole.IPictureDisp
  40.     End Function
  41.  
  42.     Private ReadOnly hCollector As New HandleCollector("Icon handles", 1000)
  43.  
  44.  
  45.     'PICTDESC is a union in native, so we'll just
  46.     'define different ones for the different types
  47.     'the "unused" fields are there to make it the right
  48.     'size, since the struct in native is as big as the biggest
  49.     'union.
  50.     Private Class PICTDESC
  51.  
  52.         'Picture Types
  53.         Public Const PICTYPE_UNINITIALIZED As Short = -1
  54.         Public Const PICTYPE_NONE As Short = 0
  55.         Public Const PICTYPE_BITMAP As Short = 1
  56.         Public Const PICTYPE_METAFILE As Short = 2
  57.         Public Const PICTYPE_ICON As Short = 3
  58.         Public Const PICTYPE_ENHMETAFILE As Short = 4
  59.  
  60.         <StructLayout(LayoutKind.Sequential)> _
  61.         Public Class Icon
  62.  
  63.             Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Icon))
  64.             Friend picType As Integer = PICTDESC.PICTYPE_ICON
  65.             Friend hicon As IntPtr = IntPtr.Zero
  66.             Friend unused1 As Integer = 0
  67.             Friend unused2 As Integer = 0
  68.  
  69.             Friend Sub New(ByVal icon As System.Drawing.Icon)
  70.                 Me.hicon = icon.ToBitmap().GetHicon()
  71.             End Sub
  72.  
  73.         End Class
  74.  
  75.         <StructLayout(LayoutKind.Sequential)> _
  76.         Public Class Bitmap
  77.  
  78.             Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Bitmap))
  79.             Friend picType As Integer = PICTDESC.PICTYPE_BITMAP
  80.             Friend hbitmap As IntPtr = IntPtr.Zero
  81.             Friend hpal As IntPtr = IntPtr.Zero
  82.             Friend unused As Integer = 0
  83.  
  84.             Friend Sub New(ByVal bitmap As System.Drawing.Bitmap)
  85.                 Me.hbitmap = bitmap.GetHbitmap()
  86.             End Sub
  87.         End Class
  88.  
  89.     End Class
  90.  
  91. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement