internal static IntPtr InitFromStream(Stream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } IntPtr imagePtr; int st; // Seeking required if (!stream.CanSeek) { byte[] buffer = new byte[256]; int index = 0; int count; do { if (buffer.Length < index + 256) { byte[] newBuffer = new byte[buffer.Length * 2]; Array.Copy(buffer, newBuffer, buffer.Length); buffer = newBuffer; } count = stream.Read(buffer, index, 256); index += count; }while (count != 0); stream = new MemoryStream(buffer, 0, index); } // Unix, with libgdiplus // We use a custom API for this, because there's no easy way // to get the Stream down to libgdiplus. So, we wrap the stream // with a set of delegates. GdiPlusStreamHelper sh = new GdiPlusStreamHelper(stream, true); st = Gdip.GdipLoadImageFromDelegate_linux(sh.GetHeaderDelegate, sh.GetBytesDelegate, sh.PutBytesDelegate, sh.SeekDelegate, sh.CloseDelegate, sh.SizeDelegate, out imagePtr); return(st == Gdip.Ok ? imagePtr : IntPtr.Zero); }
private protected static IntPtr InitializeFromStream(Stream stream) { if (stream == null) throw new ArgumentNullException(nameof(stream)); // Unix, with libgdiplus // We use a custom API for this, because there's no easy way // to get the Stream down to libgdiplus. So, we wrap the stream // with a set of delegates. GdiPlusStreamHelper sh = new GdiPlusStreamHelper(stream, true); int st = Gdip.GdipLoadImageFromDelegate_linux(sh.GetHeaderDelegate, sh.GetBytesDelegate, sh.PutBytesDelegate, sh.SeekDelegate, sh.CloseDelegate, sh.SizeDelegate, out IntPtr imagePtr); // Since we're just passing to native code the delegates inside the wrapper, we need to keep sh alive // to avoid the object being collected and therefore the delegates would be collected as well. GC.KeepAlive(sh); Gdip.CheckStatus(st); return imagePtr; }
public void Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) { Status st; IntPtr nativeEncoderParams; Guid guid = encoder.Clsid; if (encoderParams == null) { nativeEncoderParams = IntPtr.Zero; } else { nativeEncoderParams = encoderParams.ConvertToMemory(); } try { if (GDIPlus.RunningOnUnix()) { GdiPlusStreamHelper sh = new GdiPlusStreamHelper(stream, false); st = SafeNativeMethods.Gdip.GdipSaveImageToDelegate_linux(nativeObject, sh.GetBytesDelegate, sh.PutBytesDelegate, sh.SeekDelegate, sh.CloseDelegate, sh.SizeDelegate, ref guid, nativeEncoderParams); } else { st = SafeNativeMethods.Gdip.GdipSaveImageToStream(new HandleRef(this, nativeObject), new ComIStreamWrapper(stream), ref guid, new HandleRef(encoderParams, nativeEncoderParams)); } } finally { if (nativeEncoderParams != IntPtr.Zero) { Marshal.FreeHGlobal(nativeEncoderParams); } } SafeNativeMethods.Gdip.CheckStatus(st); }