vb.net - Display image in picturebox at runtime - Stack Overflow
I am trying to display images in a picturebox at runtime. The images are received continuously over UDP from another host. However non of the images are getting displayed. I have tried saving the images on the disk and found that they get saved properly. The following is my code
Sub ReceiveImageContinuously()
Dim udpClient As New UdpClient(11000)
Console.WriteLine($"Listening for UDP packets on port {11000}...")
Try
While True
Dim receivedBytes As New List(Of Byte)()
While True
' Receive a packet
Dim remoteEndPoint As New IPEndPoint(IPAddress.Any, 0)
Dim receivedChunk As Byte() = udpClient.Receive(remoteEndPoint)
Dim receivedMessage As String = System.Text.Encoding.UTF8.GetString(receivedChunk)
' Check for termination signal
If receivedMessage = "END" Then
Console.WriteLine("Received image file termination signal.")
Exit While
End If
' Add received chunk to the list
receivedBytes.AddRange(receivedChunk)
Console.WriteLine($"Received chunk of size {receivedChunk.Length}")
End While
' Combine all chunks into a single byte array
Dim completeImageBytes As Byte() = receivedBytes.ToArray()
LoadPictureBox(completeImageBytes)
End While
Catch ex As Exception
Console.WriteLine($"Error: {ex.Message}")
Finally
udpClient.Close()
End Try
End Sub
Public Sub LoadPictureBox(receivedBytes As Byte())
Try
' Validate the received bytes
If receivedBytes Is Nothing OrElse receivedBytes.Length = 0 Then
Console.WriteLine("Received an empty byte array.")
Return
End If
' Use MemoryStream to create an Image from byte array
Using ims As New MemoryStream(receivedBytes)
' Attempt to load the image
Dim img As Image = Image.FromStream(ims)
' Check if Invoke is required
If PictureBox1.InvokeRequired Then
PictureBox1.Invoke(Sub()
PictureBox1.Image = img
PictureBox1.Refresh()
End Sub)
Else
PictureBox1.Image = img
PictureBox1.Refresh()
End If
End Using
Catch ex As Exception
' Log or display error message for debugging
Console.WriteLine($"Error loading image: {ex.Message}")
MessageBox.Show($"Error loading image: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
I have tried to save the images on the disk as follows and I found them getting saved correctly.
Dim savePath As String = Path.Combine(saveFolderPath, $"received_image_{cycle}.jpg")
File.WriteAllBytes(savePath , completeImageBytes)
最新文章
- 微软苹果和谷歌引导:硬件触控化或是大趋势
- 京东1.7亿美元投资金蝶原因:布局企业ERP市场
- 美学者发现比特币重大漏洞:能导致货币体系崩溃
- Windows 8取胜机会:10个值得考虑的因素
- "双核"创新造就苹果辉煌
- excel - When loading my userform i get Run-Time Error 381: Could not set the list property. Invalid property array index - Stack
- R CatBoost support for incremental training - Stack Overflow
- c# - TrackableBehaviour.Status type is missing when using Vuforia - Stack Overflow
- tracking - Add custom hand gestures in three.js - Stack Overflow
- mvvm - How to Call ViewModelProvider without ref in Flutter - Stack Overflow
- swiftui - Cannot access generic subscript - Stack Overflow
- wordpress - Give access to users own submitted entries only wpforms - Stack Overflow
- html - Need help getting a button with a drop-down menucontent in it to be centered and to push content down - Stack Overflow
- imagemagick - How to add annotations in Right-To-Left (RTL) languages (like Arabic and Persian) to images using R's magi
- postgresql - AzureStorageClient raises the error Unable to determine account name for shared key credential - Stack Overflow
- c++ - Member of struct constructed twice in custom constructor? - Stack Overflow
- java - YubiKey PIV AuthenticationDecryption returns 0x6A80 error - Stack Overflow