doudianhuo1129 2018-09-20 05: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 02: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条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 前端js怎么实现word的.doc后缀文件在线预览
  • ¥20 macmin m 4连接iPad
  • ¥15 DBIF_REPO_SQL_ERROR
  • ¥15 根据历年月数据,用Stata预测未来六个月汇率
  • ¥15 DevEco studio开发工具 真机联调找不到手机设备
  • ¥15 请教前后端分离的问题
  • ¥100 冷钱包突然失效,急寻解决方案
  • ¥15 下载honeyd时报错 configure: error: you need to instal a more recent version of libdnet
  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
  • ¥15 霍尔传感器hmc5883l的xyz轴输出和该点的磁感应强度大小的关系是什么
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部