public void Save(Stream outputStream) { if (_iconData != null) { ArgumentNullException.ThrowIfNull(outputStream); outputStream.Write(_iconData, 0, _iconData.Length); } else { // 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 g = typeof(IPicture).GUID; IPicture picture = OleCreatePictureIndirect(pictdesc, ref g, false); if (picture != null) { try { ArgumentNullException.ThrowIfNull(outputStream); picture.SaveAsFile(new GPStream(outputStream, makeSeekable: false), -1, out int temp); } finally { Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); Marshal.ReleaseComObject(picture); } } } }
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); } } } }
public void Save(Stream outputStream) { if (_iconData != null) { outputStream.Write(_iconData, 0, _iconData.Length); } else { // 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 g = typeof(IPicture).GUID; IPicture picture = OleCreatePictureIndirect(pictdesc, ref g, false); if (picture != null) { try { // We threw this way on NetFX if (outputStream == null) { throw new ArgumentNullException("dataStream"); } picture.SaveAsFile(new GPStream(outputStream, makeSeekable: false), -1, out int temp); } finally { Marshal.ReleaseComObject(picture); } } } }
internal static extern IPicture OleCreatePictureIndirect(PICTDESC pictdesc, [In] ref Guid refiid, bool fOwn);