The idea here is that, when a large image is being displayed, there are fewer of them on the screen. I had toyed with the idea of a "smart" algorithm that might create three or so different thumbnails at different sizes. Resizing images on the fly is very time consuming for large images. This seems like a good value that creates presentable images across a variety of resolutions. That is proportional to the master image dimensions, with the width fixed at 256 pixels across. Notice that after the original image is loaded, a thumbnail is created: The images array is locked when adding an image because the main application thread might be in the middle of accessing it. If I don't, the GC doesn't get around to collecting the images for quite a while (seconds to minutes) and memory fills up very quickly, slowing down the whole machine. Of note here is that after the original bitmap is loaded and converted to a thumbnail, I call the Dispose method right away. Using a thread allows the application to start displaying the images as they are converted to thumbnails and the user can also start changing the viewer size, the image size, and dragging images to the single viewer. Dispose of the large image right away rather than waiting // for the GC to do it. Image image= new Bitmap(bitmap, 256, 256*bitmap.Height / bitmap.Width) LoadImagesThread= new Thread( new ThreadStart(LoadImages)) Private void OnDragDrop( object sender, e) Once the drag-drop operation completes, it starts a thread to load the images: There are a few optimization issues to consider: This is because the viewer doesn't actually preserve the source image and I don't want to spend CPU time loading a large image file before it's actually dropped-I'd rather let the receiving application deal with it. Note also that instead of putting the image into the data object, I'm storing the filename to the image. Since I'm only enabling the Copy effect, I don't particularly care what the return value is-whether the drop operation was successful or not. ((Control)sender).DoDragDrop(data, DragDropEffects.Copy) String filenames= new string ĭataObject data= new DataObject(DataFormats.FileDrop, filenames) Private void OnMouseMove( object sender, MouseEventArgs e) On a mouse down event, the image index is obtained and the drag box, which the mouse has to move outside of, is established. This is a system value that the user can set to establish how much the mouse has to move while the left button is down before the motion is considered the beginning of a drag event. Based on the code example for the DoDragDrop method, the primary consideration is to pay attention to the SystemInformation.DragSize value. I wanted to be able to drag an image in the multi-image viewer to my already created single image viewer. Therefore, casting to IDataObject properly handles both and System._ComObject object types. After posting a question on the C# forum and poking around some more, I discovered that System._ComObject implements IDataObject (this is not something that's documented in IDataObject). However, if I create my own string array for sourcing a drag-drop event and try dropping it onto my single image viewer, e.Data is of type System._ComObject. When receiving a file or file list from say, Explorer, e.Data is of type DataObject and the data returned by the " FileDrop" data format type is an Array. This is something I discovered after I wrote my single image viewer article. String ext=Path.GetExtension(fn).ToLower() If ( (e.AllowedEffect & DragDropEffects.Copy) = DragDropEffects.Copy)Īrray data=((IDataObject)e.Data).GetData( " FileDrop") as Array Protected ArrayList GetFiles(DragEventArgs e) (There isn't any error checking to make sure that those files are actually real image files.) When a drag operation is completed, the application calls GetFiles which parses the DragEventArgs data and adds only files of "jpg", "png", or "bmp" extension. The current implementation does recurse into sub-folders. You can drop a folder onto the viewer or individual files. In particular, the application receives only filenames and directory names. Receiving Multiple Files From A Drag-Drop Keep in mind that some of it is a bit hacked. This code is prototype code intended to explore performance and usability issues. This applet is part of the prototypes in the Yet Another Photo Organizer (YAPO) open source project hosted by Wdevs here.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |