简体   繁体   English

如何从pixeldata和调色板构造indexed8位图

[英]How to construct indexed8 bitmap from pixeldata and palette

I am trying to create a 3x1 bitmap with a specified palette. 我正在尝试使用指定的调色板创建3x1位图。 For testing I used 3 colors: red, green and blue. 为了测试,我使用了3种颜色:红色,绿色和蓝色。 When I run my code all I get is all red (which is the first color). 运行代码时,我得到的全是红色(这是第一种颜色)。

This is my code 这是我的代码

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);
}

you provide int as the pixelsource, and ints are 32bit, correct would be providing bytes: 您提供int作为pixelsource,并且int是32bit,正确的方法是提供字节:

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

greetings from #c#, you should stay longer to receive help ;) 来自#c#的问候,您应该待更长的时间才能获得帮助;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM