[C # development skills series] C # How to Implement Image Viewer

Source: Internet
Author: User
Summary of this topic
    • I. Introduction
    • II. Implementation ideas
    • Iii. Implementation results
    • Iv. Summary
I. Introduction

Recently, I have seen some friends in the msdn and csdn forums asking me how to use C # To implement a function similar to the image Viewer provided by Windows (and of course how to rotate images )., how to Use buttons to change image functions), so in order to help you better solve such problems, this articleArticleThis section briefly introduces how to use C # To implement the function of an image viewer. The functions saved by this tool include:

    1. You can rotate images by clicking the "previous" and "Next" button.
    2. Rotate Images
    3. Saves images after rotation. BenProgramIt not only provides the implementation of Rotating 90/180/270, but also provides a method to achieve the rotation of any angle.
    4. This program does not implement the Windows Image viewing and scaling function. The main point of this function is to change the height and width of the image in the picturebox control.
II. Implementation ideas 2.1 Implementation of the image rotation browsing Function

First, we will analyze the implementation of the first function. To achieve image rotation and browsing, we can follow the following ideas:

    • Step 1: Obtain the set of all images in the directory.Directory. getfiles ()To obtain all the files in the directory, and then filter the set to filter out the files with images,CodeFiltering by extension
    • Step 2. After obtaining all the image sets, you need to change the index of the set to achieve the previous and next functions.
    • Step 3: Consider the last or first image, and then click the next or previous image to rotate it into the first or last one.

The idea is the above. With the above idea, let's take a look at the specific code to compare and understand:

 // Step 1  //  Obtain the image set in the preview image file path.          Public   Static List < String > Getimgcollection ( String  Path ){  String [] Imgarray = Directory. getfiles (PATH );  VaR Result = From Imgstring In  Imgarray  Where Imgstring. endswith ( "  JPG  " , Stringcomparison. ordinalignorecase) | Imgstring. endswith (  "  PNG  " , Stringcomparison. ordinalignorecase) | Imgstring. endswith (  "  BMP  "  , Stringcomparison. ordinalignorecase)  Select  Imgstring;  Return  Result. tolist ();}  // Step 2  //  Obtain the index of the opened image in the image collection.          Private  Int Getindex ( String  ImagePath ){  Int Index = 0  ;  For ( Int I = 0 ; I  ){  If  (Imgarray [I]. Equals (ImagePath) {Index =I;  Break  ;}}  Return  Index ;}  //  How to switch Images          Private   Void Switchimg ( Int  Index) {newbitmap = Image. fromfile (imgarray [Index]); picboxview. Image = Newbitmap; imgpath =Imgarray [Index];}  // Step 3  //  Previous Image          Private   Void Btnpre_click ( Object  Sender, eventargs e ){  Int Index = Getindex (imgpath );  //  Release the resources of the previous image to avoid the exception of externalexception when saving  Newbitmap. Dispose (); If (Index = 0  ) {Switchimg (imgarray. Count - 1  );}  Else  {Switchimg (Index - 1  );}}  //  Next image          Private   Void Btnnext_click ( Object Sender, eventargs e ){  Int Index = Getindex (imgpath );  // Release the resource of the previous image to avoid the exception of externalexception when saving
// When the Save method is called, a common GDI error is often returned, mainly because the file is not released and the exception occurs when it is saved to the original location, to avoid this error, we need to release the resources occupied by images. Newbitmap. Dispose (); If (Index! = Imgarray. Count- 1 ) {Switchimg (Index + 1 );} Else {Switchimg ( 0 );}}
2.2 Implementation of image rotation

The above code implements the first function point. The following explains how to implement the second function point-image rotation:

For the Image Viewer that comes with windows, its rotation angle can only Rotate 90 degrees clockwise or 90 degrees clockwise. This function can be very simple to achieve, you only need to useImage. rotateflip (rotatefliptype)The method can be done. Some friends also want to rotate the image at any angle. The source code of this problem also has specific implementation. You can download the source code from the end of the article to view it, here we will not post the specific code. Next we will look at how to implement the Code for rotating the Image Viewer that comes with Windows:

    //  Rotate images 90 degrees clockwise          Private   Void Btnrotate_click ( Object Sender, eventargs e) {picboxview. sizemode = Pictureboxsizemode. Zoom;  //  Another implementation of 90 degrees clockwise rotation  Newbitmap. rotateflip (rotatefliptype. rotate90flipnone); picboxview. Image = Newbitmap; isrotate = True  ;  //  Newbitmap = (image) imagemanager. rotateimg (bitmap, 90f, color. Transparent );;  // Bitmap. Dispose ();  //  Picboxview. Image = newbitmap;  }  //  Rotate 90 degrees counterclockwise          Private   Void Btncounterclockwiserotate_click ( Object  Sender, eventargs e) {picboxview. sizemode = Pictureboxsizemode. Zoom;  //  90 degrees of counter-clockwise rotation Newbitmap. rotateflip (rotatefliptype. rotate270flipnone); picboxview. Image = Newbitmap; isrotate = True  ;  //  The code for rotating any angle is as follows:  //  Newbitmap = (image) imagemanager. rotateimg (bitmap, 360f-90f, color. Transparent );;  //  Bitmap. Dispose ();  //  Picboxview. Image = newbitmap; }
2.3 Implementation of the rotating image storage function

The last step is the implementation of rotating image storage. At this time, I refer to the implementation method of Windows built-in Image Viewer, because I use Windows built-in Image Viewer to browse the image implementation. When I rotate the image, it does not store images in real time, but when I close the windows built-in Image Viewer, The Rotated Images are saved to the file. With this idea, I put the code logic I saved in the closed event handler of the form. At this time, we only need to call the Save function.Image. Save (PATH)You can save the image by using the following code:

  //  Close the form and save the rotated image to the file.          Private   Void Form1_formclosed ( Object  Sender, formclosedeventargs e ){  If (Imgpath = Null | Isrotate = False  ){  Return  ;}  //  Save the rotated image              Switch  (Path. getextension (imgpath). tolower ()){  Case   "  . PNG  "  : Newbitmap. Save (imgpath, imageformat. PNG); newbitmap. Dispose (); Break  ;  Case   "  . Jpg  "  : Newbitmap. Save (imgpath); newbitmap. Dispose ();  Break  ;  Default  : Newbitmap. Save (imgpath, imageformat. BMP); newbitmap. Dispose ();  Break  ;}} 
Iii. Implementation results

I have already introduced the idea of implementing this program. Can you wait to see what the custom image Viewer looks like? The following uses an animation to show the running effect of a program more vividly:

 

Iv. Summary

The content of this article is over. I hope you can solve similar problems quickly from this blog. In addition, the link to this issue in msdn is included:

Http://social.msdn.microsoft.com/Forums/zh-CN/visualcshartzhchs/thread/89d09d59-ab82-4e41-896f-daab68edbd10

Download the source code of this topic: Image Viewer

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.