简体   繁体   中英

Set Dedault Picture For PictureBox And Save it C#

I want to know how i can get the picture from the project folder and if User not choice any picture i can save it as Default persoanl picture

i use this code for create name, path and save ( if i have any pic eveything as fine, if user not choice any picture my code broken

                //Set Image Name                    
                string imageName = txtNationalCode.Text.ToString() + Path.GetExtension(imgPersonalPhoto.ImageLocation);
                //Set Image Path
                string ImageLocation = imgPersonalPhoto.ImageLocation;
                //Save Image 
                string path = Application.StartupPath + "/Images/";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                try
                {
                    if (imgPersonalPhoto.Image != null)
                    {
                        imgPersonalPhoto.Image.Save(path + imageName);
                    }
                    else
                    {
                        //i dont know how set some picture for default and save it ! ( i have 1 picture for background picturebox

                        imgPersonalPhoto.Image.Save(path + imageName);
                    }
                }
                catch
                {
                    RtlMessageBox.Show("Add Picture for this Personal please ");
                }

i will try add some picture in my app Folder but if i send my app to other this folder not exist !

Set the image in Form_Load using Image.FromFile. This way it will always set the default image when the form is loaded.

private void Form1_Load(object sender, EventArgs e)
{
    pictureBox1.Image = Image.FromFile(@"C:\...");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM