我正在尝试将字节[]转换为C#中的图像.我知道这个问题已经在不同的论坛上提出过.但是他们给出的答案都没有帮助我.给出一些上下文=我打开一个图像,将其转换为byte [].我加密了byte [].最后我仍然有字节[]但它已被修改为ofc.现在我想再次显示它.byte []本身由6559个字节组成.我尝试通过以下方式转换它:
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:参数无效.
通过在List上使用.toArray()构造字节数组
List<byte> encryptedText = new List<byte>();
pbEncrypted.Image = iConverter.byteArrayToImage(encryptedText.ToArray())
Run Code Online (Sandbox Code Playgroud)
;
谁能帮我?我忘了某种格式或什么吗?
必须转换为图像的字节:

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());
}
Run Code Online (Sandbox Code Playgroud)
尝试这样的事情.处理图像和内存流时,请务必将所有内容都包含在using语句中,以确保正确放置对象.
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;
}
Run Code Online (Sandbox Code Playgroud)