贴代码(两个方法)
      ///将Image对象转化成二进制流///
        ///</summary>
        ///<paramname="image"></param>
        ///<returns></returns>
        public byte [] ImageToByteArray(Image image)
        {
            //实例化流
            System.IO.MemoryStream imageStream = new System.IO.MemoryStream();
            //将图片的实例保存到流中
            image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            //return imageStream;
            //保存流的二进制数组
            byte[] imageContent = new Byte[imageStream.Length];
            imageStream.Position = 0;
            //将流泻如数组中
            imageStream.Read(imageContent, 0, (int)imageStream.Length);
            return imageStream .ToArray ();
        }
       private string Changeto16(Image image)
        {
            
            StringBuilder tempStr =new StringBuilder ();
             byte [] mybytes = ImageToByteArray(image);
              for (int i = 0; i < mybytes.Length; i++)
            {
               // Convert.ToInt32("dfdf",2);                tempStr.Append(Convert.ToString(mybytes[i], 16));
            }
            return tempStr.ToString().ToUpper();          
        }
我是把我等比例缩放处理过的图片image对象转换为十六进制,数据是出来了,但是把tempStr.ToString().ToUpper();的数据还原为图片就说它不是图片了。求解啊谢谢大家。

解决方案 »

  1.   

    你需要把字符串再转回二进制public static string ByteArrayToString(byte[] ba)
    {
      StringBuilder hex = new StringBuilder(ba.Length * 2);
      foreach (byte b in ba)
        hex.AppendFormat("{0:x2}", b);
      return hex.ToString();
    }
    or:public static string ByteArrayToString(byte[] ba)
    {
      string hex = BitConverter.ToString(ba);
      return hex.Replace("-","");
    }
    There are even more variants of doing it, for example here.The reverse conversion would go like this:public static byte[] StringToByteArray(String hex)
    {
      int NumberChars = hex.Length;
      byte[] bytes = new byte[NumberChars / 2];
      for (int i = 0; i < NumberChars; i += 2)
        bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
      return bytes;
    }代码来自:
    http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa-in-c