David
David

Reputation: 235

VB.net GDI rotateflip happening every frame

I've set up a function to return an potentially transformed (X flipped) image to a drawing class, but I am finding the image flipped every frame. The code i'm using is:

Public Function getImage() As Image
    Dim returnedImage As Image

    Select Case pictureCounter
        Case 1
            returnedImage = pic1
        Case 2
            returnedImage = pic2
        Case 3
            returnedImage = pic3
        Case 4
            returnedImage = pic4
        Case 5
            returnedImage = pic5
        Case 6
            returnedImage = pic6
        Case 7
            returnedImage = pic7
        Case 8
            returnedImage = pic8
        Case 9
            returnedImage = pic9
        Case Else
            returnedImage = pic1
    End Select

    Call returnedImage.RotateFlip(transform)
    Return returnedImage
    returnedImage.Dispose()
End Function

If transform = RotateFlipType.RotateNoneFlipX then it looks like the image is having a fit. I figure its flipping X, then back again each time its called, but I don't understand how - since the transform is not being applied to the source images. Can anyone explain why this would be happening?

Thanks in advance

Upvotes: 0

Views: 220

Answers (1)

Hans Passant
Hans Passant

Reputation: 942050

since the transform is not being applied to the source images

That's the core mistake, it is applied to the source image. The Image.FlipRotate() method doesn't return a new image. You got confused by not seeing the image change in the PictureBox. That's because you didn't tell the PictureBox control that you modified the image, it can't figure it out by itself because the Image class doesn't have any kind of "Changed" event. You'd have to call the PictureBox.Invalidate() method to tell it yourself.

Otherwise simple to see by minimizing and restoring the window after you called this method once. That forces the controls to repaint themselves, you'll now see the flipped image.

If you don't want the original image modified then you'll have to make a copy of the image:

Public Function getImage() As Image
    Dim returnedImage As Image

    Select Case pictureCounter
       '' etc...
    End Select

    Dim copy = new Bitmap(returnedImage)
    copy.RotateFlip(transform)
    Return copy
End Function

Paying attention to the Dispose() method gets to very critical now, these copies gobble up memory in a hurry when the bitmaps are large. Do be aware that you got that pretty wrong in your snippet.

Upvotes: 2

Related Questions