C # Image Rendering control PictureBox

Source: Internet
Author: User

in C #, you can use the PictureBox control to render an image, an image resource can come from a file, or it can be a bitmap object that exists in memory. You can display a local image file or a picture from a network, or from an image in a project file.

Loads an image file from a URI.

Either the load (synchronous load) or the LoadAsync (asynchronous load) method is called to load the image from the specified URI. If the image is large, it can be loaded asynchronously using the LoadAsync method, which ensures that the user interface is able to respond to user actions and that there is a deadlock phenomenon

If you use asynchronous loading, you can also handle the Loadprogresschanged event, which is raised when the progress of the load changes. The LoadCompleted event occurs when an image is loaded, changed, or canceled.

1. Create a project

2. Drag the TextBox from the Toolbox as the input to the picture path, button as the confirmation load buttons, PictureBox as the picture loading container

Layout

3. Add Key events

 Private voidButton1_Click (Objectsender, EventArgs e) {            //determine if the Txturi character is empty            if(string. Isnullorwhitespace (Txturi.text)) {MessageBox.Show ("Please enter an image URI","Tips", MessageBoxButtons.OK, messageboxicon.warning); return; }            //Close the Load buttonButton1. Enabled =false; //loading images asynchronously, loading Txturi imagesPicturebox1.loadasync (Txturi.text); }

4. We can add, loadprogersschanged (Load Progress) and LoadComplete (load error) events because we are using asynchronous load mode

 Private voidPicturebox1_loadprogresschanged (Objectsender, ProgressChangedEventArgs e) {            //Show Progress             This. Lblmsg.text =string. Format ("Current progress: {0}.", E.progresspercentage); }        Private voidPicturebox1_loadcompleted (Objectsender, AsyncCompletedEventArgs e) {            //loading error handling Events            if(E.error! =NULL) {Lblmsg.text="error message:"+E.error.message; return; }            //If you cancel            if(e.cancelled) {Lblmsg.text="The operation was canceled. "; }            Else{Lblmsg.text="loading is complete."; } button1. Enabled=true; }

Run the program, enter the image URI in the text box, either a local image full path or a network picture address.

C # Image Rendering control PictureBox

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.