Stream from Image?

G

Guest

Hi, ive been working at this for a week and cant seem to get it. I want to
get a stream from an Image file so i can send it in a socket. Every attempt
i get an error saying "Buffer can not be null"....here the code, any help
would be awsome, thanks

Private Sub btnsendfile_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnsendfile.Click

Try
With Me.OpenFileDialog1

'' .Filter = "JPEG (*.jpeg)|*.jpeg"
.FilterIndex = 0
.Multiselect = False

If .ShowDialog = DialogResult.OK And .FileName <> Nothing Then

IO.File.OpenRead(.FileName)

Dim fs As New IO.FileStream(.FileName, IO.FileMode.Open,
IO.FileAccess.Read)
Dim sr As New IO.StreamReader(fs)
Dim bf As New IO.BufferedStream(fs)

Dim line As String
Dim bytes As Char()
Dim sendbytes As Byte()


If conntype Then
Dim tempsend As Byte() = ASCII.GetBytes("[Image]")
ClientSocket.BeginSend(tempsend, 0, tempsend.Length,
SocketFlags.None, AddressOf ReceiveCallBack, Nothing)

''Heres a way i tried using a streamreader

'While sr.Peek > -1
' sr.ReadBlock(bytes, 0, 1400)
' bytes.CopyTo(sendbytes, 0)
' ClientSocket.BeginSend(sendbytes, 0,
sendbytes.Length, SocketFlags.None, AddressOf ReceiveCallBack, Nothing)
'End While

''Heres how i tried it using a binary reader

'While bf.Read(sendbytes, 0, 1400) <> -1
' ClientSocket.BeginSend(sendbytes, 0,
sendbytes.Length, SocketFlags.None, AddressOf ReceiveCallBack, Nothing)
'End While

Else
Dim tempsend As Byte() = ASCII.GetBytes("[Image]")
ConnectedListen.BeginSend(tempsend, 0,
tempsend.Length, SocketFlags.None, AddressOf ListenReceiveCallBack, Nothing)

While sr.Peek > -1
sr.ReadBlock(bytes, 0, 1400)
bytes.CopyTo(sendbytes, 0)
ConnectedListen.BeginSend(sendbytes, 0,
sendbytes.Length, SocketFlags.None, AddressOf ListenReceiveCallBack, Nothing)
End While

End If

If sr.Peek <= -1 Then
If conntype Then
Dim tempsend As Byte() =
ASCII.GetBytes("[/Image]")
ClientSocket.BeginSend(tempsend, 0,
tempsend.Length, SocketFlags.None, AddressOf ReceiveCallBack, Nothing)
Else
Dim tempsend As Byte() =
ASCII.GetBytes("[/Image]")
ConnectedListen.BeginSend(tempsend, 0,
tempsend.Length, SocketFlags.None, AddressOf ListenReceiveCallBack, Nothing)
End If

End If

End If

End With

Catch ex As Exception
Me.rtbinfo.Text += ex.Message & vbNewLine
End Try

End Sub

Conntype is a boolean value on if the user connected to someone else, or
someone else connected to the user, so i know which socket to send to
 
B

Bob Powell [MVP]

Save the image into a memory stream, rewind the stream and pass it to the
socket to write out as a file...

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





iwdu15 said:
Hi, ive been working at this for a week and cant seem to get it. I want to
get a stream from an Image file so i can send it in a socket. Every
attempt
i get an error saying "Buffer can not be null"....here the code, any help
would be awsome, thanks

Private Sub btnsendfile_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnsendfile.Click

Try
With Me.OpenFileDialog1

'' .Filter = "JPEG (*.jpeg)|*.jpeg"
.FilterIndex = 0
.Multiselect = False

If .ShowDialog = DialogResult.OK And .FileName <> Nothing
Then

IO.File.OpenRead(.FileName)

Dim fs As New IO.FileStream(.FileName,
IO.FileMode.Open,
IO.FileAccess.Read)
Dim sr As New IO.StreamReader(fs)
Dim bf As New IO.BufferedStream(fs)

Dim line As String
Dim bytes As Char()
Dim sendbytes As Byte()


If conntype Then
Dim tempsend As Byte() = ASCII.GetBytes("[Image]")
ClientSocket.BeginSend(tempsend, 0,
tempsend.Length,
SocketFlags.None, AddressOf ReceiveCallBack, Nothing)

''Heres a way i tried using a streamreader

'While sr.Peek > -1
' sr.ReadBlock(bytes, 0, 1400)
' bytes.CopyTo(sendbytes, 0)
' ClientSocket.BeginSend(sendbytes, 0,
sendbytes.Length, SocketFlags.None, AddressOf ReceiveCallBack, Nothing)
'End While

''Heres how i tried it using a binary reader

'While bf.Read(sendbytes, 0, 1400) <> -1
' ClientSocket.BeginSend(sendbytes, 0,
sendbytes.Length, SocketFlags.None, AddressOf ReceiveCallBack, Nothing)
'End While

Else
Dim tempsend As Byte() = ASCII.GetBytes("[Image]")
ConnectedListen.BeginSend(tempsend, 0,
tempsend.Length, SocketFlags.None, AddressOf ListenReceiveCallBack,
Nothing)

While sr.Peek > -1
sr.ReadBlock(bytes, 0, 1400)
bytes.CopyTo(sendbytes, 0)
ConnectedListen.BeginSend(sendbytes, 0,
sendbytes.Length, SocketFlags.None, AddressOf ListenReceiveCallBack,
Nothing)
End While

End If

If sr.Peek <= -1 Then
If conntype Then
Dim tempsend As Byte() =
ASCII.GetBytes("[/Image]")
ClientSocket.BeginSend(tempsend, 0,
tempsend.Length, SocketFlags.None, AddressOf ReceiveCallBack, Nothing)
Else
Dim tempsend As Byte() =
ASCII.GetBytes("[/Image]")
ConnectedListen.BeginSend(tempsend, 0,
tempsend.Length, SocketFlags.None, AddressOf ListenReceiveCallBack,
Nothing)
End If

End If

End If

End With

Catch ex As Exception
Me.rtbinfo.Text += ex.Message & vbNewLine
End Try

End Sub

Conntype is a boolean value on if the user connected to someone else, or
someone else connected to the user, so i know which socket to send to
 
G

Guest

how can i declare which file the memory stream goes to? i tried the msdn help
but that was no help at all. and after i do that, how do i rewind the stream?
thanks for your help

--iwdu150
 
C

Carlos J. Quintero [VB MVP]

Since you are dealing with images, ensure that you are using stream readers
that use bytes, and not chars or strings or encodings. So, something like
StreamReader or ASCII will likely fail. I suggest you to use a BinaryReader
and the ReadBytes(count) function.

Also it seems that your problem is getting the bytes of the binary file, so
remove the sockets noise from the problem in your post(s). Once you can get
the array of bytes, to send them through a socket should not be a problem.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

iwdu15 said:
Hi, ive been working at this for a week and cant seem to get it. I want to
get a stream from an Image file so i can send it in a socket. Every
attempt
i get an error saying "Buffer can not be null"....here the code, any help
would be awsome, thanks

Private Sub btnsendfile_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnsendfile.Click

Try
With Me.OpenFileDialog1

'' .Filter = "JPEG (*.jpeg)|*.jpeg"
.FilterIndex = 0
.Multiselect = False

If .ShowDialog = DialogResult.OK And .FileName <> Nothing
Then

IO.File.OpenRead(.FileName)

Dim fs As New IO.FileStream(.FileName,
IO.FileMode.Open,
IO.FileAccess.Read)
Dim sr As New IO.StreamReader(fs)
Dim bf As New IO.BufferedStream(fs)

Dim line As String
Dim bytes As Char()
Dim sendbytes As Byte()


If conntype Then
Dim tempsend As Byte() = ASCII.GetBytes("[Image]")
ClientSocket.BeginSend(tempsend, 0,
tempsend.Length,
SocketFlags.None, AddressOf ReceiveCallBack, Nothing)

''Heres a way i tried using a streamreader

'While sr.Peek > -1
' sr.ReadBlock(bytes, 0, 1400)
' bytes.CopyTo(sendbytes, 0)
' ClientSocket.BeginSend(sendbytes, 0,
sendbytes.Length, SocketFlags.None, AddressOf ReceiveCallBack, Nothing)
'End While

''Heres how i tried it using a binary reader

'While bf.Read(sendbytes, 0, 1400) <> -1
' ClientSocket.BeginSend(sendbytes, 0,
sendbytes.Length, SocketFlags.None, AddressOf ReceiveCallBack, Nothing)
'End While

Else
Dim tempsend As Byte() = ASCII.GetBytes("[Image]")
ConnectedListen.BeginSend(tempsend, 0,
tempsend.Length, SocketFlags.None, AddressOf ListenReceiveCallBack,
Nothing)

While sr.Peek > -1
sr.ReadBlock(bytes, 0, 1400)
bytes.CopyTo(sendbytes, 0)
ConnectedListen.BeginSend(sendbytes, 0,
sendbytes.Length, SocketFlags.None, AddressOf ListenReceiveCallBack,
Nothing)
End While

End If

If sr.Peek <= -1 Then
If conntype Then
Dim tempsend As Byte() =
ASCII.GetBytes("[/Image]")
ClientSocket.BeginSend(tempsend, 0,
tempsend.Length, SocketFlags.None, AddressOf ReceiveCallBack, Nothing)
Else
Dim tempsend As Byte() =
ASCII.GetBytes("[/Image]")
ConnectedListen.BeginSend(tempsend, 0,
tempsend.Length, SocketFlags.None, AddressOf ListenReceiveCallBack,
Nothing)
End If

End If

End If

End With

Catch ex As Exception
Me.rtbinfo.Text += ex.Message & vbNewLine
End Try

End Sub

Conntype is a boolean value on if the user connected to someone else, or
someone else connected to the user, so i know which socket to send to
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

NNTP Client 4
TCP Transfer issues 3
Need Help with this Async code 1
multiple tcp connections 1
How to debug this? 10
Async socket connection ends up in Unable to read data from.... 5
send file 1
Data Logger 1

Top