2011-03-24 52 views
2

对于学校,我们必须在速记中做一个项目,我选择了使用bmp并将文本放入其中。它对正常的图片效果很好,但是从具有相同字节序列的图片开始,例如白色区域或出现问题的图像是this picture将图像转换为字节数组的问题

当我从字节阵列中的文字在图像中制作,然后我读回字节阵列已经改变。而我所做的唯一的事情是从字节阵列和图像中创建一个图像回一个字节阵列。

这是我的代码:

namespace steganografie 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void btnFile_Click(object sender, EventArgs e) 
    { 
     OpenFileDialog dialog = new OpenFileDialog(); 
     dialog.Filter = "bmp files (*.bmp)|*.bmp|All files (*.*)|*.*"; 
     dialog.Title = "Select a image"; 
     dialog.InitialDirectory = Application.ExecutablePath; 

     if (dialog.ShowDialog() == DialogResult.OK) 
     { 
      txtText.Enabled = true; 
      txtFile.Text = dialog.FileName; 
      byte[] arImage = imageToByteArray(Image.FromFile(txtFile.Text)); 
      txtText.MaxLength = ((arImage.Length -54) /8)-5; 
      lblCharLeft.Text = txtText.MaxLength+""; 
      lblCharLeft.Tag = txtText.MaxLength; 
      picOriginal.Image = Image.FromFile(dialog.FileName); 
     } 
    } 
    private void btnSteganografie_Click(object sender, EventArgs e) 
    { 
     byte[] arImage = imageToByteArray(Image.FromFile(txtFile.Text)); 
     string input = txtText.Text; 
     string inputInBits = GetBits(input); 
     char[] bits = inputInBits.ToCharArray(); 
     int i = 0; 
     for (i = 54; (i < arImage.Length & i < bits.Length+54); i++) 
     { 
      if ((int)arImage[i] == 0) 
      { 
       arImage[i] = (byte)2; 
      } 
      if ((int)arImage[i] == 255) 
      { 
       byte bBit = new byte(); 
       bBit = 2; 
       arImage[i] = (byte)(arImage[i]-bBit); 
      } 
      int lastBit = ((int)arImage[i]) % 2; 
      int dataBit = ((int)bits[i - 54])%2; 
      if (lastBit != dataBit) 
      { 
       arImage[i]++; 
      } 
     } 
     arImage[i] = 0; 
     Image result = byteArrayToImage(arImage); 
     picEncoded.Image = result; 
     SaveFileDialog sfd = new SaveFileDialog(); 
     if (sfd.ShowDialog() == DialogResult.OK) 
     { 
      result.Save(sfd.FileName+".bmp",System.Drawing.Imaging.ImageFormat.Bmp); 
     } 
    } 
    public string GetBits(string input) 
    { 
     StringBuilder sbBuilder = new StringBuilder(); 
     byte[] bytes = Encoding.Unicode.GetBytes(input); 
     foreach (byte bByte in Encoding.Unicode.GetBytes(input)) 
     { 
      int iByte = (int)bByte; 

      for (int i = 7; i >= 0 & (iByte!=0 | i!=7); i--) 
      { 
       if (iByte - Math.Pow(2, i) >= 0) 
       { 
        sbBuilder.Append("1"); 
        iByte -= (int)Math.Pow(2, i); 
       } 
       else 
       { 
        sbBuilder.Append("0"); 
       } 
      } 
     } 
     return sbBuilder.ToString(); 
    } 
    public byte[] imageToByteArray(System.Drawing.Image imageIn) 
    { 
     MemoryStream ms = new MemoryStream(); 
     imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); 
     return ms.ToArray(); 
    } 
    public Image byteArrayToImage(byte[] byteArrayIn) 
    { 
     //**********ERROR HAPPENS HERE ************* 
     MemoryStream ms = new MemoryStream(byteArrayIn); 
     Image returnImage = Image.FromStream(ms); 
     byte[] arImageTEST = imageToByteArray(returnImage); 
     //**********byteArrayIn should be the same as arImageTEST *********** 
     return returnImage; 
    } 
} 
+0

可能很难在具有如此纯色的图像中隐藏信息。 – sarnold 2011-03-24 09:07:55

+0

什么样的错误?这是一个例外吗?它什么都没有? – Arthur 2011-03-24 09:08:54

+0

将随机字节转换为UTF通常会导致损坏。你永远不会从该字符串中检索正确的字节。 – CodingBarfield 2011-03-24 11:05:48

回答

0

或许这是与现有here这样的信息:

你必须保持开放的流为 寿命形象。

希望它有帮助。

+0

我想他们的意思是,当你再次使用相同的流它会关闭连接,但我只使用一次的流,所以它应该保持开放,我不认为这是问题 – user674570 2011-03-24 09:19:19

+0

我认为他们的意思图像的信息不重复,并且图像从流数据中抽取。如果关闭流,内存将被释放,并且图像数据不再有效。 – 2011-03-25 09:03:03

+0

但我怎么解决这个比? – user674570 2011-03-26 11:13:07