OpenFileDialog control

  • Thread starter Thread starter Lina
  • Start date Start date
L

Lina

Hello,

I wanted to ask if anyone can help me complete the
following code. I found this example while searching
through help documnets. What i want to achieve is to open
a dialog box that lets the user select an images file.
I am using the openFileDialog control.

Here is what i have done so far for the onlick event for
the button...
Dim myStream As Stream
Dim openFileDialog1 As New OpenFileDialog()

openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All
files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True

If openFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = openFileDialog1.OpenFile()
If Not (myStream Is Nothing) Then
' Insert code to read the stream here.
myStream.Close()
End If
End If

Im not sure on what to write for the stream code (where
the comment is). Does anyone have any useful articles or
advise.

thx.
 
Here is some C# code sample:

OpenFileDialog authFileDlg = new OpenFileDialog();
authFileDlg.InitialDirectory = ".";
authFileDlg.Filter = "auth files (*.fox)|*.fox|All files
(*.*)|*.*";
authFileDlg.FilterIndex = 1;
authFileDlg.RestoreDirectory = true;
try
{
if (authFileDlg.ShowDialog() == DialogResult.OK)
{
Stream myStream = null;
m_arAuth.Clear(); //clear the list
if ((myStream = authFileDlg.OpenFile()) != null)
{
//get the login details
BinaryFormatter myBinFormat = new BinaryFormatter ();
m_arAuth = (ArrayList)myBinFormat.Deserialize(myStream);
myStream.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "!!! OPEN - ERROR !!!");
return;
}
 
Hi Lina,

I do not understand your question, (I understand it but why a stream) if you
want an image, the only reason can be in my opinion if you want to write it
to a database direct withouth showing it or something.

If you only want the image and you do not have used a picture box then it is
\\\
dim myimage as image
If openFileDialog1.ShowDialog() = DialogResult.OK Then
myimage.fromfile(openFileDialog1.filename)
End If
///

I hope this helps,

Cor
 
Hi Cor,

I want to load the image into a picture box.
I wasnt to sure what i was doin and i found this example
with the stream in it!! :o)

What do i write if i want to insert the image that is
selceted by the user into my picturebox?
Of course i will have to make sure that the user actully
selects a file!

Also, I have managed to set the filter to bitmaps, jpegs,
and all files. However i only want the all files to show
all the image files. How would i write this??

Thx for for help#
:)
 
Hi Lina,

fdlOpen.Filter = "All Image Formats (*.bmp;*.jpg;*.jpeg;*.gif;*.tif)|" & _
"*.bmp;*.jpg;*.jpeg;*.gif;*.tif|Bitmaps (*.bmp)|*.bmp|" &
_
"GIFs (*.gif)|*.gif|JPEGs (*.jpg)|*.jpg;*.jpeg|TIFs
(*.tif)|*.tif"

And than of course simple.

Picturebox1.image.fromfile(openFileDialog1.filename)

I hope this was what you where looking for?

Cor
 
Hi Cor,

I have used the following code but the image doesnt seem
to load into my picture box!!

OpenFileDialog1.InitialDirectory = "c:\"
OpenFileDialog1.Filter = "All Image Formats
(*.bmp;*.jpg;*.jpeg;*.gif;*.tif)|" & _
"*.bmp;*.jpg;*.jpeg;*.gif;*.tif|Bitmaps (*.bmp)|*.bmp|"
& _
"GIFs (*.gif)|*.gif|JPEGs (*.jpg)|*.jpg;*.jpeg|TIFs
(*.tif)|*.tif"

OpenFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True

If openFileDialog1.ShowDialog() = DialogResult.OK Then
PictureBox1.Image.FromFile(OpenFileDialog1.FileName)
End If

-----Original Message-----
Hi Lina,

fdlOpen.Filter = "All Image Formats
(*.bmp;*.jpg;*.jpeg;*.gif;*.tif)|" & _
 
Hi Lina,

Sorry it was to quick and dirty typed

PictureBox1.Image.FromFile(OpenFileDialog1.FileName)

PictureBox1.Image = image.FromFile(OpenFileDialog1.FileName)

I think it will go now?

Cor
 
Back
Top