隐藏

winform 通过 PictureBox添加图片的四种方法。

发布:2024/1/25 16:17:25作者:管理员 来源:本站 浏览次数:587

            //1,从项目资源文件中加载图片,首先将图片添加到项目资源中(工程项目下 - Properties 下双击Resources.resx进行添加资源)
            //this.pictureBox1.Image = TreeView20190926.Properties.Resources.Penguins;


            //2,从窗体资源文件中加载图片  --- 前提需要先在窗体路径下找到资源文件(后缀为.resx)双击然后将图片添加进去。
            //System.Resources.ResourceManager rm = new System.Resources.ResourceManager(typeof(Form1));
            //this.pictureBox1.Image = (Bitmap)rm.GetObject("Hydrangeas");


            //3,加载本地图片,使用Image.FromFile()加载计算机中的图片
            //取得程序当前所在路径,这点很重要
            //string appPath = System.Windows.Forms.Application.StartupPath;
            仅当图片存在时才加载图片
            //if (System.IO.File.Exists(appPath + @"\Hydrangeas.jpg")) //图片需跟exe同一路径下
            //{
            //    Image img = Image.FromFile(appPath + @"\Hydrangeas.jpg");
            //    this.pictureBox1.Image = img;
            //}


            //4,加载网络图片,当网络比较慢,加载时间长时可看到设置的InitialImage的效果。
            //通过ImageLocation设置网络图片
            this.pictureBox1.ImageLocation = "https://img-my.csdn.net/uploads/201407/26/1406382766_5762.jpg";
            //从网络获取图片  https://blog.csdn.net/zhq217217/article/details/53379383