Get SPListItem image size
11/01/2018 11:39
How to get image dimensions from SPListItem in case of picture is stored in Pictue library or Document library. in case of Picture library, there are fields to store this values, but not in Document library. However this value is stored in MetaInfo field which contains also other values. So we can get image dimensions as described below
1.) picture item is in picture library
item["ImageWidth"]
item["ImageHeight"]
2.) picture item is in document library (works in picture library as well)
var metaInfo = item["MetaInfo"];
Example of MetaInfo value (separated by end of line):
vti_thumbnailexists:BW|false
vti_parserversion:SR|15.0.0.4701
TeamTags:SW|
vti_folderitemcount:IR|0
vti_stickycachedpluggableparserprops:VX|wic_XResolution Subject vti_lastheight vti_title vt
i_lastwidth wic_YResolution oisimg_imageparsedversion
vti_lastwidth:IW|800
vti_author:SR|i:0#.w|dev\\lsevcik
vti_previewexists:BW|false
vti_modifiedby:SR|i:0#.w|dev\\lsevcik
vti_foldersubfolderitemcount:IR|0
vti_lastheight:IW|600
ContentTypeId:SW|0x0101005A7F022E2869E74195A5202DD4A1AB0B00230275AA1270FE4C8B49F47EFFE14461
vti_title:SW|BalconDeEuropa.jpg
wic_YResolution:DW|96.0000000000000
oisimg_imageparsedversion:IW|4
wic_XResolution:DW|96.0000000000000
Subject:SW|BalconDeEuropa.jpg
using (SPWeb web = site.OpenWeb(webUrl))
{
var list = web.Lists["Documents"];
var item = list.GetItemById(199); //picture in document library
var metaInfo = item["MetaInfo"].ToString();
var regexH = new Regex("vti_lastheight:IW\\|(\\d)*\\b");
var matchH = regexH.Match(metaInfo).ToString();
var height = matchH.Replace("vti_lastheight:IW|", string.Empty);
var regexW = new Regex("vti_lastwidth:IW\\|(\\d)*\\b");
var matchW = regexW.Match(metaInfo).ToString();
var width = matchW.Replace("vti_lastwidth:IW|", string.Empty);
Console.WriteLine("{0}: {1} x {2} px", item.File.Name, width, height);
}