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条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统