Advertisement
Guest User

PictureDispConverter.cs

a guest
Apr 11th, 2013
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 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. using System;
  8. using System.Drawing;
  9. using System.Collections.Generic;
  10. using System.Runtime.InteropServices;
  11.  
  12. public static class PictureDispConverter
  13. {
  14.     //IPictureDisp guid
  15.     public static Guid iPictureDispGuid = typeof(stdole.IPictureDisp).GUID;
  16.  
  17.     /// Converts an Icon into a IPictureDisp
  18.     public static stdole.IPictureDisp ToIPictureDisp(Icon icon)
  19.     {
  20.         PICTDESC.Icon pictIcon = new PICTDESC.Icon(icon);
  21.         return PictureDispConverter.OleCreatePictureIndirect(pictIcon, ref iPictureDispGuid, true);
  22.     }
  23.  
  24.     /// Converts an image into a IPictureDisp
  25.     public static stdole.IPictureDisp ToIPictureDisp(Image image)
  26.     {
  27.         Bitmap bitmap = (image is Bitmap) ? (Bitmap)image : new Bitmap(image);
  28.         PICTDESC.Bitmap pictBit = new PICTDESC.Bitmap(bitmap);
  29.         return PictureDispConverter.OleCreatePictureIndirect(pictBit, ref iPictureDispGuid, true);
  30.     }
  31.  
  32.  
  33.     [DllImport("OleAut32.dll", EntryPoint = "OleCreatePictureIndirect", ExactSpelling = true, PreserveSig = false)]
  34.     private static extern stdole.IPictureDisp OleCreatePictureIndirect([MarshalAs(UnmanagedType.AsAny)] object picdesc, ref Guid iid, bool fOwn);
  35.    
  36.     private readonly static HandleCollector handleCollector = new HandleCollector("Icon handles", 1000);
  37.  
  38.     // WINFORMS COMMENT:
  39.     // PICTDESC is a union in native, so we'll just
  40.     // define different ones for the different types
  41.     // the "unused" fields are there to make it the right
  42.     // size, since the struct in native is as big as the biggest
  43.     // union.
  44.     private static class PICTDESC
  45.     {
  46.         //Picture Types
  47.         public const short PICTYPE_UNINITIALIZED = -1;
  48.         public const short PICTYPE_NONE = 0;
  49.         public const short PICTYPE_BITMAP = 1;
  50.         public const short PICTYPE_METAFILE = 2;
  51.         public const short PICTYPE_ICON = 3;
  52.         public const short PICTYPE_ENHMETAFILE = 4;
  53.  
  54.         [StructLayout(LayoutKind.Sequential)]
  55.         public class Icon
  56.         {
  57.             internal int cbSizeOfStruct = Marshal.SizeOf(typeof(PICTDESC.Icon));
  58.             internal int picType = PICTDESC.PICTYPE_ICON;
  59.             internal IntPtr hicon = IntPtr.Zero;
  60.             internal int unused1 = 0;
  61.             internal int unused2 = 0;
  62.  
  63.             internal Icon(System.Drawing.Icon icon)
  64.             {
  65.                 this.hicon = icon.ToBitmap().GetHicon();
  66.             }
  67.         }
  68.  
  69.         [StructLayout(LayoutKind.Sequential)]
  70.         public class Bitmap
  71.         {
  72.             internal int cbSizeOfStruct = Marshal.SizeOf(typeof(PICTDESC.Bitmap));
  73.             internal int picType = PICTDESC.PICTYPE_BITMAP;
  74.             internal IntPtr hbitmap = IntPtr.Zero;
  75.             internal IntPtr hpal = IntPtr.Zero;
  76.             internal int unused = 0;
  77.  
  78.             internal Bitmap(System.Drawing.Bitmap bitmap)
  79.             {
  80.                 this.hbitmap = bitmap.GetHbitmap();
  81.             }
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement