public unsafe Bitmap(Stream stream, bool useIcm) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } using DrawingCom.IStreamWrapper streamWrapper = DrawingCom.GetComWrapper(new GPStream(stream)); IntPtr bitmap = IntPtr.Zero; if (useIcm) { Gdip.CheckStatus(Gdip.GdipCreateBitmapFromStreamICM(streamWrapper.Ptr, &bitmap)); } else { Gdip.CheckStatus(Gdip.GdipCreateBitmapFromStream(streamWrapper.Ptr, &bitmap)); } ValidateImage(bitmap); SetNativeImage(bitmap); EnsureSave(this, null, stream); }
/// <summary> /// Saves this <see cref='Image'/> to the specified stream in the specified format. /// </summary> public void Save(Stream stream, ImageCodecInfo encoder, EncoderParameters?encoderParams) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (encoder == null) { throw new ArgumentNullException(nameof(encoder)); } IntPtr encoderParamsMemory = IntPtr.Zero; if (encoderParams != null) { _rawData = null; encoderParamsMemory = encoderParams.ConvertToMemory(); } try { Guid g = encoder.Clsid; bool saved = false; if (_rawData != null) { ImageCodecInfo?rawEncoder = RawFormat.FindEncoder(); if (rawEncoder != null && rawEncoder.Clsid == g) { stream.Write(_rawData, 0, _rawData.Length); saved = true; } } if (!saved) { using DrawingCom.IStreamWrapper streamWrapper = DrawingCom.GetComWrapper(new GPStream(stream, makeSeekable: false)); unsafe { Gdip.CheckStatus(Gdip.GdipSaveImageToStream( new HandleRef(this, nativeImage), streamWrapper.Ptr, &g, new HandleRef(encoderParams, encoderParamsMemory))); } } } finally { if (encoderParamsMemory != IntPtr.Zero) { Marshal.FreeHGlobal(encoderParamsMemory); } } }
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); }