예제 #1
0
        /// <summary>
        /// Create a palette from the list of colors.
        /// </summary>
        public BitmapPalette(IList<Color> colors)
        {
            if (colors == null)
            {
                throw new ArgumentNullException("colors");
            }

            int count = colors.Count;

            if (count < 1 || count > 256)
            {
                throw new InvalidOperationException(SR.Get(SRID.Image_PaletteZeroColors, null));
            }

            Color[] colorArray = new Color[count];

            for (int i = 0; i < count; ++i)
            {
                colorArray[i] = colors[i];
            }

            _colors = new PartialList<Color>(colorArray);

            _palette = CreateInternalPalette();

            UpdateUnmanaged();
        }
예제 #2
0
        /// <summary>
        /// Create a palette from the list of colors.
        /// </summary>
        public BitmapPalette(IList <Color> colors)
        {
            if (colors == null)
            {
                throw new ArgumentNullException("colors");
            }

            int count = colors.Count;

            if (count < 1 || count > 256)
            {
                throw new InvalidOperationException(SR.Get(SRID.Image_PaletteZeroColors, null));
            }

            Color[] colorArray = new Color[count];

            for (int i = 0; i < count; ++i)
            {
                colorArray[i] = colors[i];
            }

            _colors = new PartialList <Color>(colorArray);

            _palette = CreateInternalPalette();

            UpdateUnmanaged();
        }
예제 #3
0
        private void UpdateManaged()
        {
            Debug.Assert(_palette != null && !_palette.IsInvalid);

            int numColors     = 0;
            int cActualColors = 0;

            HRESULT.Check(UnsafeNativeMethods.WICPalette.GetColorCount(_palette,
                                                                       out numColors));

            List <Color> colors = new List <Color>();

            if (numColors < 1 || numColors > 256)
            {
                throw new InvalidOperationException(SR.Get(SRID.Image_PaletteZeroColors, null));
            }
            else
            {
                ImagePaletteColor[] paletteColorArray = new ImagePaletteColor[numColors];
                unsafe
                {
                    fixed(void *paletteColorArrayPinned = paletteColorArray)
                    {
                        HRESULT.Check(UnsafeNativeMethods.WICPalette.GetColors(
                                          _palette,
                                          numColors,
                                          (IntPtr)paletteColorArrayPinned,
                                          out cActualColors));

                        Debug.Assert(cActualColors == numColors);
                    }
                }

                for (int i = 0; i < numColors; ++i)
                {
                    ImagePaletteColor c = paletteColorArray[i];

                    colors.Add(Color.FromArgb(c.A, c.R, c.G, c.B));
                }
            }

            _colors = new PartialList <Color>(colors);
        }
예제 #4
0
        private void UpdateManaged()
        {
            Debug.Assert(_palette != null && !_palette.IsInvalid);

            int numColors = 0;
            int cActualColors = 0;
            HRESULT.Check(UnsafeNativeMethods.WICPalette.GetColorCount(_palette,
                        out numColors));

            List<Color> colors = new List<Color>();

            if (numColors < 1 || numColors > 256)
            {
                throw new InvalidOperationException(SR.Get(SRID.Image_PaletteZeroColors, null));
            }
            else
            {
                ImagePaletteColor[] paletteColorArray = new ImagePaletteColor[numColors];
                unsafe
                {
                    fixed(void* paletteColorArrayPinned = paletteColorArray)
                    {
                        HRESULT.Check(UnsafeNativeMethods.WICPalette.GetColors(
                                    _palette,
                                    numColors,
                                    (IntPtr)paletteColorArrayPinned,
                                    out cActualColors));

                        Debug.Assert(cActualColors == numColors);
                    }
                }

                for (int i = 0; i < numColors; ++i)
                {
                    ImagePaletteColor c = paletteColorArray[i];

                    colors.Add(Color.FromArgb(c.A, c.R, c.G, c.B));
                }
            }

            _colors = new PartialList<Color>(colors);
        }