I dont have idea on how to save the image on the picturebox that sent from client using TCP. the sending picture works fine but when it comes to save the image that sent from the client is my problem because i dont have idea on how to make this.
here is my
CLIENT (SENDING PICTURE)
Imports System.Net.Sockets
Imports System.Threading
Imports System.IO
Imports System.Drawing
Public Class Form1
Private m_listener As TcpListener
Private m_listenerStop As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ThreadPool.QueueUserWorkItem(AddressOf ImageListener)
lblpath.Text = PictureBox1.ImageLocation
Timer1.Interval = 1000
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
m_listenerStop = True
m_listener.Stop()
End Sub
Private Sub ImageReceived(ByVal image As Object)
PictureBox1.Image = DirectCast(image, Image)
End Sub
Private Sub ImageListener(ByVal state As Object)
Try
m_listener = New TcpListener(IPAddress.Any, 50000)
m_listener.Start()
Do
Me.Invoke(New ParameterizedThreadStart(AddressOf ImageReceived), _
Image.FromStream(m_listener.AcceptTcpClient.GetStream()))
Loop While Not m_listenerStop
Catch ex As Exception
End Try
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
<big>I WANT TO SAVE HERE IF ANY IMAGE EXISTS IN PICTURE BOX </big>
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strfilename As String
Dim folder As String
strfilename = PictureBox1.Text
folder = FolderBrowserDialog1.SelectedPath
Call PictureBox1.Image.Save(folder & "\" & strfilename, System.Drawing.Imaging.ImageFormat.Jpeg)
End Sub
Public Sub SaveImageCopy(filename As String, image As Image)
Dim path As String = System.IO.Path.Combine(My.Application.Info.DirectoryPath, filename & ".jpg")
Dim dest As New Bitmap(image.Width, image.Height)
Dim gfx As Graphics = Graphics.FromImage(dest)
gfx.DrawImageUnscaled(image, Point.Empty)
gfx.Dispose()
dest.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg)
dest.Dispose()
End Sub
End Class
And here is my
SERVER (RECEIVING PICTURE)
Public Shared Sub SendImage(ByVal image As Image, ByVal host As String)
Using sock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
sock.Connect(host, 50000)
Dim stream As New System.IO.MemoryStream
image.Save(stream, image.RawFormat)
Dim buffer As Byte() = stream.ToArray()
Dim bytesRemain As Integer = buffer.Length
Dim offset As Integer = 0
While (bytesRemain > 0)
Dim [error] As SocketError
Dim bytesSent As Integer = sock.Send(buffer, offset, bytesRemain, SocketFlags.None, [error])
If [error] <> SocketError.Success Then
Throw New InvalidOperationException("Send image fail")
End If
offset += bytesSent
bytesRemain -= bytesSent
End While
End Using
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SendImage(Image.FromFile(labelpath.Text), "SheerwoodJohn")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
OpenFileDialog1.Filter = "jpeg|*.jpg|bmp|*.*.png|png|*.*"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.PictureBox1.Image = System.Drawing.Image.FromFile(OpenFileDialog1.FileName)
PictureBox1.Image = System.Drawing.Image.FromFile(OpenFileDialog1.FileName)
Label1.Text = OpenFileDialog1.FileName
End If
End Sub
if anyone sir/maam know on how to save image automaically using timer or anything tool that can save pls tell me. :) thank you!