本文整理汇总了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;
}