我有一张8位PNG图片(见附件)。但是当我使用 Image.FromFile 方法读取它时,像素格式变成了32位。因此,我无法修改调色板。
请帮助我。
以下是我用于读取文件并更新调色板的代码:
请帮助我。
以下是我用于读取文件并更新调色板的代码:
public static Image GetPreviewImage()
{
Bitmap updatedImage = null;
try
{
// Reads the colors as a byte array
byte[] paletteBytes = FetchColorPallette();
updatedImage = Image.FromFile(@"C:\Screen-SaverBouncing.png");
ColorPalette colorPalette = updatedImage.Palette;
int j = 0;
if (colorPalette.Entries.Length > 0)
{
for (int i = 0; i < paletteBytes.Length / 4; i++)
{
Byte AValue = Convert.ToByte(paletteBytes[j]);
Byte RValue = Convert.ToByte(paletteBytes[j + 1]);
Byte GValue = Convert.ToByte(paletteBytes[j + 2]);
Byte BValue = Convert.ToByte(paletteBytes[j + 3]);
j += 4;
colorPalette.Entries[i] = Color.FromArgb(AValue, RValue, GValue, BValue);
}
updatedImage.Palette = colorPalette; ;
}
return updatedImage;
}
catch
{
throw;
}
}