Low resource usage on code to find image sizehttp://forums.asp.net/t/1790214.aspx/1?Low+resource+usage+on+code+to+find+image+sizeSat, 07 Apr 2012 12:03:57 -040017902144920700http://forums.asp.net/p/1790214/4920700.aspx/1?Low+resource+usage+on+code+to+find+image+sizeLow resource usage on code to find image size <p>Hi everyone,</p> <p>I've got a peace of code, but i know it consumes more resources than i'd like to make it automatic and on the fly, so could anyone please help me come up with an idea to create a parser to parse the files in a directoryinfo for their respective width and height?</p> <pre class="prettyprint">void ParseDirectory(string path) { DirectoryInfo di = new DirectoryInfo(path); foreach (FileInfo fi in di.GetFiles(&quot;*.jpg&quot;, SearchOption.TopDiretoryOnly)) { System.Drawing.Image img = System.Drawing.Image.FromFile(fi.FullName); if(img.Width &gt; 900) resizeImage(img,fi.FullName); addImageToTree(img); } }</pre> <p>I've also got a bit of code to resize the image, i'm going to post it, but as this doesn't run every single time, it doesn't have to be that strictly on the resource usage, it's only ment as a secondary if anyone knows of something.</p> <pre class="prettyprint">void resizeImage(System.Drawing.Image img,string filename) { System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(900,600); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bmp); // some options, removed because they fill a lot of space graphics.DrawImage(img,0,0,900,600); img.Dispose(); bmp.Save(filename,System.Drawing.Imaging.ImageFormat.Jpeg); GC.Collect(); }</pre> <p><br> <br> </p> <p>Best Regards</p> 2012-04-07T10:06:30-04:004920769http://forums.asp.net/p/1790214/4920769.aspx/1?Re+Low+resource+usage+on+code+to+find+image+sizeRe: Low resource usage on code to find image size <p>You can get image Height and Width without loading the image.</p> <p>Getting image height and width from its header is explained in following articles.</p> <p><a target="_blank" href="http://stackoverflow.com/questions/111345/getting-image-dimensions-without-reading-the-entire-file">http://stackoverflow.com/questions/111345/getting-image-dimensions-without-reading-the-entire-file</a></p> <p><a target="_blank" href="http://www.codeproject.com/Articles/35978/Reading-Image-Headers-to-Get-Width-and-Height">http://www.codeproject.com/Articles/35978/Reading-Image-Headers-to-Get-Width-and-Height</a></p> <p>For resizing images in fastest way refer this article</p> <p><a target="_blank" href="http://weblogs.asp.net/bleroy/archive/2010/05/03/the-fastest-way-to-resize-images-from-asp-net-and-it-s-more-supported-ish.aspx">http://weblogs.asp.net/bleroy/archive/2010/05/03/the-fastest-way-to-resize-images-from-asp-net-and-it-s-more-supported-ish.aspx</a></p> <p>Also refer this thread to get some alternative methods enlisted.</p> <p><a target="_blank" href="http://forums.asp.net/t/1085119.aspx">http://forums.asp.net/t/1085119.aspx</a></p> <p></p> <p>Hope it helps.</p> <p></p> <p></p> 2012-04-07T11:24:26-04:004920800http://forums.asp.net/p/1790214/4920800.aspx/1?Re+Low+resource+usage+on+code+to+find+image+sizeRe: Low resource usage on code to find image size <p>Thank you very much, those links where usefull beyond what i hoped for!</p> 2012-04-07T12:03:57-04:00