当前位置: 首页>>代码示例>>C#>>正文


C# PixelFormat.IsDeepColor方法代码示例

本文整理汇总了C#中PixelFormat.IsDeepColor方法的典型用法代码示例。如果您正苦于以下问题:C# PixelFormat.IsDeepColor方法的具体用法?C# PixelFormat.IsDeepColor怎么用?C# PixelFormat.IsDeepColor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PixelFormat的用法示例。


在下文中一共展示了PixelFormat.IsDeepColor方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ChangePixelFormat

        /// <summary>
        /// Changes the pixel format.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="targetFormat">The target format.</param>
        /// <param name="quantizer">The color quantizer.</param>
        /// <returns>The converted image in a target format.</returns>
        public static Image ChangePixelFormat(this Image image, PixelFormat targetFormat, IColorQuantizer quantizer)
        {
            // checks for image validity
            if (image == null)
            {
                const String message = "Cannot change a pixel format for a null image.";
                throw new ArgumentNullException(message);
            }

            // checks whether a target format is supported
            if (!targetFormat.IsSupported())
            {
                String message = string.Format("A pixel format '{0}' is not supported.", targetFormat);
                throw new NotSupportedException(message);
            }

            // checks whether there is a quantizer for a indexed format
            if (targetFormat.IsIndexed() && quantizer == null)
            {
                String message = string.Format("A quantizer is cannot be null for indexed pixel format '{0}'.", targetFormat);
                throw new NotSupportedException(message);
            }

            // creates an image with the target format
            Bitmap result = new Bitmap(image.Width, image.Height, targetFormat);
            ColorPalette imagePalette = image.Palette;

            // gathers some information about the target format
            Boolean hasSourceAlpha = image.PixelFormat.HasAlpha();
            Boolean hasTargetAlpha = targetFormat.HasAlpha();
            Boolean isSourceIndexed = image.PixelFormat.IsIndexed();
            Boolean isTargetIndexed = targetFormat.IsIndexed();
            Boolean isSourceDeepColor = image.PixelFormat.IsDeepColor();
            Boolean isTargetDeepColor = targetFormat.IsDeepColor();

            // if palette is needed create one first
            if (isTargetIndexed)
            {
                quantizer.Prepare(image);
                image.AddColorsToQuantizer(quantizer);
                Int32 targetColorCount = result.GetPaletteColorCount();
                List<Color> palette = quantizer.GetPalette(targetColorCount);
                result.SetPalette(palette);
            }

            Action<Pixel, Pixel> changeFormat = (sourcePixel, targetPixel) =>
            {
                // if both source and target formats are deep color formats, copies a value directly
                if (isSourceDeepColor && isTargetDeepColor)
                {
                    UInt64 value = sourcePixel.Value;
                    targetPixel.SetValue(value);
                }
                else
                {
                    // retrieves a source image color
                    Color color = isSourceIndexed ? imagePalette.Entries[sourcePixel.Index] : sourcePixel.Color;

                    // if alpha is not present in the source image, but is present in the target, make one up
                    if (!hasSourceAlpha && hasTargetAlpha)
                    {
                        Int32 argb = 255 << 24 | color.R << 16 | color.G << 8 | color.B;
                        color = Color.FromArgb(argb);
                    }

                    // sets the color to a target pixel
                    if (isTargetIndexed)
                    {
                        // for the indexed images, determines a color from the octree
                        Byte paletteIndex = (Byte) quantizer.GetPaletteIndex(color);
                        targetPixel.SetIndex(paletteIndex);
                    }
                    else
                    {
                        // for the non-indexed images, sets the color directly
                        targetPixel.SetColor(color);
                    }
                }
            };

            // process image -> changes format
            image.ProcessImagePixels(result, changeFormat);

            // returns the image in the target format
            return result;
        }
开发者ID:googlecodetogithub,项目名称:denpa-qr-code-editor,代码行数:93,代码来源:Extend.Image.cs


注:本文中的PixelFormat.IsDeepColor方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。