Invert image faster in C#

后端 未结 3 1659
醉酒成梦
醉酒成梦 2020-12-19 19:57

I\'m using WinForms. I have a picture-box in my form. When I open a picture in the picture-box I am able to invert the colors back and forth on a click of a button, but my c

3条回答
  •  青春惊慌失措
    2020-12-19 20:23

    In case someone need the similar code in VB.NET.

    Also note the variable inv.A instead of the value 255. In case your picturebox has transparency you need this.

        Public Function InvertImageColors(ByVal p As Image) As Image
            Dim pic As New Bitmap(p)
            For y As Integer = 0 To pic.Height - 1
                For x As Integer = 0 To pic.Width - 1
                    Dim inv As Color = pic.GetPixel(x, y)
                    inv = Color.FromArgb(inv.A, 255 - inv.R, 255 - inv.G, 255 - inv.B)
                    pic.SetPixel(x, y, inv)
                Next x
            Next y
            Return pic
        End Function
    

    Usage:

        pic.image = InvertImageColors(pic.image)
    

提交回复
热议问题