将二进制文件转换为图像时出错

时间:2015-12-13 09:53:01

标签: asp.net itextsharp

我尝试将图像从mssql varbinary(max)列导入iTextSharp。 但它总是显示错误" NullReferenceException"在" phrase.Add(imageChunk)"

我的代码是:

Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("dbconnection").ConnectionString)

Dim q As String = "select top 1  pic_id from pic"
Dim cmd As SqlCommand = New SqlCommand(q, conn)
conn.Open()
Dim sr As SqlDataReader = cmd.ExecuteReader

While sr.Read
    Dim byt() As Byte = CType(sr.Item("pic_id"), Byte())
    Dim ms As MemoryStream = New MemoryStream(byt)
    Dim sdi As System.Drawing.Image = System.Drawing.Image.FromStream(ms)
    Dim img As Image = Image.GetInstance(sdi, ImageFormat.Jpeg)

    Dim imageChunk As Chunk = New Chunk(img, 0, 0)
    phrase.Add(imageChunk)
End While

该列的数据类型是" varbinary(max)"数据是这样的:(0x89504E470D0A1A0A0000000D494 ....................)

1 个答案:

答案 0 :(得分:1)

你的变量"短语"没有设置,它没有/ null。确保传递正确的"短语"对你的方法很有可能解决你的问题。