Salut,
d'abord une fonction pour écrire dans un fichier,
Puis une fonction qui crée un tableau de byte de l'image passée en param.
Elle écrit ensuite les octets en visu binaire ou hexa au choix.
Attention, ce test utilise une image au format Format32bppArgb, ajuster au besoin (Format8bppIndexed)
private void LOGF(string sFile, string sText, bool bNewFile)
{
StreamWriter sw = null;
try
{
if (bNewFile) File.Delete(sFile);
sw = new StreamWriter(sFile, true);
sw.Write(sText);
}
catch (Exception) { /*...*/ }
finally { if( sw != null ) sw.Close(); }
}
private void LogBytesImage(Bitmap img, bool bVisuBin, string LOGFILE)
{
StringBuilder sb = new StringBuilder();
// Crée un tableau de bytes représentant l'image
BitmapData bmpData = img.LockBits(
new Rectangle(0, 0, img.Width, img.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format32bppArgb);
IntPtr ptr = bmpData.Scan0;
int depth = System.Drawing.Bitmap.GetPixelFormatSize(img.PixelFormat);
int step = depth / 8;
byte[] pixels = new byte[(bmpData.Height * bmpData.Width) * step];
Marshal.Copy(ptr, pixels, 0, pixels.Length);
img.UnlockBits(bmpData);
// Boucle sur les octets
for (int i = 0; i < pixels.Length; i++)
{
// Motif binaire ou byte
if (bVisuBin)
sb.Append(Convert.ToString(pixels[i], 2).PadLeft(8, '0') + " ");
else
sb.Append(pixels[i].ToString("X").PadLeft(2, '0') + " ");
// Formate la sortie pour lisibilité
if ((i+1) % 40 == 0) sb.Append("\r\n");
}
// Ecrit dans le fichier
LOGF(LOGFILE, sb.ToString(), true);
}
Exemple d'utilisation :
Bitmap monImg = new Bitmap("monImage.bmp");
// Visu hexa
LogBytesImage(monImg, false, "logHexa.txt");
// Visu bin
LogBytesImage(monImg, true, "logBinaire.txt");
bye...