Convert a Byte array to Image in c# after modifying the array

21,315

Solution 1

You should skip the header and only encrypt the image. You can do this by copying the first 54 bytes of your bytearray into the new bytearray in which the encrypted image will be. Than you loop over all the other bytes in the image and you encrypt them. Something like this:

        for (int i = 0; i < img.Length; i++)
        {
            if (i < 54)
            {
                //copy the first 54 bytes from the header
                _cyph[i] = img[i];
            }else{
                //encrypt all the other bytes
                _cyph[i] = encrypt(img[i]);
            }
        }

In the end you use the code you used to convert a bytearray into an image.

I hope this works for you!

Solution 2

Try something like this. When working with images and memory streams, be sure to wrap everything in using statements to make sure your objects are disposed properly.

public static Image CreateImage(byte[] imageData)
{
    Image image;
    using (MemoryStream inStream = new MemoryStream())
    {
        inStream.Write(imageData, 0, imageData.Length);

        image = Bitmap.FromStream(inStream);
    }

    return image;
}

Solution 3

Based on the question and the comments, I would guess that you are modifying the bytes associated with the image header. You can not modify these bytes (with an encryption method) and still be able to load the image.

Be sure you are not changing the header bytes.

You can find out about header formats on google/wikipedia.

Solution 4

To add to @Boo's answer, you can get hold of the raw image data -- minus header -- using the Bitmap.LockBits method. There's an example of manipulating an image in this way on the MSDN page on the BitmapData class.

Share:
21,315
Olivier_s_j
Author by

Olivier_s_j

Updated on April 14, 2020

Comments

  • Olivier_s_j
    Olivier_s_j about 4 years

    I'm trying to convert a byte[] to an image in C#. I know this question has been asked on different forums. But none of the answers given on them helped me. To give some context= I open an image, convert it to a byte[]. I encrypt the byte[]. In the end I still have the byte[] but it has been modified ofc. Now I want to display this again. The byte[] itself consists of 6559 bytes. I try to convert it by doing :

     public Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }
    

    and I get this error: Parameter is not valid.

    The byte array is constructed by using the .toArray() on a List

    List<byte> encryptedText = new List<byte>();    
    pbEncrypted.Image = iConverter.byteArrayToImage(encryptedText.ToArray())
    

    ;

    Can anyone help me? Am I forgetting some kind of format or something ?

    The byte that has to be converted to an image : alt text

     private void executeAlgoritm(byte[] plainText)
        {
    
            // Empty list of bytes
            List<byte> encryptedText = new List<byte>();
    
            // loop over all the bytes in the original byte array gotten from the image
            foreach (byte value in plainText)
            {
                // convert it to a bitarray
                BitArray myBits = new BitArray(8); //define the size
    
                for (byte x = 0; x < myBits.Count; x++)
                {
                    myBits[x] = (((value >> x) & 0x01) == 0x01) ? true : false;
                }
    
                // encrypt the bitarray and return a byte
                byte bcipher = ConvertToByte( sdes.IPInvers(sdes.FK(sdes.Shift(sdes.FK(sdes.IP(myBits),keygen.P8(keygen.shift(keygen.P10(txtKey.Text))))),keygen.P8(keygen.shift(keygen.shift(keygen.shift(keygen.P10(txtKey.Text))))))));
    
                // add the byte to the list
                encryptedText.Add(bcipher);
    
            }
            // show the image by converting the list to an array and the array to an image
            pbEncrypted.Image = iConverter.byteArrayToImage(encryptedText.ToArray());
        }