Esempio n. 1
0
        /// <summary>
        /// Helper method that will retrieve ColorContexts from an unmanaged object (e.g. BitmapDecoder or BitmapFrameDecode)
        /// </summary>
        internal static IList <ColorContext> GetColorContextsHelper(GetColorContextsDelegate getColorContexts)
        {
            uint numContexts = 0;
            List <ColorContext> colorContextsList = null;

            int hr = getColorContexts(ref numContexts, null);

            if (hr != (int)WinCodecErrors.WINCODEC_ERR_UNSUPPORTEDOPERATION)
            {
                HRESULT.Check(hr);
            }

            if (numContexts > 0)
            {
                // GetColorContexts does not create new IWICColorContexts. Instead, it initializes existing
                // ones so we must create them beforehand.
                SafeMILHandle[] colorContextHandles = new SafeMILHandle[numContexts];

                using (FactoryMaker factoryMaker = new FactoryMaker())
                {
                    for (uint i = 0; i < numContexts; ++i)
                    {
                        HRESULT.Check(UnsafeNativeMethodsMilCoreApi.WICCodec.CreateColorContext(factoryMaker.ImagingFactoryPtr, out colorContextHandles[i]));
                    }
                }

                // The Marshal is unable to handle SafeMILHandle[] so we will convert it to an IntPtr[] ourselves.
                {
                    IntPtr[] colorContextPtrs = new IntPtr[numContexts];

                    for (uint i = 0; i < numContexts; ++i)
                    {
                        colorContextPtrs[i] = colorContextHandles[i].DangerousGetHandle();
                    }

                    HRESULT.Check(getColorContexts(ref numContexts, colorContextPtrs));
                }

                colorContextsList = new List <ColorContext>((int)numContexts);
                for (uint i = 0; i < numContexts; ++i)
                {
                    colorContextsList.Add(new ColorContext(colorContextHandles[i]));
                }
            }

            return(colorContextsList);
        }
Esempio n. 2
0
        internal static IList<ColorContext> GetColorContextsHelper(GetColorContextsDelegate getColorContexts)
        {
            uint numContexts = 0;
            List<ColorContext> colorContextsList = null;

            int hr = getColorContexts(ref numContexts, null);
            if (hr != (int)WinCodecErrors.WINCODEC_ERR_UNSUPPORTEDOPERATION)
            {
                HRESULT.Check(hr);
            }
            
            if (numContexts > 0)
            {
                // GetColorContexts does not create new IWICColorContexts. Instead, it initializes existing
                // ones so we must create them beforehand.
                SafeMILHandle[] colorContextHandles = new SafeMILHandle[numContexts];
                
                using (FactoryMaker factoryMaker = new FactoryMaker())
                {
                    for (uint i = 0; i < numContexts; ++i)
                    {
 
                        HRESULT.Check(UnsafeNativeMethodsMilCoreApi.WICCodec.CreateColorContext(factoryMaker.ImagingFactoryPtr, out colorContextHandles[i]));
                    }
                }

                // The Marshal is unable to handle SafeMILHandle[] so we will convert it to an IntPtr[] ourselves.
                {
                    IntPtr[] colorContextPtrs = new IntPtr[numContexts];
                    
                    for (uint i = 0; i < numContexts; ++i)
                    {
                        colorContextPtrs[i] = colorContextHandles[i].DangerousGetHandle();
                    }

                    HRESULT.Check(getColorContexts(ref numContexts, colorContextPtrs));
                }

                colorContextsList = new List<ColorContext>((int)numContexts);
                for (uint i = 0; i < numContexts; ++i)
                {
                    colorContextsList.Add(new ColorContext(colorContextHandles[i]));
                }
            }

            return colorContextsList;