【问题标题】:How to construct indexed8 bitmap from pixeldata and palette如何从像素数据和调色板构造 indexed8 位图
【发布时间】:2016-01-05 03:21:55
【问题描述】:

我正在尝试使用指定的调色板创建 3x1 位图。为了测试,我使用了 3 种颜色:红色、绿色和蓝色。当我运行我的代码时,我得到的都是红色(这是第一种颜色)。

这是我的代码

public void CreateTestImg()
{
    // List to store colors for palette
    List<Color> Colors = new List<Color>();
    // Adds Red to the list
    Colors.Add(Media.Color.FromArgb(255, 255, 0, 0));
    // Adds Green to the list
    Colors.Add(Media.Color.FromArgb(255, 0, 255, 0));
    // Adds Blue to the list
    Colors.Add(Media.Color.FromArgb(255, 0, 0, 255));
    // Create a new palette with the list of colors as input
    BitmapPalette PLT = new BitmapPalette(Colors);
    // Make a Bitmap with the specified width, height, dpix, dpiy, pixelformat, palette, PixelData, stride.
    BitmapSource wb = BitmapSource.Create(3, 1, 96, 96, PixelFormats.Indexed8, PLT, {0, 1, 2}, 3);
    // Freezes the object
    wb.Freeze();
    // Creates a new brush from the Bitmap so I can draw with it later
    ImageBrush newBMB = new ImageBrush(wb);
    // Freezes the brush
    newBMB.Freeze();
    // Adds brush to dictionary for later referencing
    ImageBatch.Add("WHATEVER", newBMB);
}

【问题讨论】:

  • PixelFormats.Indexed8 指的是 8 位或 256 调色板。你的颜色是 32 位的?
  • @Kris 不是假设在调色板中存储 256 种颜色,并且像素数据通过索引引用调色板中的颜色吗?这就是我从文档中了解到的:msdn.microsoft.com/en-us/library/…

标签: c# wpf graphics palette bitmapsource


【解决方案1】:

您提供 int 作为像素源,并且 int 是 32 位的,正确的是提供字节:

BitmapSource wb = BitmapSource.Create(3,1, 96, 96, PixelFormats.Indexed8, PLT, new byte[] { 0, 1, 2 }, 3);

来自#c# 的问候,你应该多呆一会儿才能得到帮助;)

【讨论】:

  • 非常感谢,这确实是解决方案。好看,我整天都在挠头,真是个愚蠢的错误:'(
猜你喜欢
  • 1970-01-01
  • 2021-04-28
  • 2011-01-04
  • 1970-01-01
  • 2011-04-16
  • 2021-12-03
  • 1970-01-01
  • 2012-06-07
  • 2018-04-17
相关资源
最近更新 更多