public HRESULT Clone(out IStream stream) { // The cloned object should have the same current "position" stream = new GPStream(_dataStream) { _virtualPosition = _virtualPosition }; return(HRESULT.S_OK); }
public unsafe void Save(Stream outputStream) { if (_iconData != null) { outputStream.Write(_iconData, 0, _iconData.Length); } else { if (outputStream == null) { throw new ArgumentNullException(nameof(outputStream)); } // Ideally, we would pick apart the icon using // GetIconInfo, and then pull the individual bitmaps out, // converting them to DIBS and saving them into the file. // But, in the interest of simplicity, we just call to // OLE to do it for us. PICTDESC pictdesc = PICTDESC.CreateIconPICTDESC(Handle); Guid iid = DrawingCom.IPicture.IID; IntPtr lpPicture; Marshal.ThrowExceptionForHR(OleCreatePictureIndirect(&pictdesc, &iid, fOwn: 0, &lpPicture)); IntPtr streamPtr = IntPtr.Zero; try { // Use UniqueInstance here because we never want to cache the wrapper. It only gets used once and then disposed. using DrawingCom.IPicture picture = (DrawingCom.IPicture)DrawingCom.Instance .GetOrCreateObjectForComInstance(lpPicture, CreateObjectFlags.UniqueInstance); var gpStream = new GPStream(outputStream, makeSeekable: false); streamPtr = DrawingCom.Instance.GetOrCreateComInterfaceForObject(gpStream, CreateComInterfaceFlags.None); DrawingCom.ThrowExceptionForHR(picture.SaveAsFile(streamPtr, -1, null)); } finally { if (streamPtr != IntPtr.Zero) { int count = Marshal.Release(streamPtr); Debug.Assert(count == 0); } if (lpPicture != IntPtr.Zero) { int count = Marshal.Release(lpPicture); Debug.Assert(count == 0); } } } }
private static unsafe IntPtr LoadGdipImageFromStream(GPStream stream, bool useEmbeddedColorManagement) { using DrawingCom.IStreamWrapper streamWrapper = DrawingCom.GetComWrapper(stream); IntPtr image = IntPtr.Zero; if (useEmbeddedColorManagement) { Gdip.CheckStatus(Gdip.GdipLoadImageFromStreamICM(streamWrapper.Ptr, &image)); } else { Gdip.CheckStatus(Gdip.GdipLoadImageFromStream(streamWrapper.Ptr, &image)); } return(image); }
internal static IStreamWrapper GetComWrapper(GPStream stream) { return(new IStreamWrapper(Marshal.GetComInterfaceForObject <GPStream, Interop.Ole32.IStream>(stream))); }