Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        /// <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);
                }
            }
        }
Esempio n. 3
0
        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);
                    }
                }
            }
        }
Esempio n. 4
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);
        }