我正在尝试将 byte[] 转换为 C# 中的图像。我知道在不同的论坛上有人问过这个问题。但是他们给出的答案都没有帮助我。给出一些上下文= 我打开一张图片,将其转换为 byte[]。我加密字节[]。最后我仍然有 byte[] 但它已被修改为 ofc。现在我想再次显示它。 byte[] 本身包含 6559 个字节。我尝试通过以下方式转换它:
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
我收到此错误:参数无效。
字节数组是通过在 List 上使用 .toArray() 构造的
List<byte> encryptedText = new List<byte>();
pbEncrypted.Image = iConverter.byteArrayToImage(encryptedText.ToArray())
;
谁能帮帮我?我是不是忘记了某种格式之类的?
必须转换为图像的字节:
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());
}
最佳答案
尝试这样的事情。使用图像和内存流时,请务必将所有内容包装在 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;
}
关于c# - 修改数组后将字节数组转换为 C# 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4444421/