bilal
bilal

Reputation: 127

I want to load default image in picturebox if the user does not select an image in C#

I am developing a window form in C#. In my window form there is a picture box. I want if the user does not select an image then the default image will be load in picture box which is save in my project folder.

thanks in advance

Upvotes: 3

Views: 5516

Answers (2)

Neowizard
Neowizard

Reputation: 3027

Either set an image in the designer and override it in code

for instance you can place this after user selection:

if (someConditionIndicatingUserAction)
    pictureBoxControlName.Image = UserSelectedImage;

Or, you can store a default value in a variable and set the picturebox image to it (if the user selects nothing, you don't change the value, and if he does, change it accordingly)

Upvotes: 0

Random Dev
Random Dev

Reputation: 52300

I guess you want to know how you can get the picture from the project folder right?

First add the picture to you project (add existing item) and set the Build Action to Embedded Resource: enter image description here

then the following code do the trick:


        private void SetPicture()
        {
            var assembly = System.Reflection.Assembly.GetExecutingAssembly();
            using (var imgStream = assembly.GetManifestResourceStream("DataGrid.TestImage.jpg"))
            {
                var img = new Bitmap(imgStream);
                Picturebox.Image = img;
            }
        }

where "DataGrid" is MY project-name (you have to insert your own) and "TestImage.jpg" is the name of your Image (if you put it into a folder you might have to give the foldername too). Picturebox is a PictureBox-Control I set the image to.

Upvotes: 6

Related Questions