如何将SQL Server BLOB字符串转换为System.Drawing.Image? [英] How to convert an SQL Server BLOB string to System.Drawing.Image?

查看:91
本文介绍了如何将SQL Server BLOB字符串转换为System.Drawing.Image?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将表示为字符串的BLOB转换为System.Drawing.Image类型?

How can I convert a BLOB represented as a string to a System.Drawing.Image type?

我将使用c#从csv文件中导入有关用户的信息以及他们的照片.我使用的专有SDK要求照片必须是System.Drawing.Image

I am going to import information about users together with their photo from a csv file using c#. The proprietary SDK that I use requires the photo to be a System.Drawing.Image

下面是csv文件的示例.

Below is an example of the csv file.

surname firstname   photo                       
Blogs    Joe        0xFFD8FFE000104A46494600010101005F005F0000FFDB0043000D090A
                    

photo字段实际上是5k字符长,是SQL Server数据库中BLOB字段值的直接导出.我们刚刚从数据库字段中获取了原始值,并将其导出到csv文件中.

The photo field is actually 5k chars long and is a direct export of the BLOB field value in the sql server db. We have just taken the raw value from the database field and exported it to the csv file.

下面的代码演示了我已经走了多远. cvsuser变量代表csv文件的一行.

Below is the code that demonstrates how far I have got. The cvsuser variable represents one row of the csv file.

// We need to convert the photo from a string to a byte array
string strPhoto = null;
strPhoto = csvuser.photo; // The string that represents the BLOB
byte[] bytes = new byte[strPhoto.Length * sizeof(char)];
System.Buffer.BlockCopy(strPhoto.ToCharArray(), 0, bytes, 0, bytes.Length);
                   
// Then we create a memory stream that holds the image
MemoryStream photostream = new MemoryStream( bytes );

// Then we can create a System.Drawing.Image by using the Memory stream
var photo = Image.FromStream(photostream);

但是,Image.FromStream()行会引发System.ArgumentException并显示消息参数无效".

However, the Image.FromStream() line throws a System.ArgumentException with the message "Parameter is not valid."

如何将表示为字符串的BLOB转换为System.Drawing.Image类型?

How can I convert the BLOB represented as a string to a System.Drawing.Image type?

例如,我之前看到的示例将直接从数据库中获取blob或从文件中读取图像.

The examples I have seen previously would, for example, fetch the blob straight from the database or reading the image from a file.

推荐答案

问题是十六进制编码的字符串到字节数组之间的转换未完成属性.

The issue is that the conversion between the hex-encoded string to byte array isn't being done property.

您列出的代码会将blob中的每个字符视为一个字节,因此'F'将被视为0x46(大写F的ascii代码).您要做的是将每个2个字符解码为一个字节-即F0 = 240等.

The code you listed will treat each of the chars in the blob as a byte, so an 'F' will be treated as 0x46 (the ascii code for a capital F). What you want to do is de-code each 2 characters as a single byte - i.e. F0 = 240 etc.

假定字符串是偶数个字符.您还需要去除"0x"前缀,因为这只是指示其后是数据的十六进制表示形式.

It assumes the string is an even number of characters. You'll also need to strip off the '0x' prefix, as that's just an indicator that what follows is a hexadecimal representation of the data.

所以代替这个:

strPhoto = csvuser.photo; // The string that represents the BLOB
byte[] bytes = new byte[strPhoto.Length * sizeof(char)];
System.Buffer.BlockCopy(strPhoto.ToCharArray(), 0, bytes, 0, bytes.Length);

执行类似的操作(改编自先前给出的

Do something like this (adapted from previous answer given here):

      strPhoto = csvuser.photo; // The string that represents the BLOB

      //remove first 2 chars (the '0x')
      strPhoto = strPhoto.Remove(0, 2);

      //convert hex-string to bytes:
      int NumberChars = strPhoto.Length/2;
      byte[] bytes = new byte[NumberChars];
      using (StringReader sr = new StringReader(strPhoto)){
            for (int i = 0; i < NumberChars; i++)
               bytes[i] = Convert.ToByte(new string(new char[2]{(char)sr.Read(), (char)sr.Read()}), 16);
      }

      // Then we create a memory stream that holds the image
      MemoryStream photostream = new MemoryStream( bytes );

      // Then we can create a System.Drawing.Image by using the Memory stream
      var photo = Image.FromStream(photostream);

这篇关于如何将SQL Server BLOB字符串转换为System.Drawing.Image?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆