Home All Groups Group Topic Archive Search About

Please help!?! OutOfMemoryException

Author
30 Mar 2006 7:43 PM
juvi
Dim myGraphics As System.Drawing.Graphics
    Dim buffer As Bitmap
    Dim fr_bm As Bitmap
    Dim to_bm As Bitmap


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
        pbxclock.Refresh()
        pbxclock.Image.Dispose()

        If Not (buffer Is Nothing) Then
            buffer.Dispose()
        End If
        If Not (fr_bm Is Nothing) Then
            fr_bm.Dispose()
        End If
        If Not (to_bm Is Nothing) Then
            to_bm.Dispose()
        End If

        ' Get the Bitmaps and a Graphics object
        ' for the destination Bitmap.

        fr_bm = New Bitmap(ImageList1.Images(selectedtheme))
        to_bm = New Bitmap(pbxclock.Image)

        Dim gr As Graphics = Graphics.FromImage(to_bm)


        ' Get source and destination rectangles.
        Dim fr_rect As New Rectangle(0, 20, 240, 240)
        Dim to_rect As New Rectangle(0, -15, 240, 320)

        ' Draw from the source to the destination.
        gr.DrawImage(fr_bm, to_rect, fr_rect, GraphicsUnit.Pixel)
        gr.DrawRectangle(New Pen(Color.Transparent), to_rect)


        ' Display the results.
        pbxclock.Image = to_bm


        buffer = New Bitmap(pbxclock.Image)

        'Dim buffer As New Bitmap(pbxclock.Image)

        pbxclock.Image = buffer

        Dim cx, cy, rad As Integer
        cx = 120 : cy = 120 : rad = 80


        myGraphics = Graphics.FromImage(pbxClock.Image)

        'myGraphics.FillEllipse(Brushes.White, cx - rad, cy - rad, rad * 2,
rad * 2)
        'myGraphics.DrawEllipse(Pens.Yellow, cx - rad, cy - rad, rad * 2,
rad * 2)
        DrawHand(myGraphics, New Pen(Color.Red), cx, cy, Hour(Now) * 5, rad
* 0.6)
        'DrawHand(myGraphics, New Pen(Color.Red), cx - 1, cy - 1, Hour(Now)
* 5, rad * 0.6)
        DrawHand(myGraphics, New Pen(Color.Green), cx, cy, Minute(Now), rad
* 0.9)
        'DrawHand(myGraphics, New Pen(Color.Green), cx - 1, cy - 1,
Minute(Now), rad * 0.9)
        DrawHand(myGraphics, New Pen(Color.Blue), cx, cy, Second(Now), rad)
        Label3.Text = "Time: " & Now.ToLongTimeString
        Label4.Text = "Date: " & Now.ToShortDateString
        'DrawHand(myGraphics, New Pen(Color.Blue), cx - 1, cy - 1,
Second(Now), rad)
        myGraphics.Dispose()
    End Sub

    Sub DrawHand(ByVal myGraphics As Graphics, ByVal myPen As Pen, ByVal cx
As Integer, ByVal cy As Integer, ByVal num As Integer, ByVal rad As Integer)

        Dim x, y As Integer

        x = rad * Math.Sin((num * Math.PI) / 30)
        y = rad * Math.Cos((num * Math.PI) / 30)
        myGraphics.DrawLine(myPen, cx, cy, cx + x, cy - y)

    End SubHi...I am new to VB.NET programming. Please help me to find my
mistakes in my code, because I do not find them: thanky you...juvi

Author
31 Mar 2006 12:09 AM
Dennis
I would start by putting a break point at each line and check that the
calculated values are within reason.  Like the -15 for the rectangle...is
this reasonable?
--
Dennis in Houston


Show quoteHide quote
"juvi" wrote:

>     Dim myGraphics As System.Drawing.Graphics
>     Dim buffer As Bitmap
>     Dim fr_bm As Bitmap
>     Dim to_bm As Bitmap
>
>
>     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Timer1.Tick
>         pbxclock.Refresh()
>         pbxclock.Image.Dispose()
>
>         If Not (buffer Is Nothing) Then
>             buffer.Dispose()
>         End If
>         If Not (fr_bm Is Nothing) Then
>             fr_bm.Dispose()
>         End If
>         If Not (to_bm Is Nothing) Then
>             to_bm.Dispose()
>         End If
>
>         ' Get the Bitmaps and a Graphics object
>         ' for the destination Bitmap.
>
>         fr_bm = New Bitmap(ImageList1.Images(selectedtheme))
>         to_bm = New Bitmap(pbxclock.Image)
>
>         Dim gr As Graphics = Graphics.FromImage(to_bm)
>
>
>         ' Get source and destination rectangles.
>         Dim fr_rect As New Rectangle(0, 20, 240, 240)
>         Dim to_rect As New Rectangle(0, -15, 240, 320)
>
>         ' Draw from the source to the destination.
>         gr.DrawImage(fr_bm, to_rect, fr_rect, GraphicsUnit.Pixel)
>         gr.DrawRectangle(New Pen(Color.Transparent), to_rect)
>
>
>         ' Display the results.
>         pbxclock.Image = to_bm
>
>
>         buffer = New Bitmap(pbxclock.Image)
>
>         'Dim buffer As New Bitmap(pbxclock.Image)
>
>         pbxclock.Image = buffer
>       
>         Dim cx, cy, rad As Integer
>         cx = 120 : cy = 120 : rad = 80
>
>
>         myGraphics = Graphics.FromImage(pbxClock.Image)
>
>         'myGraphics.FillEllipse(Brushes.White, cx - rad, cy - rad, rad * 2,
> rad * 2)
>         'myGraphics.DrawEllipse(Pens.Yellow, cx - rad, cy - rad, rad * 2,
> rad * 2)
>         DrawHand(myGraphics, New Pen(Color.Red), cx, cy, Hour(Now) * 5, rad
> * 0.6)
>         'DrawHand(myGraphics, New Pen(Color.Red), cx - 1, cy - 1, Hour(Now)
> * 5, rad * 0.6)
>         DrawHand(myGraphics, New Pen(Color.Green), cx, cy, Minute(Now), rad
> * 0.9)
>         'DrawHand(myGraphics, New Pen(Color.Green), cx - 1, cy - 1,
> Minute(Now), rad * 0.9)
>         DrawHand(myGraphics, New Pen(Color.Blue), cx, cy, Second(Now), rad)
>         Label3.Text = "Time: " & Now.ToLongTimeString
>         Label4.Text = "Date: " & Now.ToShortDateString
>         'DrawHand(myGraphics, New Pen(Color.Blue), cx - 1, cy - 1,
> Second(Now), rad)
>         myGraphics.Dispose()
>     End Sub
>
>     Sub DrawHand(ByVal myGraphics As Graphics, ByVal myPen As Pen, ByVal cx
> As Integer, ByVal cy As Integer, ByVal num As Integer, ByVal rad As Integer)
>
>         Dim x, y As Integer
>
>         x = rad * Math.Sin((num * Math.PI) / 30)
>         y = rad * Math.Cos((num * Math.PI) / 30)
>         myGraphics.DrawLine(myPen, cx, cy, cx + x, cy - y)
>
>     End SubHi...I am new to VB.NET programming. Please help me to find my
> mistakes in my code, because I do not find them: thanky you...juvi
>
>
Author
31 Mar 2006 4:06 PM
Jim Wooley
> I would start by putting a break point at each line and check that the
> calculated values are within reason.  Like the -15 for the
> rectangle...is this reasonable?

Typically, OutOfMemoryExceptions arise from recursive calls that are not
exited properly. I would recommend checking out your CallStack and seeing
if there is any looping going on that you didn't expect. You may want to
enable the non-user code in the call stack as well because it can be something
happening in the framework you weren't expecting.

Jim Wooley
http://devauthority.com/blogs/jwooley