C# 将图像转换为十六进制纯文本与存储在SQL server中的图像相同

C# 将图像转换为十六进制纯文本与存储在SQL server中的图像相同,c#,sql-server,image,byte,C#,Sql Server,Image,Byte,我有一个Winforms应用程序,它通过两种方法将图像作为SQL Server列的数据类型Single和Multiple上传到SQL Server 2005 使用single,我将参数作为二进制图像从C#发送到SP 但是对于multiple,我需要将它们作为XML纯文本发送到DB,并使用 convert(image, @myimageFromXML, 2) 我尝试了很多其他网站的功能,比如 public static string ByteArrayToString(byte[] b

我有一个Winforms应用程序,它通过两种方法将图像作为SQL Server列的数据类型
Single
Multiple
上传到SQL Server 2005

使用single,我将参数作为二进制图像从C#发送到SP

但是对于multiple,我需要将它们作为XML纯文本发送到DB,并使用

convert(image, @myimageFromXML, 2)
我尝试了很多其他网站的功能,比如

     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();
     }
     public static string ByteArrayToString(byte[] ba)
     {
         string hex = BitConverter.ToString(ba);
         return hex.Replace("-", "");
     }

     public static string ByteArrayToString(byte[] Bytes)
     {
         char[] hexes = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
         char[] Result = new char[Bytes.Length << 1];
         int Offset = 0;
         for (int i = 0; i != Bytes.Length; i++)
         {
             Result[Offset++] = hexes[Bytes[i] >> 4];
             Result[Offset++] = hexes[Bytes[i] & 0x0F];
         }
         return new string(Result);
     }
     public static String ByteArrayToString(byte[] Source)
     {
         return "0x" + BitConverter.ToString(Source).Replace("-", "");
     }

     public static string ByteArrayToString(byte[] barray)
     {
         char[] c = new char[barray.Length * 2];
         byte b;
         for (int i = 0; i < barray.Length; ++i)
         {
             b = ((byte)(barray[i] >> 4));
             c[i * 2] = (char)(b > 9 ? b + 0x37 : b + 0x30);
             b = ((byte)(barray[i] & 0xF));
             c[i * 2 + 1] = (char)(b > 9 ? b + 0x37 : b + 0x30);
         }

         return new string(c);
     }
但多次上传保存的错误数据始于

 0x49492A008C040600803FA04FF004160D0784..........
 0x310033003700380030003700380037.............

请帮助我了解如何从C#byte[]获取正确的二进制数据,以便将其作为XML文本发送到SQL Server尝试将图像转换为Base64字符串:

// Convert byte[] to Base64 String
string base64ImageString = Convert.ToBase64String(imageBytes);
您需要的一些方法:

将图像转换为Base64字符串

Base64字符串到图像


来自的代码。

您如何恢复数据?
public string ImageToBase64(Image image, 
  System.Drawing.Imaging.ImageFormat format)
{
  using (MemoryStream ms = new MemoryStream())
  {
    // Convert Image to byte[]
    image.Save(ms, format);
    byte[] imageBytes = ms.ToArray();

    // Convert byte[] to Base64 String
    string base64String = Convert.ToBase64String(imageBytes);
    return base64String;
  }
}
public Image Base64ToImage(string base64String)
{
  // Convert Base64 String to byte[]
  byte[] imageBytes = Convert.FromBase64String(base64String);
  MemoryStream ms = new MemoryStream(imageBytes, 0, 
    imageBytes.Length);

  // Convert byte[] to Image
  ms.Write(imageBytes, 0, imageBytes.Length);
  Image image = Image.FromStream(ms, true);
  return image;
}