public static byte[] GetIconBytes(Bitmap bitmap) { List <byte> result = new List <byte>(); // Get palette ushort[] colors_short = new ushort[16]; Color[] colors = new Color[16]; if (bitmap.PixelFormat != PixelFormat.Format4bppIndexed) { //Console.WriteLine("Icon is {0}, converting to {1} (loss of quality possible)", bitmap.PixelFormat.ToString(), PixelFormat.Format4bppIndexed.ToString()); var quantizer = new PnnQuant.PnnQuantizer(); bitmap = quantizer.QuantizeImage(bitmap, PixelFormat.Format4bppIndexed, 16, true); } for (int u = 0; u < 16; u++) { colors[u] = bitmap.Palette.Entries[u]; int a = colors[u].A / 16; int r = colors[u].R / 16; int g = colors[u].G / 16; int b = colors[u].B / 16; colors_short[u] = (ushort)(a << 12 | r << 8 | g << 4 | b); //Console.WriteLine("Color to binary {0} ({1}) : {2} comp A:{3} R:{4} G:{5} B:{6}", u, colors[u].ToString(), colors_short[u].ToString("X"), a.ToString("X"),r.ToString("X"), g.ToString("X"), b.ToString("X")); result.AddRange(BitConverter.GetBytes(colors_short[u])); //Console.WriteLine("Color {0}:{1}", u, colors[u].ToString()); } // Get colors byte[] image = new byte[1024]; for (int y = 0; y < 32; y++) { for (int x = 0; x < 32; x++) { for (byte c = 0; c < 16; c++) { if (bitmap.GetPixel(x, y) == colors[c]) { image[y * 32 + x] = c; break; } } } } byte[] squeeze = new byte[512]; for (int u = 0; u < 1024; u++) { if (u % 2 == 0) { squeeze[u / 2] = (byte)(image[u] << 4 | image[u + 1] & 0xF); //Console.WriteLine("Byte {0}: {1} / {2}", image[u], image[u] << 4, image[u + 1] & 0xF); } } result.AddRange(squeeze); return(result.ToArray()); }
private void buttonLoadIcon_Click(object sender, EventArgs e) { using (OpenFileDialog od = new OpenFileDialog() { DefaultExt = "bmp", Filter = "Bitmap Files|*.bmp;*.png;*.jpg;*.gif|All Files|*.*" }) if (od.ShowDialog(this) == DialogResult.OK) { Bitmap bitmap = new Bitmap(od.FileName); if (bitmap.PixelFormat != PixelFormat.Format4bppIndexed) { var quantizer = new PnnQuant.PnnQuantizer(); bitmap = quantizer.QuantizeImage(bitmap, PixelFormat.Format4bppIndexed, 16, true); } meta.Icon = bitmap; pictureBoxDLCicon.Image = ScalePreview(meta.Icon, pictureBoxDLCicon, checkBoxZoom.Checked); } }