1 pièce(s) jointe(s)
Récupération du message caché dans une image en c#
Bonjour tout le monde, je suis débutant dans la programmation en c# et je suis en train de réaliser une application Windows Form qui permet de cacher un message dans une image, et j'ai un problème au moment de décryptage du message caché je ne reçoit que des ??????????, voir image ci-dessous:
Pièce jointe 321219
ci-dessous le code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TatouageImg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonOuvrir_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Image Files (*.bmp) | *.bmp";
// afficher dans la liste Box le chemin
openFileDialog1.InitialDirectory = @"C:\ ";
if (openFileDialog1.ShowDialog() == DialogResult.OK) //Si le browser de fichier est ouvert en mode lecture (open)
{
textBoxFilePath.Text = openFileDialog1.FileName.ToString();
// afficher l'image dans la pictureBox
pictureBox1.ImageLocation = textBoxFilePath.Text;
}
}
private void buttonCrypte_Click(object sender, EventArgs e)
{
// création d'un bimap à partir de l'image
Bitmap bitmap = (Bitmap)pictureBox1.Image;
for (int i=0; i < bitmap.Width; i++)
{
for(int j=0; j < bitmap.Height; j++)
{
Color pixel = bitmap.GetPixel(i, j);
if(i<1 && j < textBoxMessage.TextLength)
{
char letter = Convert.ToChar(textBoxMessage.Text.Substring(j, 1));
int value = Convert.ToInt32(letter);
bitmap.SetPixel(i, j, Color.FromArgb(pixel.R, pixel.G, pixel.B));
}
if(i==bitmap.Width-1 && j == bitmap.Height - 1)
{
bitmap.SetPixel(i, j, Color.FromArgb(pixel.R, pixel.G, textBoxMessage.TextLength));
}
}
}
saveFileDialog1.Filter = "Image Files (*.bmp) | *.bmp";
saveFileDialog1.InitialDirectory = @"C:\ ";
if (saveFileDialog1.ShowDialog() == DialogResult.OK) //Si le browser de fichier est ouvert en mode ecriture
{
textBoxFilePath.Text = saveFileDialog1.FileName.ToString();
// afficher l'image dans la pictureBox
pictureBox1.ImageLocation = textBoxFilePath.Text;
bitmap.Save(textBoxFilePath.Text);
}
}
private void buttonDecrypter_Click(object sender, EventArgs e)
{
// création d'un bimap à partir de l'image
Bitmap bitmap = (Bitmap)pictureBox1.Image;
string message = "";
Color lastpixel = bitmap.GetPixel(bitmap.Width - 1, bitmap.Height - 1);
int msgLength = lastpixel.B;
for (int i = 0; i < bitmap.Width; i++)
{
for (int j = 0; j < bitmap.Height; j++)
{
Color pixel = bitmap.GetPixel(i, j);
if (i < 1 && j < msgLength)
{
int value = pixel.B;
char c = Convert.ToChar(value);
string letter = Encoding.UTF32.GetString(new byte[] { Convert.ToByte(c) });
//string letter = Encoding.ASCII.GetString(new byte[] { Convert.ToByte(c) });
message += letter;
}
}
}
textBoxMessage.Text = message;
}
}
} |