Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. accessing folder from project

accessing folder from project

Scheduled Pinned Locked Moved C#
graphicshelp
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jashimu
    wrote on last edited by
    #1

    Hi all, I am trying to accessing a image file from my project. I am getting error saying the path is not valid or Null value. In my Projec I have folder name Image and it has some bmp files and I am trying replace the bmp file one by another depends on the condition I have placed. Two concerns. 1. access the bitmap file 2. replace image from the pictureBox to another. thanks,

    OriginalGriffO 1 Reply Last reply
    0
    • J jashimu

      Hi all, I am trying to accessing a image file from my project. I am getting error saying the path is not valid or Null value. In my Projec I have folder name Image and it has some bmp files and I am trying replace the bmp file one by another depends on the condition I have placed. Two concerns. 1. access the bitmap file 2. replace image from the pictureBox to another. thanks,

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      The simple way:

      Image i = Image.FromFile(path);

      However this will lock the file until the Bitmap object is Disposed. A longer way is to use a stream:

      Image i;
      using (Stream s = System.IO.File.Open(path ,System.IO.FileMode.Open ))
      {
      i = Image.FromStream(s);
      }

      Then just display what you need to:

      myPictureBox.Image = i;

      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      J 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        The simple way:

        Image i = Image.FromFile(path);

        However this will lock the file until the Bitmap object is Disposed. A longer way is to use a stream:

        Image i;
        using (Stream s = System.IO.File.Open(path ,System.IO.FileMode.Open ))
        {
        i = Image.FromStream(s);
        }

        Then just display what you need to:

        myPictureBox.Image = i;

        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."

        J Offline
        J Offline
        jashimu
        wrote on last edited by
        #3

        Hi OriginalGriff,thanks for your reply. I have problem accessing my file. So, what I did is put those .bmp files in the bin debug folder and

        PictureBox.Image = New Bitmap("myImage")

        Now it is working. thanks.

        T OriginalGriffO 2 Replies Last reply
        0
        • J jashimu

          Hi OriginalGriff,thanks for your reply. I have problem accessing my file. So, what I did is put those .bmp files in the bin debug folder and

          PictureBox.Image = New Bitmap("myImage")

          Now it is working. thanks.

          T Offline
          T Offline
          Tony Richards
          wrote on last edited by
          #4

          I think I understand the problem. If you don't specify an absolute file path, the .Net will try and find the image relative to your current program, hence moving the bitmaps solved the problem. If you don't want the images in the same folder as your program, the simplest solution is to specify their absolute path (e.g. c:\Folder\myImage.bmp) If you do want the images to be kept in your program folder, and they are part of your VS project, make sure their Copy To Output Directory property (right click the file, select properties) is set to Copy Always or Copy If Newer.

          1 Reply Last reply
          0
          • J jashimu

            Hi OriginalGriff,thanks for your reply. I have problem accessing my file. So, what I did is put those .bmp files in the bin debug folder and

            PictureBox.Image = New Bitmap("myImage")

            Now it is working. thanks.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            Take note of what Tony says, but also be aware that the new Bitmap(path) constructor does exactly the same thing as the Image.FromFile(path) method: It locks the file until the Bitmap is disposed. MSDN[^] What that means is that is you do:

            PictureBox.Image = New Bitmap("myImage")
            PictureBox.Image = New Bitmap("myOtherImage")
            PictureBox.Image = New Bitmap("myImage")

            You will get an exception because the file "myImage" is in use for the third bitmap. That doesn't mean you can change images in a button click event: the file remains locked until the Bitmap is Disposed. I.e. when the Garbage collector finally gets called in, and decides it is no longer needed. That could be now, could be next week. Use the Stream method instead: it does not have that problem. [edit]Luc kindly pointed out that it is a read lock, so only write, rename, and delete operations will be affected. So if you are just displaying the image and not changing the file, it won't be a problem.[/edit]

            Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."

            modified on Monday, February 28, 2011 3:27 PM

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            L 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Take note of what Tony says, but also be aware that the new Bitmap(path) constructor does exactly the same thing as the Image.FromFile(path) method: It locks the file until the Bitmap is disposed. MSDN[^] What that means is that is you do:

              PictureBox.Image = New Bitmap("myImage")
              PictureBox.Image = New Bitmap("myOtherImage")
              PictureBox.Image = New Bitmap("myImage")

              You will get an exception because the file "myImage" is in use for the third bitmap. That doesn't mean you can change images in a button click event: the file remains locked until the Bitmap is Disposed. I.e. when the Garbage collector finally gets called in, and decides it is no longer needed. That could be now, could be next week. Use the Stream method instead: it does not have that problem. [edit]Luc kindly pointed out that it is a read lock, so only write, rename, and delete operations will be affected. So if you are just displaying the image and not changing the file, it won't be a problem.[/edit]

              Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."

              modified on Monday, February 28, 2011 3:27 PM

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              OriginalGriff wrote:

              You will get an exception because the file "myImage" is in use for the third bitmap.

              Now there is a bit I disagree with. The Bitmap class locks files as you say, however they are writer locks, not reader locks. So you can't alter, delete, rename the file while such bitmaps are not disposed (*), but you can open them for reading as often as you like. (*) or is it indisposed? :-D

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              OriginalGriffO 1 Reply Last reply
              0
              • L Luc Pattyn

                OriginalGriff wrote:

                You will get an exception because the file "myImage" is in use for the third bitmap.

                Now there is a bit I disagree with. The Bitmap class locks files as you say, however they are writer locks, not reader locks. So you can't alter, delete, rename the file while such bitmaps are not disposed (*), but you can open them for reading as often as you like. (*) or is it indisposed? :-D

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                I'm glad you disagree, because it made me check! That'll teach me to trust MS documentation... :laugh: Since it said "The file remains locked until the Bitmap is disposed" I stupidly assumed it meant "Locked" not "Read locked". Oh, well - I learnt something new, and any day when you do that is not wasted. :)

                Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                L 1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  I'm glad you disagree, because it made me check! That'll teach me to trust MS documentation... :laugh: Since it said "The file remains locked until the Bitmap is disposed" I stupidly assumed it meant "Locked" not "Read locked". Oh, well - I learnt something new, and any day when you do that is not wasted. :)

                  Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  I did test your example before posting, as it didn't fit with earlier experience, and it seemed to behave much like EXE files do: you can launch them many times, however as long as a process is executing an EXE, the file can't be written/renamed/deleted. So yes it is locked, but not completely locked. I don't know what the official terminology is, what comes to mind is FileShare.Read as a parameter to File.Open :)

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  OriginalGriffO 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    I did test your example before posting, as it didn't fit with earlier experience, and it seemed to behave much like EXE files do: you can launch them many times, however as long as a process is executing an EXE, the file can't be written/renamed/deleted. So yes it is locked, but not completely locked. I don't know what the official terminology is, what comes to mind is FileShare.Read as a parameter to File.Open :)

                    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #9

                    Makes sense when you think about it - I modified my response to correct it.

                    Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    L 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      Makes sense when you think about it - I modified my response to correct it.

                      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      :thumbsup:

                      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups