public static extern int OleCreateFromFile([In] ref Guid rclsid, [MarshalAs(UnmanagedType.LPWStr)] string lpszFileName, [In] ref Guid riid, uint renderopt, ref FORMATETC pFormatEtc, IOleClientSite pClientSite, IStorage pStg, [MarshalAs(UnmanagedType.IUnknown)] out object ppvObj);
public bool InsertImageFromFile(string strFilename, int position) { ILockBytes bytes; IStorage storage; IOleClientSite site; object obj2; NativeMethods.CreateILockBytesOnHGlobal(IntPtr.Zero, true, out bytes); NativeMethods.StgCreateDocfileOnILockBytes(bytes, 0x1012, 0, out storage); IRichEditOle.GetClientSite(out site); FORMATETC pFormatEtc = new FORMATETC(); pFormatEtc.cfFormat = (CLIPFORMAT)0; pFormatEtc.ptd = IntPtr.Zero; pFormatEtc.dwAspect = DVASPECT.DVASPECT_CONTENT; pFormatEtc.lindex = -1; pFormatEtc.tymed = TYMED.TYMED_NULL; Guid riid = new Guid("{00000112-0000-0000-C000-000000000046}"); Guid rclsid = new Guid("{00000000-0000-0000-0000-000000000000}"); NativeMethods.OleCreateFromFile(ref rclsid, strFilename, ref riid, 1, ref pFormatEtc, site, storage, out obj2); if (obj2 == null) { Marshal.ReleaseComObject(bytes); Marshal.ReleaseComObject(site); Marshal.ReleaseComObject(storage); return false; } IOleObject pUnk = (IOleObject)obj2; Guid pClsid = new Guid(); pUnk.GetUserClassID(ref pClsid); NativeMethods.OleSetContainedObject(pUnk, true); REOBJECT lpreobject = new REOBJECT(); lpreobject.posistion = position; lpreobject.clsid = pClsid; lpreobject.pstg = storage; lpreobject.poleobj = Marshal.GetIUnknownForObject(pUnk); lpreobject.polesite = site; lpreobject.dvAspect = 1; lpreobject.dwFlags = 2; lpreobject.dwUser = 0; IRichEditOle.InsertObject(lpreobject); Marshal.ReleaseComObject(bytes); Marshal.ReleaseComObject(site); Marshal.ReleaseComObject(storage); Marshal.ReleaseComObject(pUnk); return true; }