![]() The size of the thumbnails is hard-encoded in imageViewer.LoadImage(imageFilename, 256, 256) but the display size is defined by the size of the control. Now that we have the basis of our application, we can easily add some nice features. The control does not jump anymore because we are telling the panel to ignore any active control. Protected override Point ScrollToControl(Control activeControl) In this example, we used it whenever the controller sends an event to the view. Use Invoke to perform the call: the method posts a message in the queue of the control/window with your callback function (you therefore need a delegate), this is thread safe. Use InvokeRequired to perform the test: the property simply tests if the working thread Id is the same as the control thread Id. NET provides a simple way to check if you are running in the control thread and to invoke methods in a safe way. You cannot directly change properties of these controls without taking the chance of an ugly exception, it may happen or not. In short, your form is running in its own thread and a different thread interacts with it or its controls. If you are going to use threads in Windows Forms, be careful of threading issues. ImageViewer.LoadImage( imageFilename, 128, 128) ImageViewer imageViewer = new ImageViewer() Private void AddImage( string imageFilename) M_Controller.OnEnd += new ThumbnailControllerEventHandler(m_Controller_OnEnd) M_Controller.OnAdd += new ThumbnailControllerEventHandler(m_Controller_OnAdd) M_Controller.OnStart += new ThumbnailControllerEventHandler(m_Controller_OnStart) ![]() M_Controller = new ThumbnailController() The controller will simply start a thread with one parameter: the path to be scanned. In order to fulfill the asynchronous requirement, we will use a controller class that scans directories, looks for images and does its stuff in a thread. In the project demo, the class ImageViewer implements a user control for image rendering. render the image m_Image with a scaling to the size of the control // for that we have to override the OnPaint of the usercontrol protected override void OnPaint(PaintEventArgs e) TempImage.Dispose() // do not forget to dispose and don't wait for GC G.InterpolationMode = InterpolationMode.HighQualityBicubic Graphics g = Graphics.FromImage(m_Image) Image tempImage = Image.FromFile(imageFilename) load the image and scale it to the given width and height // we don't use GetThumbnailImage because GDI does not always provide // an optimal quality public void LoadImage( string imageFilename, int width, int height) We could work with these two controls like this: ![]() This control is good at showing images with some options like SizeMode = PictureBoxSizeMode.Zoom. Now let's continue with an image viewer: PictureBox. If you want to know more about anchoring and docking, have a look at How to: Anchor and Dock Child Controls in a FlowLayoutPanel Control. Now you get the point: just use FlowLayoutPanel, set the property FlowDirection to FlowDirection.LeftToRight and that's it. This panel dynamically lays out its contents horizontally or vertically, contents are of course controls. The main idea is to use the FlowLayoutPanel control.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |