反转图像,VB.NET AForge.NET



我正在做一个OMR项目,我必须使用 VB.NET AForge.NET 反转图像。我正在使用此代码 -

    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

它说没有错误。但是当我运行这个时,我收到一个错误,说-

类型的未处理异常 "AForge.Imaging.UnsupportedImageFormatException" 发生在 影像学.dll

.看截图

问题出在像素格式上。我正在发布工作代码,希望有一天它可以帮助其他人-

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

最新更新