一个很好用的在Office开发中图像转化成stdole模式的类

C#

// Microsoft Office Outlook 2007 Add-in Sample Code
//
// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// 
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Runtime.InteropServices;

public static class PictureDispConverter
{
	//IPictureDisp guid
	public static Guid iPictureDispGuid = typeof(stdole.IPictureDisp).GUID;

	/// Converts an Icon into a IPictureDisp
	public static stdole.IPictureDisp ToIPictureDisp(Icon icon)
	{
		PICTDESC.Icon pictIcon = new PICTDESC.Icon(icon);
		return PictureDispConverter.OleCreatePictureIndirect(pictIcon, ref iPictureDispGuid, true);
	}

	/// Converts an image into a IPictureDisp
	public static stdole.IPictureDisp ToIPictureDisp(Image image)
	{
		Bitmap bitmap = (image is Bitmap) ? (Bitmap)image : new Bitmap(image);
		PICTDESC.Bitmap pictBit = new PICTDESC.Bitmap(bitmap);
		return PictureDispConverter.OleCreatePictureIndirect(pictBit, ref iPictureDispGuid, true);
	}


	[DllImport("OleAut32.dll", EntryPoint = "OleCreatePictureIndirect", ExactSpelling = true, PreserveSig = false)]
	private static extern stdole.IPictureDisp OleCreatePictureIndirect([MarshalAs(UnmanagedType.AsAny)] object picdesc, ref Guid iid, bool fOwn);
	
	private readonly static HandleCollector handleCollector = new HandleCollector("Icon handles", 1000);

	// WINFORMS COMMENT:
	// PICTDESC is a union in native, so we'll just
	// define different ones for the different types
	// the "unused" fields are there to make it the right
	// size, since the struct in native is as big as the biggest
	// union.
	private static class PICTDESC
	{
		//Picture Types
		public const short PICTYPE_UNINITIALIZED = -1;
		public const short PICTYPE_NONE = 0;
		public const short PICTYPE_BITMAP = 1;
		public const short PICTYPE_METAFILE = 2;
		public const short PICTYPE_ICON = 3;
		public const short PICTYPE_ENHMETAFILE = 4;

		[StructLayout(LayoutKind.Sequential)]
		public class Icon
		{
			internal int cbSizeOfStruct = Marshal.SizeOf(typeof(PICTDESC.Icon));
			internal int picType = PICTDESC.PICTYPE_ICON;
			internal IntPtr hicon = IntPtr.Zero;
			internal int unused1 = 0;
			internal int unused2 = 0;

			internal Icon(System.Drawing.Icon icon)
			{
				this.hicon = icon.ToBitmap().GetHicon();
			}
		}

		[StructLayout(LayoutKind.Sequential)]
		public class Bitmap
		{
			internal int cbSizeOfStruct = Marshal.SizeOf(typeof(PICTDESC.Bitmap));
			internal int picType = PICTDESC.PICTYPE_BITMAP;
			internal IntPtr hbitmap = IntPtr.Zero;
			internal IntPtr hpal = IntPtr.Zero;
			internal int unused = 0;

			internal Bitmap(System.Drawing.Bitmap bitmap)
			{
				this.hbitmap = bitmap.GetHbitmap();
			}
		}
	}
}

VB.NET

' Microsoft Office Outlook 2007 Add-in Sample Code
'
' THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
' KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
' IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' 
Imports System
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Runtime.InteropServices

Public Module PictureDispConverter

    'IPictureDisp guid
    Public iPictureDispGuid As Guid = GetType(stdole.IPictureDisp).GUID

    'Converts an Icon into a IPictureDisp
    Public Function ToIPictureDisp(ByVal ico As Icon) As stdole.IPictureDisp

        Dim pictIcon As New PICTDESC.Icon(ico)
        Return PictureDispConverter.OleCreatePictureIndirect(pictIcon, iPictureDispGuid, True)
    End Function

    'Converts an image into a IPictureDisp
    Public Function ToIPictureDisp(ByVal picture As Image) As stdole.IPictureDisp

        Dim bm As Bitmap
        If TypeOf picture Is Bitmap Then
            bm = picture
        Else
            bm = New Bitmap(picture)
        End If
        Dim pictBit As New PICTDESC.Bitmap(bm)
        Return PictureDispConverter.OleCreatePictureIndirect(pictBit, iPictureDispGuid, True)
    End Function


    <DllImport("OleAut32.dll", EntryPoint:="OleCreatePictureIndirect", ExactSpelling:=True, PreserveSig:=False)> _
    Private Function OleCreatePictureIndirect(<MarshalAs(UnmanagedType.AsAny)> ByVal picdesc As Object, ByRef iid As Guid, ByVal fOwn As Boolean) As stdole.IPictureDisp
    End Function

    Private ReadOnly hCollector As New HandleCollector("Icon handles", 1000)


    'PICTDESC is a union in native, so we'll just
    'define different ones for the different types
    'the "unused" fields are there to make it the right
    'size, since the struct in native is as big as the biggest
    'union.
    Private Class PICTDESC

        'Picture Types
        Public Const PICTYPE_UNINITIALIZED As Short = -1
        Public Const PICTYPE_NONE As Short = 0
        Public Const PICTYPE_BITMAP As Short = 1
        Public Const PICTYPE_METAFILE As Short = 2
        Public Const PICTYPE_ICON As Short = 3
        Public Const PICTYPE_ENHMETAFILE As Short = 4

        <StructLayout(LayoutKind.Sequential)> _
        Public Class Icon

            Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Icon))
            Friend picType As Integer = PICTDESC.PICTYPE_ICON
            Friend hicon As IntPtr = IntPtr.Zero
            Friend unused1 As Integer = 0
            Friend unused2 As Integer = 0

            Friend Sub New(ByVal icon As System.Drawing.Icon)
                Me.hicon = icon.ToBitmap().GetHicon()
            End Sub

        End Class

        <StructLayout(LayoutKind.Sequential)> _
        Public Class Bitmap

            Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Bitmap))
            Friend picType As Integer = PICTDESC.PICTYPE_BITMAP
            Friend hbitmap As IntPtr = IntPtr.Zero
            Friend hpal As IntPtr = IntPtr.Zero
            Friend unused As Integer = 0

            Friend Sub New(ByVal bitmap As System.Drawing.Bitmap)
                Me.hbitmap = bitmap.GetHbitmap()
            End Sub
        End Class

    End Class

End Module



你可能感兴趣的:(Office)