1

INTRO:

Good day dear coders! I took a look around and wasn't quite able to find answer to my simple question, tho some questions do answer my question, however they seem to be advanced for me to fathom so I'm printing here my simple situation.

QUESTION:

I want to change BackgroundImage or Image of PictureBox and thats how I am doing this:

PictureBox.Image = new Bitmap(@"C:\Users\Ailayna\documents\visual studio 2012\Projects\FormCritterTalking\FormCritterTalking\Character Pictures\CharacterNormal.png");

This picture located in one of my project folders and some how writing whole path to the picture makes no logical sense to me, since I have included all needed pictures to the project, in specific project's folder and I wonder is there a more efficient way of changing picture rather than specifying the whole path of where picture is located?

Is there a way to directly access my folder where my project pictures located using code, like for example: PictureBox.Image = FolderName.PictureName;

I would like to know how are you guys doing this in more efficient and neat way? And another thing is, do I always have to say "new Bitmap"? Can it be something else?

dandan78
  • 13,328
  • 13
  • 64
  • 78
Ailayna Entarria
  • 303
  • 3
  • 16

3 Answers3

1

I am not aware of any PictureBox control in WPF but if you are looking for the same in WPF You can do it like this in XAML:

<Image Source="/MyProject.UI.Common;component/Images/Cut.png"/>

Here your Image is located inside "Images" folder.

1

Is there a way to directly access my folder where my project pictures located using code, like for example: PictureBox.Image = FolderName.PictureName;

Yes, there is. It's called resources. In your project select properties then chose resources. Add image. Access it:

yourProjectName.Properties.Resources.imageName 

However I'd recommend to use streams to access images. This is the right way, especially in case of bitmaps. Do not forget to dispose it afterwards. See my answer here how to do this.

Community
  • 1
  • 1
Na Na
  • 818
  • 3
  • 13
  • 19
  • All of the answers here are valid for my case but Im marking this one as desired answer since its the closes to what I was looking for, thank you every one! – Ailayna Entarria Sep 27 '13 at 04:06
0

Disclaimer: Since you're loading a picture directly off the hard drive (and I'm assuming it's working) I'm guessing we're talking about a WinForms project.

There are a number of options you can choose from.

The simplest of which is probably getting the current application assembly folder and using that:

How do I get the path of the assembly the code is in?

var dir = AppDomain.CurrentDomain.BaseDirectory;

The images should be copied along with the executable. Then you can either use the code above to get the current directory OR you can try and rely on a relative path (from where the executable is located to the images).

Another option is to add the pictures as resources - these will be part of your application, but the procedure of reading them is a bit more complex.

Community
  • 1
  • 1
MBender
  • 5,395
  • 1
  • 42
  • 69