determine if blob is an image without loading entire field?

给你一囗甜甜゛ 提交于 2019-12-05 20:32:22

PNGs start with: 89 50 4E 47 0D 0A 1A 0A ; see http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html

GIFs start with 47 49 46 38 37 61 (GIF87a) or 47 49 46 38 39 61 (GIF89a) ; see http://www.fileformat.info/format/gif/egff.htm

JPEGs start with FF D8 FF E0 xx xx 4A 46 49 46 00 (EDIT: adding missing portion of the header; xx xx is the header length, in bytes) ; see http://www.obrador.com/essentialjpeg/headerinfo.htm

reference: http://wangrui.wordpress.com/2007/06/19/file-signatures-table/

  • JPEG files start with FF D8 FF
  • GIF files start with GIF89a (47 49 46 38 39)

This is taking it back to the old school (for data access tactics). Anyhow, you can use a DataReader to grab the field and stream results, then just look at the firs 8 bytes to see what kind of image you are dealing with.

See this article for an intro on how to attach a streamreader to your datareader.

All that said, I would probably just get all the bytes unless the images are ginormous, mainly because any speed you gain from examining the header could be lost when dipping into the well a second time.

Another tactic might be to create a view which inspects the field and exposes the type, based on the first header bytes, in SQL. Kind a ugly but works in a pinch.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!