PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/IssueTrackerOutlookAddin/OUTLOOK-PLUS-SERVICES-ADDIN-LIBRARY/PictureDispConverter.cs

#
C# | 116 lines | 68 code | 12 blank | 36 comment | 2 complexity | 0f20cefe784da10ae2b5bcbe4bbc77f3 MD5 | raw file
  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. /// Outlook requires a specific image format which is different than BMP/JPG, etc.
  13. /// This object support conversion of standard types into the types required by Outlook
  14. ///
  15. namespace Microsoft.SoftwarePlusServices.ReferenceBits.OutlookPlusServices
  16. {
  17. ///
  18. /// <summary>
  19. /// Convers binary and image data to a IPictureDisp interface using Win32 API functions.
  20. /// </summary>
  21. ///
  22. public static class PictureDispConverter
  23. {
  24. #region Private Fields
  25. private static Guid _iPictureDispGuid = typeof(stdole.IPictureDisp).GUID;
  26. #endregion
  27. #region Public Methods
  28. ///
  29. /// <summary>
  30. /// Converts an Icon to a IPictureDisp interface.
  31. /// </summary>
  32. /// <param name="icon">The Icon object to convert.</param>
  33. /// <returns>The IPictureDisp interface representing the image.</returns>
  34. ///
  35. public static stdole.IPictureDisp ToIPictureDisp(Icon icon)
  36. {
  37. PICTDESC.Icon pictIcon = new PICTDESC.Icon(icon);
  38. return PictureDispConverter.OleCreatePictureIndirect(pictIcon, ref _iPictureDispGuid, true);
  39. }
  40. ///
  41. /// <summary>
  42. /// Converts an Image to a IPictureDisp interface.
  43. /// </summary>
  44. /// <param name="icon">The Image object to convert.</param>
  45. /// <returns>The IPictureDisp interface representing the image.</returns>
  46. ///
  47. public static stdole.IPictureDisp ToIPictureDisp(Image image)
  48. {
  49. Bitmap bitmap = image as Bitmap;
  50. if (bitmap == null)
  51. bitmap = new Bitmap(image);
  52. PICTDESC.Bitmap pictBit = new PICTDESC.Bitmap(bitmap);
  53. return PictureDispConverter.OleCreatePictureIndirect(pictBit, ref _iPictureDispGuid, true);
  54. }
  55. #endregion
  56. #region Win32 Imports
  57. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass")]
  58. [DllImport("OleAut32.dll", EntryPoint = "OleCreatePictureIndirect", ExactSpelling = true, PreserveSig = false)]
  59. private static extern stdole.IPictureDisp OleCreatePictureIndirect([MarshalAs(UnmanagedType.AsAny)] object picdesc, ref Guid iid, [MarshalAs(UnmanagedType.Bool)] bool fOwn);
  60. // WINFORMS COMMENT:
  61. // PICTDESC is a union in native, so we'll just
  62. // define different ones for the different types
  63. // the "unused" fields are there to make it the right
  64. // size, since the struct in native is as big as the biggest
  65. // union.
  66. private static class PICTDESC
  67. {
  68. //Picture Types
  69. public const short PICTYPE_UNINITIALIZED = -1;
  70. public const short PICTYPE_NONE = 0;
  71. public const short PICTYPE_BITMAP = 1;
  72. public const short PICTYPE_METAFILE = 2;
  73. public const short PICTYPE_ICON = 3;
  74. public const short PICTYPE_ENHMETAFILE = 4;
  75. [StructLayout(LayoutKind.Sequential)]
  76. public class Icon
  77. {
  78. internal int cbSizeOfStruct = Marshal.SizeOf(typeof(PICTDESC.Icon));
  79. internal int picType = PICTDESC.PICTYPE_ICON;
  80. internal IntPtr hicon = IntPtr.Zero;
  81. internal int unused1;
  82. internal int unused2;
  83. internal Icon(System.Drawing.Icon icon)
  84. {
  85. this.hicon = icon.ToBitmap().GetHicon();
  86. }
  87. }
  88. [StructLayout(LayoutKind.Sequential)]
  89. public class Bitmap
  90. {
  91. internal int cbSizeOfStruct = Marshal.SizeOf(typeof(PICTDESC.Bitmap));
  92. internal int picType = PICTDESC.PICTYPE_BITMAP;
  93. internal IntPtr hbitmap = IntPtr.Zero;
  94. internal IntPtr hpal = IntPtr.Zero;
  95. internal int unused;
  96. internal Bitmap(System.Drawing.Bitmap bitmap)
  97. {
  98. this.hbitmap = bitmap.GetHbitmap();
  99. }
  100. }
  101. }
  102. #endregion
  103. }
  104. }