Ejemplo n.º 1
0
        protected override bool ReleaseHandle()
        {
            bool result;

            try
            {
                if (!this.IsInvalid)
                {
                    int dwCookie = this.handle.ToInt32();
                    this.handle = IntPtr.Zero;
                    try
                    {
                        this._cp.Unadvise(dwCookie);
                    }
                    finally
                    {
                        Utility.SafeRelease <IConnectionPoint>(ref this._cp);
                    }
                }
                result = true;
            }
            catch
            {
                result = false;
            }
            return(result);
        }
Ejemplo n.º 2
0
        public SafeConnectionPointCookie(IConnectionPointContainer target, object sink, Guid eventId) : base(true)
        {
            Verify.IsNotNull <IConnectionPointContainer>(target, "target");
            Verify.IsNotNull <object>(sink, "sink");
            Verify.IsNotDefault <Guid>(eventId, "eventId");
            this.handle = IntPtr.Zero;
            IConnectionPoint connectionPoint = null;

            try
            {
                target.FindConnectionPoint(ref eventId, out connectionPoint);
                int num;
                connectionPoint.Advise(sink, out num);
                if (num == 0)
                {
                    throw new InvalidOperationException("IConnectionPoint::Advise returned an invalid cookie.");
                }
                this.handle     = new IntPtr(num);
                this._cp        = connectionPoint;
                connectionPoint = null;
            }
            finally
            {
                Utility.SafeRelease <IConnectionPoint>(ref connectionPoint);
            }
        }
Ejemplo n.º 3
0
        protected override bool ReleaseHandle()
        {
            bool flag;

            try
            {
                if (!this.IsInvalid)
                {
                    int num = this.handle.ToInt32();
                    this.handle = IntPtr.Zero;
                    try
                    {
                        this._cp.Unadvise(num);
                    }
                    finally
                    {
                        Utility.SafeRelease <IConnectionPoint>(ref this._cp);
                    }
                }
                flag = true;
            }
            catch
            {
                flag = false;
            }
            return(flag);
        }
Ejemplo n.º 4
0
 public override void Close()
 {
     if (this._source != null)
     {
         Utility.SafeRelease <IStream>(ref this._source);
     }
 }
Ejemplo n.º 5
0
        // Experimentally, the base class seems to deal with the IDisposable pattern.
        // Overridden implementations aren't called, but Close is as part of the Dispose call.
        public override void Close()
        {
            if (null != _source)
            {
#if FEATURE_MUTABLE_COM_STREAMS
                Flush();
#endif
                Utility.SafeRelease(ref _source);
            }
        }
Ejemplo n.º 6
0
        private static SafeHBITMAP _CreateHBITMAPFromImageStream(Stream imgStream, out Size bitmapSize)
        {
            IWICImagingFactory    pImagingFactory = null;
            IWICBitmapDecoder     pDecoder        = null;
            IWICStream            pStream         = null;
            IWICBitmapFrameDecode pDecodedFrame   = null;
            IWICFormatConverter   pBitmapSourceFormatConverter = null;
            IWICBitmapFlipRotator pBitmapFlipRotator           = null;

            SafeHBITMAP hbmp = null;

            try
            {
                using (var istm = new ManagedIStream(imgStream))
                {
                    pImagingFactory = CLSID.CoCreateInstance <IWICImagingFactory>(CLSID.WICImagingFactory);
                    pStream         = pImagingFactory.CreateStream();
                    pStream.InitializeFromIStream(istm);

                    // Create an object that will decode the encoded image
                    Guid vendor = Guid.Empty;
                    pDecoder = pImagingFactory.CreateDecoderFromStream(pStream, ref vendor, WICDecodeMetadata.CacheOnDemand);

                    pDecodedFrame = pDecoder.GetFrame(0);
                    pBitmapSourceFormatConverter = pImagingFactory.CreateFormatConverter();

                    // Convert the image from whatever format it is in to 32bpp premultiplied alpha BGRA
                    Guid pixelFormat = WICPixelFormat.WICPixelFormat32bppPBGRA;
                    pBitmapSourceFormatConverter.Initialize(pDecodedFrame, ref pixelFormat, WICBitmapDitherType.None, IntPtr.Zero, 0, WICBitmapPaletteType.Custom);

                    pBitmapFlipRotator = pImagingFactory.CreateBitmapFlipRotator();
                    pBitmapFlipRotator.Initialize(pBitmapSourceFormatConverter, WICBitmapTransform.FlipVertical);

                    int width, height;
                    pBitmapFlipRotator.GetSize(out width, out height);

                    bitmapSize = new Size {
                        Width = width, Height = height
                    };

                    var bmi = new BITMAPINFO
                    {
                        bmiHeader = new BITMAPINFOHEADER
                        {
                            biSize        = Marshal.SizeOf(typeof(BITMAPINFOHEADER)),
                            biWidth       = width,
                            biHeight      = height,
                            biPlanes      = 1,
                            biBitCount    = 32,
                            biCompression = BI.RGB,
                            biSizeImage   = (width * height * 4),
                        },
                    };

                    // Create a 32bpp DIB.  This DIB must have an alpha channel for UpdateLayeredWindow to succeed.
                    IntPtr pBitmapBits;
                    hbmp = NativeMethods.CreateDIBSection(null, ref bmi, out pBitmapBits, IntPtr.Zero, 0);

                    // Copy the decoded image to the new buffer which backs the HBITMAP
                    var rect = new WICRect {
                        X = 0, Y = 0, Width = width, Height = height
                    };
                    pBitmapFlipRotator.CopyPixels(ref rect, width * 4, bmi.bmiHeader.biSizeImage, pBitmapBits);

                    var ret = hbmp;
                    hbmp = null;
                    return(ret);
                }
            }
            finally
            {
                Utility.SafeRelease(ref pImagingFactory);
                Utility.SafeRelease(ref pDecoder);
                Utility.SafeRelease(ref pStream);
                Utility.SafeRelease(ref pDecodedFrame);
                Utility.SafeRelease(ref pBitmapFlipRotator);
                Utility.SafeRelease(ref pBitmapSourceFormatConverter);
                Utility.SafeDispose(ref hbmp);
            }
        }