I have a complex scenario, in which a customer sends XML
files and I am supposed to extract some info out of these files. One important info is images encoded in base64
.
The xsd
file of the xml
, defines the element which contains the base64-encoded image as follows:
<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>
In SSIS, I am using XML Source
component to extract the element. In the "Input and Output Properties" of the component, I defined the datatype (in both External/Output Columns related to the base64-image element) as image [DT_IMAGE]
type.
Q1: Is it correct to choose this datatype to read base64-encoded images? what about byte stream [DT_BYTES]
or just string [DT_STR]
?
Next, I passed the output of the component to a Recordset Destination
to store the output in an SSIS variable User::xml_image
of type Object
(I did this because it is possible to have multiple images).
The object variable is then passed to a Script Task
, converted into DataTable
, and I am trying to save "let's say the first row/image" into the file system. The code I am using is as:
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);
}
The script executes until call of FromStream
method and fails with the this error:
Parameter is not valid.
Q2: How to fix the code error, and save the image successfully, considering above scenario and configurations?