Click here to Skip to main content
15,888,143 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)

VB
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
    ' Add work item to thread pool. Starting listener..
    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
    ' Don't forget to stop listener
    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
        ' Create Listener using port 50000
        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
        ' exception handle here ..
    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)

VB
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) ' Connect to port 50000
        ' Exception will be automatically generated when connect fail
        Dim stream As New System.IO.MemoryStream
        image.Save(stream, image.RawFormat) ' save image data to memory stream
        Dim buffer As Byte() = stream.ToArray() ' buffer contains image data
        Dim bytesRemain As Integer = buffer.Length ' number of unsent bytes
        Dim offset As Integer = 0 ' buffer send offset

        ' Perform loop until all image data sent successfully
        While (bytesRemain > 0)
            Dim [error] As SocketError ' contains socket error code
            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
    ' when we leave "using" statement, the socket will be automatically closed
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!
Posted

1 solution

replace
-----------------------------------------------------------
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
---------------------------------------------------------------
with

Public Sub SaveImageCopy(filename As String, image As Image)
Dim path As String = System.IO.Path.Combine(My.Application.Info.DirectoryPath, filename & ".jpg")
Dim im as Image = Image.fromFile(path)
im.save(path)
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900