本文整理汇总了C#中System.Drawing.Bitmap.SetPalette方法的典型用法代码示例。如果您正苦于以下问题:C# Bitmap.SetPalette方法的具体用法?C# Bitmap.SetPalette怎么用?C# Bitmap.SetPalette使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Bitmap
的用法示例。
在下文中一共展示了Bitmap.SetPalette方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetPaletteTest
public void SetPaletteTest()
{
Bitmap Bitmap = new Bitmap(2, 2, PixelFormat.Format8bppIndexed);
var Colors = new Color[] {
Color.FromArgb(255, 0, 0, 0),
Color.FromArgb(255, 255, 255, 255),
Color.FromArgb(255, 255, 0, 0),
Color.FromArgb(255, 0, 255, 0),
Color.FromArgb(255, 0, 0, 255),
};
Bitmap.SetPalette(Colors);
Assert.Inconclusive();
}
示例2: InitSprite
private void InitSprite(Bitmap bmp, int cols, int rows)
{
if (bmp.PixelFormat != PixelFormat.Format8bppIndexed)
throw new Exception("Image must be 8bpp indexed");
var image = bmp.Bitmap8ToPixels();
var paleta = bmp.Palette;
var imgsz = bmp.Size;
var sz = new Size(imgsz.Width / cols, imgsz.Height / rows);
var rect = new Rectangle(Point.Empty, sz);
for (var f = 0; f < rows; f++)
{
for (var g = 0; g < cols; g++)
{
rect.Location = new Point(g * sz.Width, f * sz.Height);
var img = new Bitmap(rect.Width, rect.Height, PixelFormat.Format8bppIndexed);
img.SetPalette(paleta.Entries);
img.PixelsToBitmap8(rect, image);
RawSprites.Add(img);
}
}
}
示例3: TransformPerPixelAdvanced
public void TransformPerPixelAdvanced(PixelFormat targetFormat, List<Color> palette, out Image targetImage, IList<Point> path = null, Int32 parallelTaskCount = 4, params TransformPixelAdvancedFunction[] passes)
{
// checks parameters
Guard.CheckNull(targetFormat, "targetFormat");
// creates a target bitmap in an appropriate format
targetImage = new Bitmap(Width, Height, targetFormat);
// sets image palette if needed
if (targetFormat.IsIndexed()) targetImage.SetPalette(palette);
// wraps target image to a buffer
using (ImageBuffer target = new ImageBuffer(targetImage, ImageLockMode.WriteOnly))
{
TransformPerPixelBase(target, path, parallelTaskCount, passes);
}
}
示例4: ToIndexFormat
public static Bitmap ToIndexFormat(this Bitmap image, Color[] palette)
{
var sz = image.Size;
var res = new Bitmap(sz.Width, sz.Height, PixelFormat.Format8bppIndexed);
res.SetPalette(palette);
var bmpdataori = image.Bitmap32ToPixels();
var bmpdatadest = new byte[sz.Height][];
var dic = new Dictionary<int, int>();
for (var f = 0; f < sz.Height; f++)
{
var pixelsori = bmpdataori[f];
var pixeldest = ReduceLine(pixelsori, palette,dic);
bmpdatadest[f] = pixeldest;
Debug.Print($"Scanline {f} colors {dic.Count}");
}
res.PixelsToBitmap8(bmpdatadest);
return res;
}
示例5: 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;
}