【发布时间】:2021-05-06 22:17:55
【问题描述】:
我有一个复杂的场景,客户发送XML 文件,我应该从这些文件中提取一些信息。一个重要信息是使用base64 编码的图像。
xml的xsd文件,定义了包含base64编码图像的元素如下:
<xs:element name="image" type="xs:base64Binary" nillable="false" minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:documentation xml:lang="en">
base64 image.
</xs:documentation>
</xs:annotation>
</xs:element>
在 SSIS 中,我使用XML Source 组件来提取元素。在组件的“输入和输出属性”中,我将数据类型(在与 base64-image 元素相关的外部/输出列中)定义为image [DT_IMAGE] 类型。
Q1:选择这种数据类型读取base64编码的图片是否正确? byte stream [DT_BYTES] 或者只是 string [DT_STR] 呢?
接下来,我将组件的输出传递给 Recordset Destination 以将输出存储在类型为 Object 的 SSIS 变量 User::xml_image 中(我这样做是因为可能有多个图像)。
然后将对象变量传递给Script Task,转换为DataTable,我正在尝试将“假设第一行/图像”保存到文件系统中。我使用的代码如下:
DataTable table = new DataTable();
oleAdapter.Fill(table, Dts.Variables["xml_image"].Value);
Image image;
BinaryFormatter bf = new BinaryFormatter();
var ms = new MemoryStream();
// convert the first image
bf.Serialize(ms, table.Rows[0]["image"]);
byte[] bytes = ms.ToArray();
using (MemoryStream mem = new MemoryStream(bytes))
{
mem.Seek(0, SeekOrigin.Begin);
image = Image.FromStream(mem); // error in this line: Parameter is not valid.
image.Save("D:\\tepack.jpeg", ImageFormat.Jpeg);
}
脚本一直执行,直到调用FromStream 方法并失败并出现此错误:
参数无效。
Q2:如何修复代码错误,并成功保存图像,考虑以上场景和配置?
【问题讨论】: