Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Every 1 second I'm loading the grid lines picture and on top of it I'm drawing the ECG. Its taking more CPU time.

Any one have any suggestion as to how to make background image load only once during a form load and on top of it?

I can plot ECG every second.

(I'm calling this method every single second)

Private Sub Draw_ECG()
        Dim Channel_Width As Integer
        Dim Channel_Height As Integer
        Dim myPen As New System.Drawing.Pen(Grid_Color)
        Dim myPen1 As New System.Drawing.Pen(Grid_Color)
        Dim myPen2 As New System.Drawing.Pen(ECG_Trace_Color)
        Dim bm1 As New Bitmap(ECG1.Width, ECG1.Height)
        Dim gr1 As Graphics = Graphics.FromImage(bm1)
        Dim Point1 As New Point(0, 0)
        Dim Point2 As New Point(0, 0)
        Dim Point3 As New Point(0, 0)
        Dim Point4 As New Point(0, 0)
        Dim j As Integer
        Dim Image1 As New Bitmap("c:\Grid.bmp") 'Im loading grid picture here
        Channel_Height = ECG1.Height
        Channel_Width = ECG1.Width
        Channel_Height = ECG1.Height / 2
        gr1.DrawImage(Image1, New Point(0, 0))
        'Draw ECG Trace here
        For j = 0 To (ECG1.Width - 1)
            Point3.X = j
            Point3.Y = Channel_Height - ECG_Data(j)
            Point4.X = j + 1
            Point4.Y = Channel_Height - ECG_Data(j + 1)
            gr1.DrawLine(myPen2, Point3, Point4)
            If j = Display_Cursor_Location Then
                j = j + 5
                If One_Time = True Then j = ECG1.Width
            End If
        Next j
        Me.ECG1.Image = bm1
    End Sub
Posted
Updated 29-Jul-10 23:16pm
v2
Comments
Dalek Dave 30-Jul-10 5:17am    
Edited for Code Block and Grammar (Spelling was good!)

1 solution

Try put this in the constructor:
VB
Public Sub New()
    MyBase.New()
    SetStyle(ControlStyles.DoubleBuffer, True)
    SetStyle(ControlStyles.UserPaint, True)
    SetStyle(ControlStyles.AllPaintingInWmPaint, True)
End Sub


Use the form SuspendLayout before drawing and ResumeLayout when done.

Get the image once from file and just keep it in memory instead of reloading from file every time.

Paint in the paint event instead of randomly when you like. Also, when you call this method to often and calling it before it could finish it starts to pile up and increasingly slowing down. It's like calling Invalidate instead of Redraw or Repaint immediately.

And have a look at these links for some ideas:

http://www.bobpowell.net/transcontrols.htm[^]

How to Use Transparent Images and Labels in Windows Forms[^]

Good luck!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900