vb.netimage-processingaforgeomr

Invert image with AForge.NET in VB.NET


I am working on an OMR project where I have to Invert an Image with AForge.NET using VB.NET. I am using this code -

    Private Sub Load_Butt_Click(sender As Object, e As EventArgs) Handles Load_Butt.Click
    ' load image
    Dim image As Bitmap = AForge.Imaging.Image.FromFile("c://test.bmp")
    ' create invert filter
    Dim filter As New Invert()
    Dim inv_img As Bitmap
    ' apply the invert filter
    inv_img = filter.Apply(image)
    PictureBox1.Image = inv_img
End Sub

It says no error. But when I run this I get an error that says-

An unhandled exception of type 'AForge.Imaging.UnsupportedImageFormatException' occurred in AForge.Imaging.dll

. See Screenshot


Solution

  • The problem was with pixel format.I am posting the working code hoping it may help others someday-

    Public image As Bitmap = AForge.Imaging.Image.FromFile("c://test.bmp")
    
    Public inv_img As Bitmap
    
    Public Sub ApplyFilter(filter As IFilter)
        ' apply filter
        inv_img = filter.Apply(image)
        ' display image
        PictureBox1.Image = inv_img
    End Sub
    Public Sub Load_Butt_Click(sender As Object, e As EventArgs) Handles Load_Butt.Click
    
    
        ' check pixel format
        If (image.PixelFormat = PixelFormat.Format16bppGrayScale) OrElse (Bitmap.GetPixelFormatSize(image.PixelFormat) > 32) Then
            MessageBox.Show("The demo application supports only color images.", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            ' free image
            image.Dispose()
            image = Nothing
        Else
            ' make sure the image has 24 bpp format
            If image.PixelFormat <> PixelFormat.Format24bppRgb Then
                Dim temp As Bitmap = AForge.Imaging.Image.Clone(image, PixelFormat.Format24bppRgb)
                image.Dispose()
                image = temp
            End If
        End If
    
    
        ApplyFilter(New Invert())
    End Sub