0

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 !

  • If you already have an image for a default then there doesn't seem much point loading it into a picture box just so you can save it again? – Caius Jard Jun 12 '20 at 20:58
  • @CaiusJard i have some pick in Resource folder and i use it for backgroundimage ( i dont know how i can save it for default picture ) i will try 2 or 3 code but not work – Ali Zangeneh Jun 13 '20 at 18:09

1 Answers1

0

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:\...");
}
Tim
  • 170
  • 10
  • this code not work if i install my app in other pc, in my pc my app in drive D maybe in other pc this app install in drive C – Ali Zangeneh Jun 13 '20 at 17:54
  • Well, you *can* use Application.StartupPath to know the folder your app is in, so e.g. `Path.Combine(Application.StartupPath, "defaultbackground.png")` then ship defaultbackground.png with your app... – Caius Jard Jun 13 '20 at 19:15
  • @CaiusJard cant find my Resouce Folder in my app StartupPath – Ali Zangeneh Jun 14 '20 at 00:12
  • Resources (if you mean what I think you mean) are compiled into the program. I meant just put a PNG file on disk, next to your program you can do that by by adding to your project and setting "build action: content" in its properties – Caius Jard Jun 14 '20 at 05:38