image and notifyicon problem

J

JR

Hi,



I need to change the notifyicon in a countdown. after about 3000-5000 times
there is an error.

Here is all the code. what is wrong

please help

the code needs only an notifyicon and an imagelist the timer is for changing
the icon

Jan

'start code---------------------------------

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents ImageList1 As System.Windows.Forms.ImageList

Friend WithEvents NotifyIcon1 As System.Windows.Forms.NotifyIcon

Friend WithEvents Timer1 As System.Windows.Forms.Timer

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.components = New System.ComponentModel.Container

Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)

Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)

Me.Timer1 = New System.Windows.Forms.Timer(Me.components)

'

'ImageList1

'

Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)

Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent

'

'NotifyIcon1

'

Me.NotifyIcon1.Text = "NotifyIcon1"

Me.NotifyIcon1.Visible = True

'

'Timer1

'

Me.Timer1.Enabled = True

Me.Timer1.Interval = 5

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Name = "Form1"

Me.ShowInTaskbar = False

Me.Text = "Form1"

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

sBuild_100_Icons()

End Sub

Sub sBuild_100_Icons()

Dim rect As Rectangle = New Rectangle(0, 0, 32, 32)

Dim displayGraphics As Graphics = Me.CreateGraphics()

Dim img As Image = New Bitmap(rect.Width, rect.Height, displayGraphics)

Dim imageGraphics As Graphics = Graphics.FromImage(img)

Dim bcolor As Brush = New
SolidBrush(Color.FromKnownColor(KnownColor.Control))

Dim cColor As Color = Color.Red

Dim fnt As New Font(Me.Font.Name, 16, FontStyle.Bold)

For X As Byte = 0 To 99

imageGraphics.FillRectangle(bcolor, rect)

imageGraphics.DrawString(Format(X, "00"), fnt, New SolidBrush(cColor), 0, _

(rect.Height - imageGraphics.MeasureString(Format(X, "00"), fnt).Height) /
2)

Dim mybitmap As Bitmap = img

Dim hIcon As IntPtr = mybitmap.GetHicon

ImageList1.Images.Add(Icon.FromHandle(hIcon))

Next

End Sub

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

Static Teller As Int64

Try

Teller += 1

Catch ex As Exception

Teller = 0

End Try

Dim mybitmap As Bitmap = ImageList1.Images(Teller Mod 100) 'I have tryed
many mod values

'te next line gives an error by the 3000-5000° times

Dim hIcon As IntPtr = mybitmap.GetHicon

NotifyIcon1.Icon = Icon.FromHandle(hIcon)

Dim sw As New System.IO.StreamWriter("teller.txt", True)

sw.WriteLine(Teller)

sw.Close()

End Sub

End Class
 
B

Branco Medeiros

JR wrote:
</backposted>

It would help if you provided the actual error message.

Anyway, it *seems* to me that you're wasting resources galore when you
recreate the icon at each timer tick. I suggest you forget the
ImageList and store your icons in an array, and just assign the
appropriate icon to the NotifyIcon directly from the array. Something
like this:

<aircode>
'At form level
Private Const MAX_ICON As Integer = 99
Private mIcons(MAX_ICON) As Icon

Sub sBuild_100_Icons()

'...
'... your initializations go here
'...

Dim Brush As Brush = New SolidBrush(cColor)
Dim Bmp As Bitmap = img
For X As Byte = 0 To MAX_ICON
Dim Text As String = Format(X, "00")
Dim Height As Single = imageGraphics. _
MeasureString(Text, fnt).Height
imageGraphics.FillRectangle(bcolor, rect)
imageGraphics.DrawString( _
Text, fnt, Brush, _
0, (rect.Height - Height) / 2)

Dim hIcon As IntPtr = Bmp.GetHicon
mIcons(X) = Icon.FromHandle(hIcon)
Next
End Sub

Private Sub Timer1_Tick( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Timer1.Tick

Static Teller As Long
Try
Teller += 1
Catch Ex As Exception
Teller = 0
End Try

NotifyIcon1.Icon = mIcons(Teller Mod (MAX_ICON + 1))

'...
End Sub
</aircode>

HTH.

Regards,

Branco.
 
J

JR

Hi,

I have build an imagelist (sBuild_100_Icons) than I pick almays one
calulated by a mod calculation
Also remember that the code that I have send is a test duplicate to recreate
the error and asking for help

Jan

"Branco Medeiros" <[email protected]> schreef in bericht
JR wrote:
</backposted>

It would help if you provided the actual error message.

Anyway, it *seems* to me that you're wasting resources galore when you
recreate the icon at each timer tick. I suggest you forget the
ImageList and store your icons in an array, and just assign the
appropriate icon to the NotifyIcon directly from the array. Something
like this:

<aircode>
'At form level
Private Const MAX_ICON As Integer = 99
Private mIcons(MAX_ICON) As Icon

Sub sBuild_100_Icons()

'...
'... your initializations go here
'...

Dim Brush As Brush = New SolidBrush(cColor)
Dim Bmp As Bitmap = img
For X As Byte = 0 To MAX_ICON
Dim Text As String = Format(X, "00")
Dim Height As Single = imageGraphics. _
MeasureString(Text, fnt).Height
imageGraphics.FillRectangle(bcolor, rect)
imageGraphics.DrawString( _
Text, fnt, Brush, _
0, (rect.Height - Height) / 2)

Dim hIcon As IntPtr = Bmp.GetHicon
mIcons(X) = Icon.FromHandle(hIcon)
Next
End Sub

Private Sub Timer1_Tick( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Timer1.Tick

Static Teller As Long
Try
Teller += 1
Catch Ex As Exception
Teller = 0
End Try

NotifyIcon1.Icon = mIcons(Teller Mod (MAX_ICON + 1))

'...
End Sub
</aircode>

HTH.

Regards,

Branco.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top