image and notifyicon problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JR

    #1

    image and notifyicon problem

    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.

    InitializeCompo nent()

    'Add any initialization after the InitializeCompo nent() 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.Disp ose()

    End If

    End If

    MyBase.Dispose( disposing)

    End Sub

    'Required by the Windows Form Designer

    Private components As System.Componen tModel.IContain er

    '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.NotifyIco n

    Friend WithEvents Timer1 As System.Windows. Forms.Timer

    <System.Diagnos tics.DebuggerSt epThrough()Priv ate Sub InitializeCompo nent()

    Me.components = New System.Componen tModel.Containe r

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

    Me.NotifyIcon1 = New System.Windows. Forms.NotifyIco n(Me.components )

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

    '

    'ImageList1

    '

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

    Me.ImageList1.T ransparentColor = System.Drawing. Color.Transpare nt

    '

    'NotifyIcon1

    '

    Me.NotifyIcon1. Text = "NotifyIcon 1"

    Me.NotifyIcon1. Visible = True

    '

    'Timer1

    '

    Me.Timer1.Enabl ed = True

    Me.Timer1.Inter val = 5

    '

    'Form1

    '

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

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

    Me.Name = "Form1"

    Me.ShowInTaskba r = False

    Me.Text = "Form1"

    End Sub

    #End Region

    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load

    sBuild_100_Icon s()

    End Sub

    Sub sBuild_100_Icon s()

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

    Dim displayGraphics As Graphics = Me.CreateGraphi cs()

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

    Dim imageGraphics As Graphics = Graphics.FromIm age(img)

    Dim bcolor As Brush = New
    SolidBrush(Colo r.FromKnownColo r(KnownColor.Co ntrol))

    Dim cColor As Color = Color.Red

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

    For X As Byte = 0 To 99

    imageGraphics.F illRectangle(bc olor, rect)

    imageGraphics.D rawString(Forma t(X, "00"), fnt, New SolidBrush(cCol or), 0, _

    (rect.Height - imageGraphics.M easureString(Fo rmat(X, "00"), fnt).Height) /
    2)

    Dim mybitmap As Bitmap = img

    Dim hIcon As IntPtr = mybitmap.GetHic on

    ImageList1.Imag es.Add(Icon.Fro mHandle(hIcon))

    Next

    End Sub

    Private Sub Timer1_Tick(ByV al sender As System.Object, ByVal e As
    System.EventArg s) Handles Timer1.Tick

    Static Teller As Int64

    Try

    Teller += 1

    Catch ex As Exception

    Teller = 0

    End Try

    Dim mybitmap As Bitmap = ImageList1.Imag es(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.GetHic on

    NotifyIcon1.Ico n = Icon.FromHandle (hIcon)

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

    sw.WriteLine(Te ller)

    sw.Close()

    End Sub

    End Class


  • Branco Medeiros

    #2
    Re: image and notifyicon problem

    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_Icon s()

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

    Dim Brush As Brush = New SolidBrush(cCol or)
    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(T ext, fnt).Height
    imageGraphics.F illRectangle(bc olor, rect)
    imageGraphics.D rawString( _
    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.EventArg s _
    ) Handles Timer1.Tick

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

    NotifyIcon1.Ico n = mIcons(Teller Mod (MAX_ICON + 1))

    '...
    End Sub
    </aircode>

    HTH.

    Regards,

    Branco.

    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.
    >
    InitializeCompo nent()
    >
    'Add any initialization after the InitializeCompo nent() 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.Disp ose()
    >
    End If
    >
    End If
    >
    MyBase.Dispose( disposing)
    >
    End Sub
    >
    'Required by the Windows Form Designer
    >
    Private components As System.Componen tModel.IContain er
    >
    '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.NotifyIco n
    >
    Friend WithEvents Timer1 As System.Windows. Forms.Timer
    >
    <System.Diagnos tics.DebuggerSt epThrough()Priv ate Sub InitializeCompo nent()
    >
    Me.components = New System.Componen tModel.Containe r
    >
    Me.ImageList1 = New System.Windows. Forms.ImageList (Me.components)
    >
    Me.NotifyIcon1 = New System.Windows. Forms.NotifyIco n(Me.components )
    >
    Me.Timer1 = New System.Windows. Forms.Timer(Me. components)
    >
    '
    >
    'ImageList1
    >
    '
    >
    Me.ImageList1.I mageSize = New System.Drawing. Size(16, 16)
    >
    Me.ImageList1.T ransparentColor = System.Drawing. Color.Transpare nt
    >
    '
    >
    'NotifyIcon1
    >
    '
    >
    Me.NotifyIcon1. Text = "NotifyIcon 1"
    >
    Me.NotifyIcon1. Visible = True
    >
    '
    >
    'Timer1
    >
    '
    >
    Me.Timer1.Enabl ed = True
    >
    Me.Timer1.Inter val = 5
    >
    '
    >
    'Form1
    >
    '
    >
    Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
    >
    Me.ClientSize = New System.Drawing. Size(292, 273)
    >
    Me.Name = "Form1"
    >
    Me.ShowInTaskba r = False
    >
    Me.Text = "Form1"
    >
    End Sub
    >
    #End Region
    >
    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load
    >
    sBuild_100_Icon s()
    >
    End Sub
    >
    Sub sBuild_100_Icon s()
    >
    Dim rect As Rectangle = New Rectangle(0, 0, 32, 32)
    >
    Dim displayGraphics As Graphics = Me.CreateGraphi cs()
    >
    Dim img As Image = New Bitmap(rect.Wid th, rect.Height, displayGraphics )
    >
    Dim imageGraphics As Graphics = Graphics.FromIm age(img)
    >
    Dim bcolor As Brush = New
    SolidBrush(Colo r.FromKnownColo r(KnownColor.Co ntrol))
    >
    Dim cColor As Color = Color.Red
    >
    Dim fnt As New Font(Me.Font.Na me, 16, FontStyle.Bold)
    >
    For X As Byte = 0 To 99
    >
    imageGraphics.F illRectangle(bc olor, rect)
    >
    imageGraphics.D rawString(Forma t(X, "00"), fnt, New SolidBrush(cCol or), 0,_
    >
    (rect.Height - imageGraphics.M easureString(Fo rmat(X, "00"), fnt).Height) /
    2)
    >
    Dim mybitmap As Bitmap = img
    >
    Dim hIcon As IntPtr = mybitmap.GetHic on
    >
    ImageList1.Imag es.Add(Icon.Fro mHandle(hIcon))
    >
    Next
    >
    End Sub
    >
    Private Sub Timer1_Tick(ByV al sender As System.Object, ByVal e As
    System.EventArg s) Handles Timer1.Tick
    >
    Static Teller As Int64
    >
    Try
    >
    Teller += 1
    >
    Catch ex As Exception
    >
    Teller = 0
    >
    End Try
    >
    Dim mybitmap As Bitmap = ImageList1.Imag es(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.GetHic on
    >
    NotifyIcon1.Ico n = Icon.FromHandle (hIcon)
    >
    Dim sw As New System.IO.Strea mWriter("teller .txt", True)
    >
    sw.WriteLine(Te ller)
    >
    sw.Close()
    >
    End Sub
    >
    End Class

    Comment

    • JR

      #3
      Re: image and notifyicon problem

      Hi,

      I have build an imagelist (sBuild_100_Ico ns) 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" <branco.medeiro s@gmail.comschr eef in bericht
      news:1173737837 .924051.162420@ 8g2000cwh.googl egroups.com...
      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_Icon s()

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

      Dim Brush As Brush = New SolidBrush(cCol or)
      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(T ext, fnt).Height
      imageGraphics.F illRectangle(bc olor, rect)
      imageGraphics.D rawString( _
      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.EventArg s _
      ) Handles Timer1.Tick

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

      NotifyIcon1.Ico n = mIcons(Teller Mod (MAX_ICON + 1))

      '...
      End Sub
      </aircode>

      HTH.

      Regards,

      Branco.

      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.
      >
      InitializeCompo nent()
      >
      'Add any initialization after the InitializeCompo nent() 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.Disp ose()
      >
      End If
      >
      End If
      >
      MyBase.Dispose( disposing)
      >
      End Sub
      >
      'Required by the Windows Form Designer
      >
      Private components As System.Componen tModel.IContain er
      >
      '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.NotifyIco n
      >
      Friend WithEvents Timer1 As System.Windows. Forms.Timer
      >
      <System.Diagnos tics.DebuggerSt epThrough()Priv ate Sub
      InitializeCompo nent()
      >
      Me.components = New System.Componen tModel.Containe r
      >
      Me.ImageList1 = New System.Windows. Forms.ImageList (Me.components)
      >
      Me.NotifyIcon1 = New System.Windows. Forms.NotifyIco n(Me.components )
      >
      Me.Timer1 = New System.Windows. Forms.Timer(Me. components)
      >
      '
      >
      'ImageList1
      >
      '
      >
      Me.ImageList1.I mageSize = New System.Drawing. Size(16, 16)
      >
      Me.ImageList1.T ransparentColor = System.Drawing. Color.Transpare nt
      >
      '
      >
      'NotifyIcon1
      >
      '
      >
      Me.NotifyIcon1. Text = "NotifyIcon 1"
      >
      Me.NotifyIcon1. Visible = True
      >
      '
      >
      'Timer1
      >
      '
      >
      Me.Timer1.Enabl ed = True
      >
      Me.Timer1.Inter val = 5
      >
      '
      >
      'Form1
      >
      '
      >
      Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
      >
      Me.ClientSize = New System.Drawing. Size(292, 273)
      >
      Me.Name = "Form1"
      >
      Me.ShowInTaskba r = False
      >
      Me.Text = "Form1"
      >
      End Sub
      >
      #End Region
      >
      Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
      System.EventArg s) Handles MyBase.Load
      >
      sBuild_100_Icon s()
      >
      End Sub
      >
      Sub sBuild_100_Icon s()
      >
      Dim rect As Rectangle = New Rectangle(0, 0, 32, 32)
      >
      Dim displayGraphics As Graphics = Me.CreateGraphi cs()
      >
      Dim img As Image = New Bitmap(rect.Wid th, rect.Height, displayGraphics )
      >
      Dim imageGraphics As Graphics = Graphics.FromIm age(img)
      >
      Dim bcolor As Brush = New
      SolidBrush(Colo r.FromKnownColo r(KnownColor.Co ntrol))
      >
      Dim cColor As Color = Color.Red
      >
      Dim fnt As New Font(Me.Font.Na me, 16, FontStyle.Bold)
      >
      For X As Byte = 0 To 99
      >
      imageGraphics.F illRectangle(bc olor, rect)
      >
      imageGraphics.D rawString(Forma t(X, "00"), fnt, New SolidBrush(cCol or), 0,
      _
      >
      (rect.Height - imageGraphics.M easureString(Fo rmat(X, "00"), fnt).Height) /
      2)
      >
      Dim mybitmap As Bitmap = img
      >
      Dim hIcon As IntPtr = mybitmap.GetHic on
      >
      ImageList1.Imag es.Add(Icon.Fro mHandle(hIcon))
      >
      Next
      >
      End Sub
      >
      Private Sub Timer1_Tick(ByV al sender As System.Object, ByVal e As
      System.EventArg s) Handles Timer1.Tick
      >
      Static Teller As Int64
      >
      Try
      >
      Teller += 1
      >
      Catch ex As Exception
      >
      Teller = 0
      >
      End Try
      >
      Dim mybitmap As Bitmap = ImageList1.Imag es(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.GetHic on
      >
      NotifyIcon1.Ico n = Icon.FromHandle (hIcon)
      >
      Dim sw As New System.IO.Strea mWriter("teller .txt", True)
      >
      sw.WriteLine(Te ller)
      >
      sw.Close()
      >
      End Sub
      >
      End Class


      Comment

      Working...