Exemple #1
0
        internal static IntPtr InitFromStream(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentException("stream");
            }

            IntPtr imagePtr;
            Status 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);
            }

            if (GDIPlus.RunningOnUnix())
            {
                // 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.
                GDIPlus.GdiPlusStreamHelper sh = new GDIPlus.GdiPlusStreamHelper(stream, true);

                st = GDIPlus.GdipLoadImageFromDelegate_linux(sh.GetHeaderDelegate, sh.GetBytesDelegate,
                                                             sh.PutBytesDelegate, sh.SeekDelegate, sh.CloseDelegate, sh.SizeDelegate, out imagePtr);
            }
            else
            {
                st = GDIPlus.GdipLoadImageFromStream(new ComIStreamWrapper(stream), out imagePtr);
            }

            GDIPlus.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.ToNativePtr();
            }

            try
            {
                if (GDIPlus.RunningOnUnix())
                {
                    GDIPlus.GdiPlusStreamHelper sh = new GDIPlus.GdiPlusStreamHelper(stream, false);
                    st = GDIPlus.GdipSaveImageToDelegate_linux(nativeObject, sh.GetBytesDelegate, sh.PutBytesDelegate,
                                                               sh.SeekDelegate, sh.CloseDelegate, sh.SizeDelegate, ref guid, nativeEncoderParams);
                }
                else
                {
                    st = GDIPlus.GdipSaveImageToStream(new HandleRef(this, nativeObject),
                                                       new ComIStreamWrapper(stream), ref guid, new HandleRef(encoderParams, nativeEncoderParams));
                }
            }
            finally
            {
                if (nativeEncoderParams != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(nativeEncoderParams);
                }
            }

            GDIPlus.CheckStatus(st);
        }
Exemple #3
0
	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.ToNativePtr ();

		try {
			if (GDIPlus.RunningOnUnix ()) {
				GDIPlus.GdiPlusStreamHelper sh = new GDIPlus.GdiPlusStreamHelper (stream, false);
				st = GDIPlus.GdipSaveImageToDelegate_linux (nativeObject, sh.GetBytesDelegate, sh.PutBytesDelegate,
					sh.SeekDelegate, sh.CloseDelegate, sh.SizeDelegate, ref guid, nativeEncoderParams);
			} else {
				st = GDIPlus.GdipSaveImageToStream (new HandleRef (this, nativeObject), 
					new ComIStreamWrapper (stream), ref guid, new HandleRef (encoderParams, nativeEncoderParams));
			}
		}
		finally {
			if (nativeEncoderParams != IntPtr.Zero)
				Marshal.FreeHGlobal (nativeEncoderParams);
		}
		
		GDIPlus.CheckStatus (st);		
	}
Exemple #4
0
	internal static IntPtr InitFromStream (Stream stream)
	{
		if (stream == null)
			throw new ArgumentException ("stream");

		IntPtr imagePtr;
		Status 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);
		}

		if (GDIPlus.RunningOnUnix ()) {
			// 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.
			GDIPlus.GdiPlusStreamHelper sh = new GDIPlus.GdiPlusStreamHelper (stream, true);

			st = GDIPlus.GdipLoadImageFromDelegate_linux (sh.GetHeaderDelegate, sh.GetBytesDelegate,
				sh.PutBytesDelegate, sh.SeekDelegate, sh.CloseDelegate, sh.SizeDelegate, out imagePtr);
		} else {
			st = GDIPlus.GdipLoadImageFromStream (new ComIStreamWrapper (stream), out imagePtr);
		}

		return st == Status.Ok ? imagePtr : IntPtr.Zero;
	}