Store output of OpenFileDialog in memorystream

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I need to know how to store the OpenfileDialog.OpenFile() in a memorystream. Do I need to do some kind of conversion before this works
I can't write

dim mem as new memorystream(
mem = OpenfileDialog.Openfile(

It says not supported cast or something like that

So, how to do this

Also, how do I make a memorystream public/global

Thank you VERY much in advance - this will really help me out, it's the last thing to do before I'm done with my project

Rasmus
 
* "=?Utf-8?B?UmFzbXVzIFRlZ2xnYWFyZA==?= said:
I need to know how to store the OpenfileDialog.OpenFile() in a memorystream. Do I need to do some kind of conversion before this works?
I can't write:

dim mem as new memorystream()
mem = OpenfileDialog.Openfile()

You will have to declare 'mem' as 'Stream', then read from this stream
using a 'BinaryReader' and construct a 'MemoryStream' from the byte data
read from the stream.
Also, how do I make a memorystream public/global?

Where? Inside the project?
 
Hi Herfried

Thanks for your quick reply

Can you make an example? I'm not that good a programmer, so if you will please bear with me

About the public/global. I've got some code under a Privat button event. In here I want to be able to make a memorystream that I can access from the code in another private button event
I've tried to write
public mem as memorystrea
but this won't work, I can't select memorystream after the AS word

Thanks in advance
Rasmu

----- Herfried K. Wagner [MVP] wrote: ----

* "=?Utf-8?B?UmFzbXVzIFRlZ2xnYWFyZA==?= said:
I need to know how to store the OpenfileDialog.OpenFile() in a memorystream. Do I need to do some kind of conversion before this works
I can't write
mem = OpenfileDialog.Openfile(

You will have to declare 'mem' as 'Stream', then read from this strea
using a 'BinaryReader' and construct a 'MemoryStream' from the byte dat
read from the stream
Also, how do I make a memorystream public/global

Where? Inside the project
 
Hi Rasmus,

I have made a complete sample from your problem.

Including everything I think you did ask the last two days (and from the
second 2 days in advance :-)) ).

I hope it helps?
Cor

\\\It needs a new windowsproject with 4 buttons and a picturebox.
Private abyt() As Byte
Private fo As New OpenFileDialog
Private sf As New SaveFileDialog
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Reading a picture and put it in a bytearray
If fo.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(fo.FileName, _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
abyt = br.ReadBytes(CInt(fs.Length))
br.Close()
'just to show the sample without a fileread error
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button2.Click
'writing a picture from a bytearray
If sf.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(sf.FileName, _
IO.FileMode.CreateNew)
Dim bw As New IO.BinaryWriter(fs)
bw.Write(abyt)
bw.Close()
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button3.Click
'writing a bytearray to a dataset
Dim ds As New DataSet
ds.Tables.Add(New DataTable("Photo"))
ds.Tables(0).Columns.Add(New DataColumn("Sample"))
ds.Tables(0).Columns(0).DataType =
System.Type.GetType("System.Byte[]")
ds.Tables(0).Rows.Add(ds.Tables(0).NewRow)
ds.Tables(0).Rows(0)(0) = abyt
Dim sf As New SaveFileDialog
If sf.ShowDialog = DialogResult.OK Then
ds.WriteXml(sf.FileName, XmlWriteMode.WriteSchema)
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button4.Click
'reading a picture from a dataset
Dim ds As New DataSet
If fo.ShowDialog = DialogResult.OK Then
ds.ReadXml(fo.FileName)
End If
abyt = CType(ds.Tables(0).Rows(0)(0), Byte())
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End Sub
///
 
Hi Herfried,

Yesterdayevening I thought about what you did say about the reading of the
picture direct to a byte arrea what should be much easier. And I thought
that is something I do wrong because I am used to it now. So I thought I
make a new sample.

You could not give me a sample then (I saw you did it Yesterdayevening late
in the message bellow for someone else).

I have taken your snippet in my sample. Thanks for that, did save me some
time. Although when I ask it I get nothing :-((

Another question for you (more to attent you on)

Why you do it in the sample like this
myobject as object = new object
and not direct
myobject as new object?

Cor
 
Rasmus,
In addition to Cor's example, you do not need to BinaryReader per se, you
can use methods on Stream directly.

Something like:

Dim input As Stream ' from OpenfileDialog.Openfile
Dim count As Integer = CInt(input.Length)
Dim buffer(count - 1) As Byte
input.Read(buffer, 0, count)
input.Close()

Dim output As New MemoryStream(buffer)

My concern with either method is reading a file that is substantial (MB or
GB) in size. As you will be placing a lot of pressure on the GC.

Hope this helps
Jay

Rasmus Teglgaard said:
Hi,

I need to know how to store the OpenfileDialog.OpenFile() in a
memorystream. Do I need to do some kind of conversion before this works?
I can't write:

dim mem as new memorystream()
mem = OpenfileDialog.Openfile()

It says not supported cast or something like that.

So, how to do this?

Also, how do I make a memorystream public/global?

Thank you VERY much in advance - this will really help me out, it's the
last thing to do before I'm done with my project.
 
Hi Jay B.

That was the part I copied from Herfried's
Without even examining it.

:-))

Althought I think there is nothing wrong with it.
When I look to it, I asume it must give the same intermidiate code as yours
(without to check it).

Cor
 
Hi Jay

Thanks for your reply. It won't be a problem with size, since the pictures is jpg thumbnails of no more than 10kb in size

Thanks
Rasmus
 
Hi Cor

Thanks for all of your help. This last one really straightened things out

Regards
Rasmus
 
Cor,

* "Cor said:
Yesterdayevening I thought about what you did say about the reading of the
picture direct to a byte arrea what should be much easier. And I thought
that is something I do wrong because I am used to it now. So I thought I
make a new sample.

You could not give me a sample then (I saw you did it Yesterdayevening late
in the message bellow for someone else).

I have taken your snippet in my sample. Thanks for that, did save me some
time. Although when I ask it I get nothing :-((

I feel sorry for that. Maybe I didn't understand the text or I didn't
find the link for some reason. I wil have to defragment my brain soon.
Another question for you (more to attent you on)

Why you do it in the sample like this
myobject as object = new object
and not direct
myobject as new object?

Because VB.NET doesn't complain about it.
 
* "Cor said:
I have made a complete sample from your problem.

Including everything I think you did ask the last two days (and from the
second 2 days in advance :-)) ).

Nice sample, Cor!
 
Cor,
The net effect is the same.
Althought I think there is nothing wrong with it.
There is nothing wrong with your example.

Hope this helps
Jay
 
Back
Top