3

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());
    }
7
  • Did you remember to decrypt the byte[]? Can you post all of the code -- code that converts the image to a byte[], encryption/decryption/modification? The code you have posted is not enough to determine what's wrong. Commented Dec 14, 2010 at 21:31
  • It's doesn't have to be decrypted since I have to show the encrypted Image. But the encryption is just swapping bits in the bytes (sDES). I still have a byte[] in the end and it should be possible to just show this encrypted image. Commented Dec 14, 2010 at 21:35
  • How did you convert it to byte[] in the first place? Commented Dec 14, 2010 at 21:36
  • List<byte> encryptedText = new List<byte>(); and then call convert this list to an array : pbEncrypted.Image = iConverter.byteArrayToImage(encryptedText.ToArray()); Commented Dec 14, 2010 at 21:38
  • Oh no, it has 6559 bytes in it , i've added an image with the values Commented Dec 14, 2010 at 21:43

4 Answers 4

9

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;
}
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

3

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!

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.