在C#中将Varbinary(Max)字符串转换为图像格式

本文关键字:转换 字符串 图像 格式 Max 中将 Varbinary | 更新日期: 2023-09-27 18:25:30

我正在尝试将字符串格式的varbinary(max)数据转换为图像。我正在尝试下面的代码,但得到错误

"参数无效"

在线

System.Drawing.Image image = System.Drawing.Image.FromStream(ms);

这是完整的代码。

string stringFromSQL = "0x6100730064006600";
List<byte> byteList = new List<byte>();
string hexPart = stringFromSQL.Substring(2);
for (int i = 0; i < hexPart.Length / 2; i++)
{
    string hexNumber = hexPart.Substring(i * 2, 2);
    byteList.Add((byte)Convert.ToInt32(hexNumber, 16));
}
byte[] imgData = byteList.ToArray();
using (MemoryStream ms = new MemoryStream(imgData))
{
    System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
    image.Save(@"D:'Images'Photo.jpg");
}

任何帮助都将不胜感激。

谨致问候,Muhammad Kamal

在C#中将Varbinary(Max)字符串转换为图像格式

这是因为字符串FromSQL不正确。您可以使用以下代码进行测试

步骤1:加载图像到内存在我的本地计算机

步骤2:从图像中获取字节

[Note]: you can try the get the string from the binary

步骤3:使用给定的代码生成另一个图像

var imageIn = System.Drawing.Image.FromFile(@"C:'Users'User'Desktop'sample-profile-image.jpeg");
byte[] bytes;
using (var ms = new MemoryStream())
{
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    bytes = ms.ToArray();   
}
using (MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length))
{
    System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
    image.Save(@"D:'Images'Photo.jpg");
}

更新:使用十六进制字符串

string stringFromSQL = "89504E470D0A1A0A0000000D4948445200000064000000640802000000FF800203000000017352474200AECE1CE90000000467414D410000B18F0BFC61050000001974455874536F6674776172650041646F626520496D616765526561647971C9653C0000031E49444154785EED9ADB5222311445E7FFBF47416E52DCF441A0041951D07F9955C3298B1AE7C2EE21274CCD5E4F402727C9B2733A49FBE5CA9C8C6509589680650958968065095896806509589680650958968065095896806509589680650958968065095896806509589680650958968065095896C065C9EAF57A93C9E4FEFEFEEEEE6E3C1EF3352E5C061721ABD56A2168BBDDBE7F821FB94481285A95FAB26E6F6F5F5E5EC2CD2FA000C5A2423D2ACB62AEBDBDBD8592DF42310A47B54AD49435180C4E347580C25489CA35A826EBFAFAFA8FB3EF3354A998BFAAC9E279170244C8F711229D6AB21ADC5607A81821D2A9238B05540CBD11FD7E3F02E55247162BCF187723A81E8172A9238BBC13E36E44ADB46559029E8602756491A163DC8DF8BF123C78E920D0386DD54A58504D96B73B1ADE486BF88846C3877F1A3E566E021BECE9748A1DBFB0F8E7B12C01CB12B02C01CB12B02C81FAB2DAEDF670389CCD66F3F9FCF1F1F1E9E9E9EB77F8C0577EE41205281615EA514716BBE8D168F4F0F0F0FCFC7CFA7687C254A122D523502EA9B21824F7C872B9DCEFF7E1A011BBDD8E20844AB696248B514D26136E8D18EE99202061D39465C862E29C5DD33104A78968AC246565F13767BEC4980AB3582C4ADF620565B55A2D9E6831941468AEE8F9444159ABD52A0691088D46F30528256B3C1E47F7D32977A05A4A56F2043C66BD5E4727CE4D29592C85A2EFE9D07474E2DC9492C53625FA9E0EABFCE8C4B929258BA738FBBBE87E229BCDA6DC02A2942CE8743AAFAFAF318814688E46A3F902149405BD5E2F2D79D150E9171C656541BFDF6FF09A5E852612FEB5A6B82CB8B9B92195C4B00A40709A88C64A92210B48BA3C1F4F3CBA3A1D0212B6F496F08324590786C3E1198F1F0845C0089D42AA2CE02E98CD667F99C5A84E90B41BEA836C590718273B3836BDD2C4A43055A898AFE9401D591FB4DB6D064FDE2149FFF4AC991FB944018A557F675159D60FA0A3DBEDB208003E5CC21B9D632E4BD685635902962560590296256059029625605902962560590296256059029625605902962560590296256059029625605902962560590296256059277375F50D9C32549D11F2EF450000000049454E44AE426082";
var bytes = Enumerable.Range(0, stringFromSQL.Length)
                     .Where(x => x % 2 == 0)
                     .Select(x => Convert.ToByte(stringFromSQL.Substring(x, 2), 16))
                     .ToArray();
using (MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length))
{
    System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
    image.Save(@"D:'Images'Photo.jpg");
}