一个很好用的在Office开发中图像转化成stdole模式的类
来源:互联网 发布:jsaas 源码 编辑:程序博客网 时间:2024/06/04 20:41
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 guidpublic static Guid iPictureDispGuid = typeof(stdole.IPictureDisp).GUID;/// Converts an Icon into a IPictureDisppublic 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 IPictureDisppublic 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 Typespublic 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 SystemImports System.DrawingImports System.Collections.GenericImports System.Runtime.InteropServicesPublic 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 ClassEnd Module
- 一个很好用的在Office开发中图像转化成stdole模式的类
- 一个图像转化的问题
- C/C++ 中VS开发环境中开发项目时,很好用的一个宏
- 一个很好用的zip/unzip类
- 一个很好用的时间类
- 一个很好用的php分页类
- 一个很好用的反射工具类
- 介绍一个很好用的NIO Socket开发框架--Netty2
- 一个很好的2次开发组件
- 收藏一个很好的 IOS 开发 博客
- 一个很好用的软件
- 很好用的一个轮播图
- 一个很好的对话框类
- 一个很好的Matlab图像边缘检测程序
- 一个很好的开源图像处理软件--imageJ (2
- 一个很好用的图形显示控件(可以用于实时采集的图像显示)
- 注解中一个很好用的第三方类库
- JAVA中一个很好玩的类Runtime
- 温故而知新-归并排序(JAVA)
- .net服务器上,一个虚拟目录放多个网站的方法
- 屏幕亮度自动调节的实现
- IEEE 754 浮点数的表示精度探讨
- Array的push与unshift方法性能分析
- 一个很好用的在Office开发中图像转化成stdole模式的类
- Flex开发常遇问题
- 管理oracle clusterware (Voting Disk)
- Eclipse SWT
- Android中的控件
- c++primer 2/22---变量
- Linux中的platform
- 开始android游戏开发
- RTP/RTCP/RTSP