2016-01-05 43 views
0

我想用指定的調色板創建3x1位圖。爲了測試,我使用了3種顏色:紅色,綠色和藍色。當我運行我的代碼時,我所得到的全是紅色(這是第一種顏色)。如何從pixeldata和調色板構建indexed8位圖

這是我的代碼

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

PixelFormats.Indexed8指的是8位或256色調色板。你的顏色是32位? – Kris

+0

@Kris是不是假設在調色板中存儲256種顏色,並且像素數據通過索引引用調色板中的顏色?這是我從文檔中理解的:https://msdn.microsoft.com/en-us/library/system.windows.media.pixelformats.indexed8%28v=vs.110%29.aspx – TizzyT455

回答

0

您提供INT爲pixelsource,和int爲32位,正確會提供字節:

的BitmapSource WB = BitmapSource.Create(3,1, 96,96,PixelFormats.Indexed8,PLT,新字節[] {0,1,2},3);從#C#

的問候,你應該留更長的時間才能得到幫助;)

+0

非常感謝這是的確是解決方案。看起來很好,我整天都在用這個愚蠢的錯誤撓撓我的腦袋:'( – TizzyT455