C # proportional scaling of picturebox or Dev control pictureedit

Source: Internet
Author: User

Method 1:

If the aspect ratio of the image to be loaded is not too unbalanced,

1. The sizemode attribute of picturebox can be changed to pictureboxsizemode. stretchimage,

2. Alternatively, the sizemode attribute of the dev control pictureedit Is Zoom. (ZOOM: Zoom; clip; stretchhorizontal: vertical stretch; stretchvertical: horizontal stretch; squeeze: compression)

In this way, the image loaded into the picturebox will be filled with the entire picturebox. However, this method is not very good when the aspect ratio of an image is large. You can select method 2.

Method 2:

Openfiledialog ofdpic = new openfiledialog ();

Ofdpic. Filter = "JPG (*. jpg; *. JPEG); GIF file (*. GIF) | *. jpg; *. JPEG; *. GIF ";

Ofdpic. filterindex = 1;

Ofdpic. filename = "";

If (ofdpic. showdialog () = dialogresult. OK)

{

String spicpaht = ofdpic. filename. tostring ();

Bitmap BMP Ic = new Bitmap (spicpaht );

Point ptloction = new point (bmp ic. size );

If (ptloction. x> mypicbox. Size. Width | ptloction. Y> mypicbox. Size. Height)

{

// The method of stopping the Image Frame

// Pcbpic. Dock = dockstyle. Fill;

// The image is filled with the image frame and the image holding ratio

Mypicbox. sizemode = pictureboxsizemode. Zoom;

}

Else

{

// The image is in the Image Frame

Mypicbox. sizemode = pictureboxsizemode. centerimage;

}

// Loadasync: Non-synchronous transfer to image

Mypicbox. loadasync (spicpaht );

}

3. pictureedit

Nulltext: Text displayed when no image is displayed or cannot be displayed

Readonly read-only or not

Showmune right-click the menu bar

Select zoom for the sizemode Image Display format to display proportional Scaling

The image displayed in pictureedit can be an image resource file in resource. resx, an image file on the hard disk, or an image file with a URL on the network.

Next, let's take a look at how to implement these images.
1 ,. the image resource file in resx is easier to set and select the resource file directly in the image attribute. You can also set the pictureedit1.image = resource1. _ 20090119102027514; // Resource class name + image file name in the code.
2. Displaying files in the hard disk is also relatively simple and direct introduction of the namespace using system. IO;

Use the code pictureedit1.image = image. fromfile (PATH); path is the absolute path on the image hard disk.
3. to display images in the network, you need a URL address.
Image OBJ = image. fromstream (system. net. webrequest. Create (ImagePath). getresponse (). getresponsestream ());
Use webrequest. Create to get the image stream and then directly pictureedit1.image = obj.
You can also use pictureedit1.image = new Bitmap (new system. net. WebClient (). openread (URL); WebClient to retrieve images.

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.