代码之家  ›  专栏  ›  技术社区  ›  TizzyT455

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

  •  0
  • TizzyT455  · 技术社区  · 9 年前

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

    这是我的密码

    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);
    }
    
    1 回复  |  直到 9 年前
        1
  •  0
  •   benneh    9 年前

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

    位图源wb=位图源。创建(3,1,96,96,PixelFormats.Indexed8,PLT,新字节[]{0,1,2},3);

    来自#c#的问候,您应该呆更长时间才能得到帮助;)