Loading PictureBox Image from resource file with path (Part 3)

前端 未结 5 872
北荒
北荒 2020-12-03 07:19

I understand that this question has been asked (and answered) before. However, none of the solutions are working for me.

Below is a screen capture of all the relevan

相关标签:
5条回答
  • 2020-12-03 07:34

    Setting "Copy to Output Directory" to "Copy always" or "Copy if newer" may help for you.

    Your PicPath is a relative path that is converted into an absolute path at some time while loading the image. Most probably you will see that there are no images on the specified location if you use Path.GetFullPath(PicPath) in Debug.

    0 讨论(0)
  • 2020-12-03 07:46

    The accepted answer has major drawback!
    If you loaded your image that way your PictureBox will lock the image,so if you try to do any future operations on that image,you will get error message image used in another application!
    This article show solution in VB

    and This is C# implementation

     FileStream fs = new System.IO.FileStream(@"Images\a.bmp", FileMode.Open, FileAccess.Read);
      pictureBox1.Image = Image.FromStream(fs);
      fs.Close();
    
    0 讨论(0)
  • 2020-12-03 07:47

    The path should be something like: "Images\a.bmp". (Note the lack of a leading slash, and the slashes being back slashes.)

    And then:

    pictureBox1.Image = Image.FromFile(@"Images\a.bmp");
    

    I just tried it to make sure, and it works. This is besides the other answer that you got - to "copy always".

    0 讨论(0)
  • 2020-12-03 07:51

    It depends on your file path. For me, the current directory was [project]\bin\Debug, so I had to move to the parent folder twice.

    Image image = Image.FromFile(@"..\..\Pictures\"+text+".png");
    this.pictureBox1.Image = image;
    

    To find your current directory, you can make a dummy label called label2 and write this:

    this.label2.Text = System.IO.Directory.GetCurrentDirectory();
    
    0 讨论(0)
  • 2020-12-03 07:53

    Ok...so first you need to import the image into your project.

    1) Select the PictureBox in the Form Design View

    2) Open PictureBox Tasks
    (it's the little arrow printed to right on the edge of the PictureBox)

    3) Click on "Choose image..."

    4) Select the second option "Project resource file:"
    (this option will create a folder called "Resources" which you can access with Properties.Resources)

    5) Click on "Import..." and select your image from your computer
    (now a copy of the image will be saved in "Resources" folder created at step 4)

    6) Click on "OK"

    Now the image is in your project and you can use it with the Properties command. Just type this code when you want to change the picture in the PictureBox:

    pictureBox1.Image = Properties.Resources.MyImage;
    

    Note:
    MyImage represent the name of the image...
    After typing "Properties.Resources.", all imported image files are displayed...

    0 讨论(0)
提交回复
热议问题