简体   繁体   中英

Problem with converting image to byte array

For school we have to do a project in stenography, I have chosen to use a bmp and put text into it. It works fine with normal pictures but from the moment on when you have pictures with a same byte sequence for example a white area or the image where I had the problem is this picture .

When I make from the bytearray where the text is in an image and I read it back the bytearray has changed. while the only thing I did was making an image from the bytearray and from the image back a bytearray.

This is my code:

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;
    }
 }

Maybe it has something to do with this information available here :

You must keep the stream open for the lifetime of the Image.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM