(转)C#picturebox控件使用

       PictureBox是C#常用图片空间,本文是学习中搜集网络资料的一些整理和记录

      1,PictureBox加载图片

          

using System.Drawing;

//方式1,从图片文件载入  
//下面的路径是写死的,可以获取程序运行路径,这样更灵活  
     Image AA = new Bitmap(@"/Program Files/PictureBoxControlTest/tinyemulator_content.jpg");
     pictureBox1.Image =AA;
//方式2,通过imageList控件载入,这种方法的好处是可以设置图片的大小 imageList1.ImageSize = new Size(92,156); pictureBox1.Image = imageList1.Images[0]; 方式3 从程序的资源文件载入.这样做的好处是无需发布图片,已经被整合到程序集中去了. Bitmap bmp=new Bitmap (System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream ("PictureBoxControlTest.tinyemulator_res.jpg")); pictureBox1.Image =bmp;

  2 加载图片后本地图片无法删除问题(加载方式1)

              在使用方式1加载后,本地图片仍然无法删除

              解决方案1.0,将Image设置为空(pictureBox1.Image=NULL)仍然不行,原因是资源没有释放(等待垃圾回收器自动释放);

              解决方案2.0,在1.0的基础上进行手动释放资源(AA.Dispose();),如果picturebox仍然在使用,可能会报错;

              解决方案3.0,将使用 流将image读入内存,然后使用副本操作

             

        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属性 SizeMode

       SizeMode = Normal ,Image 置于 PictureBox 的左上角,凡是因过大而不适合 PictureBox 的任何图像部分都将被剪裁掉;

        SizeMode=StretchImage ,会使Image图像拉伸或收缩,以便适合 PictureBox控件大小;

        SizeMode=AutoSize ,会使PictureBox控件调整大小,以便总是适合Image图像的大小;

       SizeMode=CenterImage,会使图像居于PictureBox工作区的中心,其它被剪掉;

       SizeMode=Zoom ,  会使图像被拉伸或收缩以适应 PictureBox控件,但是仍然保持原始纵横比(图像大小按其原有的大小比例被增加或减小

      原文链接:

                 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

原文地址:https://www.cnblogs.com/hhhh2010/p/4088487.html