The C#picturebox control uses

Source: Internet
Author: User

PictureBox is a common image space in C #, this article is the study of the collection of network data, some collation and record

1,picturebox Loading Pictures

Using system.drawing;//mode 1, load from picture file  //The following path is write dead, you can get the program run path, so more flexible       image AA = new Bitmap (@ "/program files/ Pictureboxcontroltest/tinyemulator_content.jpg ");
     pictureBox1.Image =aa;
Mode 2, with the ImageList control loaded, the advantage of this method is that you can set the size of the picture imagelist1.imagesize = new Size (92,156); pictureBox1.Image = Imagelist1.images[0]; Mode 3 is loaded from the program's resource file. The advantage of this is that there is no need to publish the image and has been integrated into the assembly. Bitmap bmp=new Bitmap (System.Reflection.Assembly.GetExecutingAssembly (). GetManifestResourceStream ("PictureBoxControlTest.tinyemulator_res.jpg")); pictureBox1.Image =bmp;

2 Local picture cannot delete problem after loading picture (load Mode 1)

Local pictures still cannot be deleted after you load using mode 1

Solution 1.0, setting the Image to null (picturebox1.image=null) still does not work because the resource is not released (waiting for the garbage collector to release automatically);

Solution 2.0, the manual release of resources on the basis of 1.0 (Aa.dispose ();), if the PictureBox is still in use, may error;

Solution 3.0, the stream will be used to read the image into memory and then use the copy operation

        Private MemoryStream Myreadfile (string picpath)        {            if (! File.exists (Picpath))                return null;            using (FileStream file = new FileStream (Picpath, FileMode.Open))            {                byte[] b = new Byte[file. Length];                File. Read (b,0,b.length);                MemoryStream stream = new MemoryStream (b);                return stream;            }        }        Private Image Mygetfile (string picpath)        {            MemoryStream stream = Myreadfile (Picpath);            return stream = = null? Null:Image.FromStream (stream);        }

3 PictureBox Property SizeMode

SizeMode = Normal, image is placed in the upper-left corner of the PictureBox, and any part of the image that is too large to fit the PictureBox will be trimmed off;

Sizemode=stretchimage, which causes image images to be stretched or shrunk to fit the size of the PictureBox control;

Sizemode=autosize, the PictureBox control is resized so that it always fits the image size;

Sizemode=centerimage, the image will be in the center of the PictureBox work area, others are cut off;

Sizemode=zoom, causes the image to be stretched or shrunk to fit the PictureBox control, but still retains the original aspect ratio (the image size is increased or decreased by its original size )

Original link:

http://blog.csdn.net/patriot074/article/details/4295847

http://blog.csdn.net/jinhongdu/article/details/7703251

Http://msdn.microsoft.com/zh-cn/library/system.windows.forms.pictureboxsizemode%28v=vs.110%29.aspx

(go) the C#picturebox control uses

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.