Lazy Folders revisited (C#)
Now then, following yesterday's post (and I can't promise that they'll all be every day like this!) I spent some more time with the code and put it into use on my vista machine (the main home system with all the duplicate files). It does work rather wonderfully, but I'm not quite done with the updates and modifications yet...
I spent some time working with the photo resizing and tried two main things - the first was an attempt to make the photo display work a little faster. There are two ways to put a photo in a picture box (that I know of anyway) - by loading it into the memory as an Image filetype and assigning it to the pictureBox.Image property, or by directly dropping the file into the pictureBox using the pictureBox.ImageLocation property. I figured that dropping it into the pictureBox with the ImageLocation property might be a little faster since its not going through the extra step of loading it into the memory first before displaying it.
I was wrong! The pictureBox.Image property was the faster way - by a noticable margin. I know I don't have the perfect testing environment set up, but I'm pretty confident since I was using 2Mb pictures and there was an appreciable difference in load times within the application. I may also test reducing the resolution on the loaded image (since I'm dropping it into a much smaller box than the original file size) versus leaving it alone for additional speed - but that will have to come later.
Then I played with the resizing logic and came up with this doosy:
Then simply assigning the xSize to the width and ySize to the height properties of the pictureBox. Basically, this function attempts to keep the height of the image at 279 pixels with correct scaling of width. If the width is greater than 279 after the ration adjustment, it does it again leaving the width 279 and ratio adjusting the height. This may be slightly faster if I evaluate the original height and width of the image and perform the ration adjustment just once, but I think this is a little more readable and fast enough for these purposes.
One other conundrum I worked through yesterday was how to pop the previewed image in the pictureBox into a Windows Picture and Fax Viewer window. This came reasonably quickly after some internet searches, but noone really had the whole solution, so I had to finagle it myself! The basic premise uses the System.Diagnostics.Process collection (referenced in the using section as System.Diagnostics) to run a command line with some Arguments following a click on the pictureBox:
I had discovered with the internet searches that the Windows Picture and Fax Viewer was actually a function built into another program in Windows rather than another program entirely. I found that using a single entry in the StartInfo.fileName propery including the arguments would return a file not found error. Adding them into the StartInfo.Arguments property made it work!
I am now working on reading the EXIF data to give a little more information on the image that is being previewed - more to come on that! I'll also be adding in support for additional filetypes starting with either MP3 or MPG.
I spent some time working with the photo resizing and tried two main things - the first was an attempt to make the photo display work a little faster. There are two ways to put a photo in a picture box (that I know of anyway) - by loading it into the memory as an Image filetype and assigning it to the pictureBox.Image property, or by directly dropping the file into the pictureBox using the pictureBox.ImageLocation property. I figured that dropping it into the pictureBox with the ImageLocation property might be a little faster since its not going through the extra step of loading it into the memory first before displaying it.
I was wrong! The pictureBox.Image property was the faster way - by a noticable margin. I know I don't have the perfect testing environment set up, but I'm pretty confident since I was using 2Mb pictures and there was an appreciable difference in load times within the application. I may also test reducing the resolution on the loaded image (since I'm dropping it into a much smaller box than the original file size) versus leaving it alone for additional speed - but that will have to come later.
Then I played with the resizing logic and came up with this doosy:
decimal ratio = (Convert.ToDecimal(previewImage.Width) / Convert.ToDecimal(previewImage.Height)); int ySize = 279; int xSize = Convert.ToInt32(ySize * ratio); if (xSize > 279) { xSize = 279; ySize = Convert.ToInt32(xSize / ratio); } |
Then simply assigning the xSize to the width and ySize to the height properties of the pictureBox. Basically, this function attempts to keep the height of the image at 279 pixels with correct scaling of width. If the width is greater than 279 after the ration adjustment, it does it again leaving the width 279 and ratio adjusting the height. This may be slightly faster if I evaluate the original height and width of the image and perform the ration adjustment just once, but I think this is a little more readable and fast enough for these purposes.
One other conundrum I worked through yesterday was how to pop the previewed image in the pictureBox into a Windows Picture and Fax Viewer window. This came reasonably quickly after some internet searches, but noone really had the whole solution, so I had to finagle it myself! The basic premise uses the System.Diagnostics.Process collection (referenced in the using section as System.Diagnostics) to run a command line with some Arguments following a click on the pictureBox:
string fileLoc = this.folderBrowserDialog2.SelectedPath.ToString() + @"\" + listBox3.SelectedItem.ToString(); Process proc = new Process(); proc.StartInfo.FileName = @"rundll32.exe"; proc.StartInfo.Arguments = @"C:\WINDOWS\System32\shimgvw.dll,ImageView_Fullscreen "+ fileLoc; proc.Start(); |
I had discovered with the internet searches that the Windows Picture and Fax Viewer was actually a function built into another program in Windows rather than another program entirely. I found that using a single entry in the StartInfo.fileName propery including the arguments would return a file not found error. Adding them into the StartInfo.Arguments property made it work!
I am now working on reading the EXIF data to give a little more information on the image that is being previewed - more to come on that! I'll also be adding in support for additional filetypes starting with either MP3 or MPG.
Labels: c#, image adjustment, image ratio, ImageLocation, Lazy Folders, picturebox, picturebox.Image, StartInfo.Arguments, StartInfo.FileName
