1、从项目资源文件中加载图片,首先将图片添加到项目资源中(工程项目下-Properies 下双击Resources.resx进行添加资源)
pictureBox1.lmage=xxx,Properties.Resources.Penguins;
2、从窗体资源文件中加载图片-前提需要先在体路径下找到资源文件(后缀为.resx)双击然后将图片添加进去。
System.Resources.ResourceManager m=new System.ResourceS.Resource Manager (typeof (Form1));
pictureBoxl.lmage=(Bitmap)rm.GetObject("xxx");
3、加载本地图片,使用Image.From File()加载计算机中的图片
//取得程序当前所在路径,这点很重要
string appPath=System.Windows.Forms.Application.StartupPath ;
//仅当留片存在时才加载图片
if(System.IO.File.Exists(apppath+@"\xxx.jpg"))//图片需exe同一路径下
{
Image img=lmage.FromFile(appPath+@"\xxx.jpg");
pictureBox1.Image = img;
}
4、加载网络图片,当网络比较慢,加载时间长时可看到设置的初始图像的效果。
//通过Image Location设置网络图片
pictureBox1.ImageLocation ="https://xxx/xxx,jpg";
PS:
//设置背景透明
pictureBox1.Parent = panel1;
pictureBox1.BackColor = Color.Transparent;