【问题标题】:Convert a Byte array to Image in c# after modifying the array修改数组后在c#中将字节数组转换为图像
【发布时间】: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 个字节,我已经添加了一个带有值的图像

标签: c# image byte


【解决方案1】:

试试这样的。处理图像和内存流时,请务必将所有内容都包含在 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;
}

【讨论】:

    【解决方案2】:

    根据问题和 cmets,我猜您正在修改与图像标头关联的字节。您不能修改这些字节(使用加密方法)并且仍然能够加载图像。

    确保您没有更改标头字节。

    您可以在 google/wikipedia 上找到有关标题格式的信息。

    【讨论】:

      【解决方案3】:

      您应该跳过标题,只加密图像。 您可以通过将 bytearray 的前 54 个字节复制到加密图像所在的新 bytearray 中来做到这一点。 比你遍历图像中的所有其他字节并加密它们。 像这样的:

              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]);
                  }
              }
      

      最后你使用你用来将字节数组转换为图像的代码。

      我希望这对你有用!

      【讨论】:

        【解决方案4】:

        要添加到@Boo 的答案,您可以使用Bitmap.LockBits 方法获取原始图像数据——减去标题。在MSDN page on the BitmapData class 上有一个以这种方式处理图像的示例。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-07-07
          • 2012-08-20
          • 2018-08-01
          • 1970-01-01
          • 2015-08-16
          • 2013-05-19
          相关资源
          最近更新 更多