int width = bitmap.Width;
int height = bitmap.Height;
Bitmap bitmap8bits = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
ColorPalette pal = bitmap8bits.Palette;
byte[] tab = new byte[6] { 0, 51, 102, 153, 204, 255 };
for (int b = 0; b < 6; b++)
for (int g = 0; g < 6; g++)
for (int r = 0; r < 6; r++)
{
pal.Entries[(36 * r) + (6 * g) + b] =
Color.FromArgb(255, tab[r], tab[g], tab[b]);
}
bitmap8bits.Palette = pal;// Reaffectation sinon la palette n'est pas modifie dans l'image
Dim width As Integer = bitmap.Width
Dim height As Integer = bitmap.Height
Dim bitmap8bits As Bitmap = New Bitmap(width, height, PixelFormat.Format8bppIndexed)
Dim pal As ColorPalette = bitmap8bits.Palette
Dim tab() As Byte = {0, 51, 102, 153, 204, 255}
Dim b, g, r As Integer
For b = 0 To 5
For g = 0 To 5
For r = 0 To 5
pal.Entries((36 * r) + (6 * g) + b) = Color.FromArgb(255, tab(r), tab(g), tab(b))
Next
bitmap8bits.Palette = pal 'ne pas oublier de reaffecter la palette l'image
private ColorPalette _GetColorPalette()
int num1 = -1;
int num2 = SafeNativeMethods.Gdip.GdipGetImagePaletteSize(new HandleRef(this, this.nativeImage), out num1);
if (num2 != 0)
throw SafeNativeMethods.Gdip.StatusException(num2);
ColorPalette palette1 = new ColorPalette(num1);
IntPtr ptr1 = Marshal.AllocHGlobal(num1);
num2 = SafeNativeMethods.Gdip.GdipGetImagePalette(new HandleRef(this, this.nativeImage), ptr1, num1);
try
palette1.ConvertFromMemory(ptr1);
finally
Marshal.FreeHGlobal(ptr1);
return palette1;
Private Function _GetColorPalette() As ColorPalette
Dim num1 As Integer = -1
Dim num2 As Integer = Gdip.GdipGetImagePaletteSize(New HandleRef(Me, Me.nativeImage), num1)
If (num2 <> 0) Then
Throw Gdip.StatusException(num2)
End If
Dim palette1 As New ColorPalette(num1)
Dim ptr1 As IntPtr = Marshal.AllocHGlobal(num1)
num2 = Gdip.GdipGetImagePalette(New HandleRef(Me, Me.nativeImage), ptr1, num1)
Try
palette1.ConvertFromMemory(ptr1)
Finally
Marshal.FreeHGlobal(ptr1)
End Try
Return palette1
End Function
//table de pre-calcul
Byte[] canalTab = new byte[256];
int m;
for (int i = 0; i < 256; i++)
canalTab[i] = (byte)(i / 51);
unsafe
BitmapData bmpDataOld = bitmap.LockBits(new Rectangle(0, 0,
width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
BitmapData bmpDataNew = bitmap8bits.LockBits(new Rectangle(0,
0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
byte* oldPixel = (byte*)(void*)bmpDataOld.Scan0;
byte* newPixel = (byte*)(void*)bmpDataNew.Scan0;
int offsetNew = bmpDataNew.Stride - (width);
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
newPixel[0] = (byte)((36 * canalTab[oldPixel[2]]) +
(6 * canalTab[oldPixel[1]]) + canalTab[oldPixel[0]]);
oldPixel += 4;
newPixel++;
newPixel += offsetNew;
bitmap8bits.UnlockBits(bmpDataNew);
bitmap.UnlockBits(bmpDataOld);
'table de pre-calcul
Dim canalTab(256) As Byte
Dim i As Integer
For i = 0 To 255
canalTab(i) = CByte(i / 51)
Dim bmpDataOld As BitmapData = bitmap.LockBits(New Rectangle(0, 0, width, height) _
, System.Drawing.Imaging.ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb)
Dim bmpDataNew As BitmapData = bitmap8bits.LockBits(New Rectangle(0, 0, width, height) _
, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed)
Dim oldPixel(width * height * 4 - 1) As Byte
Marshal.Copy(bmpDataOld.Scan0, oldPixel, 0, oldPixel.Length)
Dim newPixel(width * height - 1) As Byte
'Pas necessaire de recuperer les donnees de l'image 8 bits vierge par un Marshal.Copy
Dim loc, x, y As Integer
For y = 0 To height - 1
For x = 0 To width - 1
loc = width * y + x
newPixel(loc) = (36 * canalTab(oldPixel(loc * 4 + 2)) _
+ (6 * canalTab(oldPixel(loc * 4 + 1))) _
+ canalTab(oldPixel(loc * 4)))
Marshal.Copy(newPixel, 0, bmpDataNew.Scan0, newPixel.Length)
bitmap8bits.UnlockBits(bmpDataNew)
bitmap.UnlockBits(bmpDataOld)