转换一个字节数组图像在C#中修改后阵 [英] Convert a Byte array to Image in c# after modifying the array
问题描述
我想一个byte []转换为C#的图像。我知道这个问题已经被问在不同的论坛。但没有对他们给出的答案帮助了我。为了给一些背景=
我打开一个图像,将其转换为一个byte []。我加密的byte []。最后,我仍然有一个字节[],但它已被修改OFC。现在我想再次显示这一点。字节[]本身包含的6559个字节。我尝试这样做,将其转换:
公众形象byteArrayToImage(字节[] byteArrayIn)
{
MemoryStream的毫秒=新的MemoryStream(byteArrayIn);
图像returnImage = Image.FromStream(毫秒);
返回returnImage;
}
和我得到这个错误:参数是无效的。
字节数组是由一个使用列表中的.toArray()
名单,LT构建;字节> encryptedText =新的List<位>();
pbEncrypted.Image = iConverter.byteArrayToImage(encryptedText.ToArray())
谁能帮助我?我忘记了某种格式或什么的?
这必须被转换成图像的字节:
私人无效executeAlgoritm(字节[]明文)
{
//字节
名单,下空单;字节> encryptedText =新的List<位>();
//循环遍历所有的原始字节数组中从图像
的foreach(明文字节值)
{
//将其转换为得到字节bitarray
BitArray myBits =新BitArray(8); //定义大小
为(字节x = 0; X< myBits.Count; X ++)
{
myBits [X] =(((价值>> ; X)及0×01)== 0×01)?真假;
}
//加密bitarray,并返回一个字节
字节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)) ))))));
//字节添加到列表
encryptedText.Add(bcipher);
}
//由列表转换为阵列和阵列的图像
pbEncrypted.Image = iConverter.byteArrayToImage显示图像(encryptedText.ToArray());
}
您应该跳过头和只能加密图像。
。您可以通过复制的第一个54个字节的字节组进入新的bytearray其中加密图像会做到这一点。
比你遍历图像中的所有其他字节,你将它们加密。
事情是这样的:
的for(int i = 0; I< img.Length;我++)
{
如果(I< 54)
{
//从标题
_cyph复制的第一个54个字节[I] = IMG [我]
}其他{
//加密所有其他字节
_cyph [I] =加密(IMG [我]);
}
}
在最后,你用你用来转换代码为bytearray成图像。
我希望这对你的作品!
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 :
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());
}
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!
这篇关于转换一个字节数组图像在C#中修改后阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!