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?
void ParseDirectory(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
foreach (FileInfo fi in di.GetFiles("*.jpg", SearchOption.TopDiretoryOnly))
{
System.Drawing.Image img = System.Drawing.Image.FromFile(fi.FullName);
if(img.Width > 900)
resizeImage(img,fi.FullName);
addImageToTree(img);
}
}
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.
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();
}
Best Regards
image resize
Please remember to post your code.
If this post answered your question or solved your problem, please Mark it as Answer.
Trolderik
Member
645 Points
355 Posts
Low resource usage on code to find image size
Apr 07, 2012 10:06 AM|LINK
Hi everyone,
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?
void ParseDirectory(string path) { DirectoryInfo di = new DirectoryInfo(path); foreach (FileInfo fi in di.GetFiles("*.jpg", SearchOption.TopDiretoryOnly)) { System.Drawing.Image img = System.Drawing.Image.FromFile(fi.FullName); if(img.Width > 900) resizeImage(img,fi.FullName); addImageToTree(img); } }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.
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(); }Best Regards
image resize
If this post answered your question or solved your problem, please Mark it as Answer.
avinash_bhud...
Contributor
2881 Points
517 Posts
Re: Low resource usage on code to find image size
Apr 07, 2012 11:24 AM|LINK
You can get image Height and Width without loading the image.
Getting image height and width from its header is explained in following articles.
http://stackoverflow.com/questions/111345/getting-image-dimensions-without-reading-the-entire-file
http://www.codeproject.com/Articles/35978/Reading-Image-Headers-to-Get-Width-and-Height
For resizing images in fastest way refer this article
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
Also refer this thread to get some alternative methods enlisted.
http://forums.asp.net/t/1085119.aspx
Hope it helps.
image resize
Trolderik
Member
645 Points
355 Posts
Re: Low resource usage on code to find image size
Apr 07, 2012 12:03 PM|LINK
Thank you very much, those links where usefull beyond what i hoped for!
image resize
If this post answered your question or solved your problem, please Mark it as Answer.