【发布时间】:2010-12-14 21:29:00
【问题描述】:
我正在尝试将 byte[] 转换为 C# 中的图像。我知道这个问题已经在不同的论坛上提出过。但是他们给出的答案都没有帮助我。给出一些上下文= 我打开一个图像,将其转换为字节 []。我加密了字节[]。最后我仍然有 byte[] 但它已被修改过。现在我想再次显示这个。 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());
}
【问题讨论】:
-
你记得解密 byte[] 吗?您能否发布所有代码——将图像转换为字节 []、加密/解密/修改的代码?您发布的代码不足以确定问题所在。
-
它不必解密,因为我必须显示加密的图像。但是加密只是交换字节中的位(sDES)。我最后还有一个字节[],应该可以只显示这个加密的图像。
-
你是怎么把它转换成
byte[]的? -
List
encryptedText = new List ();然后调用将此列表转换为数组: pbEncrypted.Image = iConverter.byteArrayToImage(encryptedText.ToArray()); -
哦不,它有 6559 个字节,我已经添加了一个带有值的图像