accessing folder from project
-
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,
-
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,
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."
-
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."
-
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.
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.
-
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.
Take note of what Tony says, but also be aware that the
new Bitmap(path)
constructor does exactly the same thing as theImage.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
-
Take note of what Tony says, but also be aware that the
new Bitmap(path)
constructor does exactly the same thing as theImage.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
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.
-
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.
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'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 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 toFile.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.
-
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 toFile.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.
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."
-
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."
: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.