doudianhuo1129 2018-09-20 13:57
浏览 122
已采纳

如何使用golang将Microsoft SQL varbinary(max)字段提取到图像?

I have a varbinary(max) field in Microsoft SQL Server which contains an image.

When running "SELECT IMAGE FROM TABLE", I get a result which looks like "0x07FD30...."

When using go to retrieve the data, I get the same hex string which is stored as a []byte:

type Person struct {
    PersonID string
    Image    []byte
} 

I connect to the database and do:

rows.Scan(&person.PersonID, &person.Image)

And then print the result as hex, it's the same:

fmt.Printf("%#x", p.Image)

Result:

0x07fd30...

My question is, how do I turn this back into an image?

I've tried writing the raw bytes to a file:

ioutil.WriteFile("./tempfile.png", p.Image, 0644)

I've tried using the image library to decode it, which just errors with an unidentified kind:

image.Decode(bytes.NewReader(p.Image))

And also tried png.Encode too.

Any thoughts or pointers in the right direction greatly appreciated.

Many thanks in advance.

  • 写回答

2条回答 默认 最新

  • dsjswclzh40259075 2018-09-21 10:36
    关注

    Writing the raw bytes to a file with ioutil.WriteFile("./tempfile.png", p.Image, 0644) does seem to be the correct process. However, for whatever reason, Microsoft Dynamics AX 2012 seems to add an extra 7 bytes to the beginning of the file when saving it to the database with their BinData class. So, using ioutil.WriteFile(s, p.Image[7:], 0644) works. I'd like to understand what those 7 bytes are and what they're for, but that's for another day. Thanks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大