Display an image that is being written?

bpeirson

Member
Joined
Dec 21, 2010
Messages
7
Location
United States
Programming Experience
1-3
I have a production line running a custom HMI application that I wrote in VB.NET using the InGear.NET driver for Allen-Bradley to communicate with my PLC. The assembly line has several cameras for vision inspection to make sure parts are assembled in the proper orientation. The cameras are capable of saving a BMP of the inspection to a server, in this case a virtual server that handles some SQL data logging for the rest of the line. With the cameras set to only store a history of 1 image they continually overwrite the same file, Image0.bmp

In an effort to give the operators some feedback as to why the camera is failing a part I want to display the image after the camera saves it. I'm using a file system watcher acting on the change event to do this.

Right now the running program throws a GDI+ exception, with not a lot more detail than that. The current version doesn't have any error handling....

So I put the code in a try statement so that the program wouldn't just hang on the operator. The GDI+ error was occurring because the file was in use. I added a function to try to open the file first and then display it only if the file open was false. That is the code below, and that throws me the same error.

I've tried just about everything I can think of, looking for any suggestions to actually get this image to display reliably, without running into an error (handled or not).

VB.NET:
    Friend Function CheckForOpenFile(ByVal Path As String)

        Try

            File.Open(Path, FileMode.Open, FileAccess.Read, FileShare.None)

            FileClose(1)

            Return False

        Catch ex As Exception

            Return True

        End Try

    End Function

    Friend Sub DisplayImage()

        ImageOpen = CheckForOpenFile(CameraFile)


        Try

            CameraFile_Buffer = File.ReadAllBytes(CameraFile)

            CameraFile_Stream = New MemoryStream(CameraFile_Buffer)

            CameraImage = CType(Bitmap.FromStream(CameraFile_Stream), Bitmap)

            CameraFile_Stream.Dispose()

            frmMain.PictureBox1.Image = CameraImage

            frmMain.PictureBox1.Visible = True

        Catch ex As Exception

        End Try
 
SORRY THIS WAS NEVER ANSWERED FOR YOU. A FILEWATCHER IS THE PATH I'D GO TO DO THAT:

VB.NET:
Imports System.IO
Imports System.Drawing

Public Class CameraImageDisplay
    Private WithEvents watcher As FileSystemWatcher
    Private imagePath As String = "\\path\to\your\server\Image0.bmp"

    Public Sub New()
        watcher = New FileSystemWatcher()
        watcher.Path = Path.GetDirectoryName(imagePath)
        watcher.Filter = Path.GetFileName(imagePath)
        watcher.NotifyFilter = NotifyFilters.LastWrite
        watcher.EnableRaisingEvents = True
    End Sub

    Private Sub OnChanged(sender As Object, e As FileSystemEventArgs) Handles watcher.Changed
        DisplayImage()
    End Sub

    Private Sub DisplayImage()
        If File.Exists(imagePath) Then
            Dim img As Image = Image.FromFile(imagePath)
            ' Assuming you have a PictureBox named pictureBox1 to display the image
            pictureBox1.Image = img
            pictureBox1.Refresh()
        End If
    End Sub
End Class
 
Back
Top