Out of Memory When Rotating Bitmap Iamge

  • Thread starter Thread starter SKBone
  • Start date Start date
S

SKBone

Guest
Public Class Form1

Private normalImage As Image = New Bitmap(Image.FromFile("T:\..."))

Private Sub rotateDialBttn_Click(sender As Object, e As EventArgs) Handles rotateDialBttn.Click

Dim newImage As Image = Me.RotateImage(normalImage, Convert.ToInt32(degreeTxt.Text))
imgBox.Image = newImage

End Sub

Private Function RotateImage(ByVal image As Image, ByVal angle As Single) As Bitmap

If image Is Nothing Then
Throw New ArgumentNullException("image")
End If

Dim upperLeftDrawPoint As Point = New Point(0, 0)
Dim imageCenterOffset As Point = New Point(image.Width / 2, image.Height / 2)

'create new empty bitmap to hold rotate image
Dim rotatedBmp As Bitmap = New Bitmap(image.Width, image.Height)
rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution)

'make graphics object from the empty bitmap
Dim g As Graphics = Graphics.FromImage(rotatedBmp)

'put rotation point in center of image
g.TranslateTransform(upperLeftDrawPoint.X + imageCenterOffset.X, upperLeftDrawPoint.Y + imageCenterOffset.Y)

'rotate image
g.RotateTransform(angle)

'translate back to original positioning
g.TranslateTransform((upperLeftDrawPoint.X + imageCenterOffset.X) * -1, (upperLeftDrawPoint.Y + imageCenterOffset.Y) * -1)

'draw passed in image onto graphics object
g.DrawImage(image, upperLeftDrawPoint)
g.ResetTransform()

Return rotatedBmp

End Function
End Class
I am having an issue understanding exactly how I should accomplish a specific task using Bitmaps and images. I have a picturebox that has an image of a dial in my form as well as a textbox and button. Basically as it is now the button rotates the image in the picture box based on the degrees inside the textbox. All of the rotating seems to be working correctly but the issue comes in when I hit the rotate button enough times I get an out of memory issue. How do I get around this problem. I've been looking and reading up on the Dispose() method but wherever I have tried to use it does not help. If anyone can help me or explain to me how to use the Dispose method correctly it would be greatly appreciated.

Continue reading...
 
Back
Top