Problem with dynamic image resizing

Apr 27 2009 10:12 PM
Hello,

I'm trying to create a chart generator where random numbers define the chart coordinates.
As new values are added on the chart, the scrollbar appears and then scrolls automatically, pointing to the end of the chart, so the user can see the last data inserted.
The problem is that I'm inserting new values on the picturebox's image but after the scroll exceeds the original picturebox's size, it start repeating the image, instead of displaying the new values - it becomes just like a repeating scene on loop. I don't know what I can in order to solve that, I'd like to have the new values displayed on the image as the scroll continues.

The source code is below (sorry, but I don't know how to place it formatted here...):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private const int WM_SCROLL = 276; // Horizontal scroll
        private const int WM_VSCROLL = 277; // Vertical scroll
        private const int SB_LINEUP = 0; // Scrolls one line up
        private const int SB_LINELEFT = 0;// Scrolls one cell left
        private const int SB_LINEDOWN = 1; // Scrolls one line down
        private const int SB_LINERIGHT = 1;// Scrolls one cell right
        private const int SB_PAGEUP = 2; // Scrolls one page up
        private const int SB_PAGELEFT = 2;// Scrolls one page left
        private const int SB_PAGEDOWN = 3; // Scrolls one page down
        private const int SB_PAGERIGTH = 3; // Scrolls one page right
        private const int SB_PAGETOP = 6; // Scrolls to the upper left
        private const int SB_LEFT = 6; // Scrolls to the left
        private const int SB_PAGEBOTTOM = 7; // Scrolls to the upper right
        private const int SB_RIGHT = 7; // Scrolls to the right
        private const int SB_ENDSCROLL = 8; // Ends scroll

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
   
        List<Point> pointList = new List<Point>();

        Graphics graphics;
        Pen pen;
        Random random = new Random();
        int current = 0;
        Point oldPoint = new Point();
        Point newPoint = new Point();
        Color bgColor;
        Bitmap newBitmap;

        public Form1()
        {
            InitializeComponent();

            bgColor = Color.White;
            pen = new Pen(Color.Black);
            newBitmap = new Bitmap(pbChart.Width, pbChart.Height, PixelFormat.Format24bppRgb);
            graphics = Graphics.FromImage(newBitmap);
            graphics.Clear(bgColor);

            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        }

        void drawPoint(int px, int py)
        {
            newPoint.X = px;
            newPoint.Y = py;

            if (pointList.Count != 0)
            {
                oldPoint = pointList.Last();
            }
            else
            {
                oldPoint = newPoint;
            }

            pointList.Clear();

            pointList.Add(oldPoint);
            pointList.Add(newPoint);

            if (px > pbChart.Width)
            {
                pbChart.Width += (px - pbChart.Width);
                SendMessage(panel1.Handle, WM_SCROLL, (IntPtr)SB_RIGHT, IntPtr.Zero);
            }

            graphics.DrawCurve(pen, pointList.ToArray());

            pbChart.BackgroundImage = newBitmap;

            pbChart.Refresh();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            drawPoint(current, random.Next(0, pbChart.Height));
            current += 5;
        }

        private void btnPlayPause_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled)
                timer1.Stop();
            else
                timer1.Start();
        }
    }
}

I guess that after the width is increased, the graphics width keeps the old one, so it does not show the new values, but I don't know how to overcome that, if that is the root cause of the problem.

Thanks in advance,

Regards,
Caio.