Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. Custom controll flickers!

Custom controll flickers!

Scheduled Pinned Locked Moved Windows Forms
graphicscsswinformsdesignhelp
16 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Luc Pattyn

    there are a few ways to reduce flickering, the most obvious one is to make your Paint handler cheaper, i.e. consume less CPU cycles and fewer objects. In your case, here are some suggestions: 1. there seems to be an image file that gets loaded and decoded each and every time; I suggest you store that image as a class member and load it only once. 2. you are calling panel1.HorizontalScroll.Value and panel1.VerticalScroll.Value over and over; you'd better store those in a couple of local variables (ADDED: within your Paint handler of course). So try these, then re-evaluate the situation. :)

    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


    I only read formatted code with indentation, so please use PRE tags for code snippets.


    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


    modified on Wednesday, June 9, 2010 3:51 PM

    V Offline
    V Offline
    venomation
    wrote on last edited by
    #3

    I done what you recommended but it still flickers allot and its only 1000 tiles at 32*32 width and height ! :confused: Updated Code:

    using System.Drawing;
    using System.Windows.Forms;

    namespace TileMapper.Presentation.Controls
    {
    public partial class MapRenderer : UserControl
    {
    private Point _tileBound;
    private Point _rowsColumns;
    private bool _initalized = false;
    public MapRenderer()
    {
    InitializeComponent();
    this.DoubleBuffered = true;

            // Activates double buffering 
            this.SetStyle(ControlStyles.DoubleBuffer |
               ControlStyles.OptimizedDoubleBuffer |
               ControlStyles.UserPaint |
               ControlStyles.AllPaintingInWmPaint, true);
            this.UpdateStyles();
    
    
        }
    
        public void InitalizeGrid(int tileWidth, int tileHeight, int rows, int columns)
        {
            \_tileBound = new Point(tileWidth, tileHeight);
            \_rowsColumns = new Point(rows, columns);
            panel1.AutoScrollMinSize = new Size(\_tileBound.X \* \_rowsColumns.X, \_tileBound.Y \* \_rowsColumns.Y);
            \_initalized = true;
            \_pen = new Pen(Color.Black);
            \_tile = new Rectangle(0, 0, \_tileBound.X, \_tileBound.Y);
            \_im = Image.FromFile(@"C:/box.png");
    
            \_scrollValue = new Point(panel1.HorizontalScroll.Value, panel1.VerticalScroll.Value);
        }
    
        private Point \_scrollValue;
    
        Pen \_pen;
        Rectangle \_tile;
        private Image \_im;
        void DrawGrid(Graphics e)
        {
            for (int x = 0; x < \_rowsColumns.X; x++)
            {
                for (int y = 0; y < \_rowsColumns.Y; y++)
                {
                    Rectangle rec = new Rectangle(x \* \_tileBound.X - \_scrollValue.X
                        , y \* \_tileBound.Y - \_scrollValue.Y, \_tileBound.X, \_tileBound.Y);
    
    
                    e.DrawImage(\_im, rec, \_tile, GraphicsUnit.Pixel);
                }
    
            }
    
        }
    
    
        private void panel1\_Paint\_1(object sender, PaintEventArgs e)
        {
            if (\_initalized)
            {
                \_scrollValue.X = panel1.HorizontalScroll.Value;
                \_scrollValue.Y = panel1.VerticalScroll.Value;
                DrawGrid(e.Graphics);
            }
        }
    
    }
    

    }

    L 1 Reply Last reply
    0
    • V venomation

      I done what you recommended but it still flickers allot and its only 1000 tiles at 32*32 width and height ! :confused: Updated Code:

      using System.Drawing;
      using System.Windows.Forms;

      namespace TileMapper.Presentation.Controls
      {
      public partial class MapRenderer : UserControl
      {
      private Point _tileBound;
      private Point _rowsColumns;
      private bool _initalized = false;
      public MapRenderer()
      {
      InitializeComponent();
      this.DoubleBuffered = true;

              // Activates double buffering 
              this.SetStyle(ControlStyles.DoubleBuffer |
                 ControlStyles.OptimizedDoubleBuffer |
                 ControlStyles.UserPaint |
                 ControlStyles.AllPaintingInWmPaint, true);
              this.UpdateStyles();
      
      
          }
      
          public void InitalizeGrid(int tileWidth, int tileHeight, int rows, int columns)
          {
              \_tileBound = new Point(tileWidth, tileHeight);
              \_rowsColumns = new Point(rows, columns);
              panel1.AutoScrollMinSize = new Size(\_tileBound.X \* \_rowsColumns.X, \_tileBound.Y \* \_rowsColumns.Y);
              \_initalized = true;
              \_pen = new Pen(Color.Black);
              \_tile = new Rectangle(0, 0, \_tileBound.X, \_tileBound.Y);
              \_im = Image.FromFile(@"C:/box.png");
      
              \_scrollValue = new Point(panel1.HorizontalScroll.Value, panel1.VerticalScroll.Value);
          }
      
          private Point \_scrollValue;
      
          Pen \_pen;
          Rectangle \_tile;
          private Image \_im;
          void DrawGrid(Graphics e)
          {
              for (int x = 0; x < \_rowsColumns.X; x++)
              {
                  for (int y = 0; y < \_rowsColumns.Y; y++)
                  {
                      Rectangle rec = new Rectangle(x \* \_tileBound.X - \_scrollValue.X
                          , y \* \_tileBound.Y - \_scrollValue.Y, \_tileBound.X, \_tileBound.Y);
      
      
                      e.DrawImage(\_im, rec, \_tile, GraphicsUnit.Pixel);
                  }
      
              }
      
          }
      
      
          private void panel1\_Paint\_1(object sender, PaintEventArgs e)
          {
              if (\_initalized)
              {
                  \_scrollValue.X = panel1.HorizontalScroll.Value;
                  \_scrollValue.Y = panel1.VerticalScroll.Value;
                  DrawGrid(e.Graphics);
              }
          }
      
      }
      

      }

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #4

      I'm not absolutely sure, but I have the impression that you are trying to create a virtual panel (a big one scrolling inside a smaller one), maybe 32000 * 32000 pixels large, of which only a small part is visible (I can't see the value of _rowsColumns). Now painting 1 billion pixels when only say 1 million are visible is a huge waste. I can only hope that the PaintEventArgs holds something (maybe ClipRectangle, maybe some member of Graphics) that will enable you to skip most iterations of your nested for loops. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read formatted code with indentation, so please use PRE tags for code snippets.


      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


      V 1 Reply Last reply
      0
      • L Luc Pattyn

        I'm not absolutely sure, but I have the impression that you are trying to create a virtual panel (a big one scrolling inside a smaller one), maybe 32000 * 32000 pixels large, of which only a small part is visible (I can't see the value of _rowsColumns). Now painting 1 billion pixels when only say 1 million are visible is a huge waste. I can only hope that the PaintEventArgs holds something (maybe ClipRectangle, maybe some member of Graphics) that will enable you to skip most iterations of your nested for loops. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        V Offline
        V Offline
        venomation
        wrote on last edited by
        #5

        Thanks for that I think I have solved the looping (not looping through everything) but now a new problem arises, i have to refresh the control every time I scroll to see the updated grid !

        using System.Drawing;
        using System.Windows.Forms;

        namespace TileMapper.Presentation.Controls
        {
        public partial class MapRenderer : UserControl
        {
        private Point _tileBound;
        private Point _rowsColumns;
        private bool _initalized = false;
        Rectangle[,] _renderBounds;
        public MapRenderer()
        {
        InitializeComponent();
        this.DoubleBuffered = true;

                // Activates double buffering 
                this.SetStyle(ControlStyles.DoubleBuffer |
                   ControlStyles.OptimizedDoubleBuffer |
                   ControlStyles.UserPaint |
                   ControlStyles.AllPaintingInWmPaint, true);
                this.UpdateStyles();
        
        
            }
        
            public void InitalizeGrid(int tileWidth, int tileHeight, int rows, int columns)
            {
                \_tileBound = new Point(tileWidth, tileHeight);
                \_rowsColumns = new Point(rows, columns);
                panel1.AutoScrollMinSize = new Size(\_tileBound.X \* \_rowsColumns.X, \_tileBound.Y \* \_rowsColumns.Y);
                \_initalized = true;
                \_pen = new Pen(Color.Black);
                \_tile = new Rectangle(0, 0, \_tileBound.X, \_tileBound.Y);
                \_im = Image.FromFile(@"D:/tile.png");
        
                \_scrollValue = new Point(panel1.HorizontalScroll.Value, panel1.VerticalScroll.Value);
        
                \_renderBounds = new Rectangle\[\_tileBound.X, \_tileBound.Y\];
        
                for (int x = 0; x < \_tileBound.X; x++)
                {
                    for (int y = 0; y < \_tileBound.Y; y++)
                    {
                        \_renderBounds\[x,y\] = new Rectangle(x \* \_tileBound.X
                           , y \* \_tileBound.Y, \_tileBound.X, \_tileBound.Y);
                    }
                }
            }
        
            private Point \_scrollValue;
        
            Pen \_pen;
            Rectangle \_tile;
            private Image \_im;
            void DrawGrid(Graphics e, Rectangle bound)
            {
                int start = bound.X / \_tileBound.X;
                int end = bound.X/\_tileBound.X + bound.Width/\_tileBound.X;
        
                int startY = bound.Y / \_tileBound.Y;
                int endY = bound.Y + bound.Height/\_tileBound.Y;
        
                for (int x = start; x < end; x++)
                {
                    for (int y = startY; y < endY; y++)
                    {
                       e.DrawImage(\_im, \_renderB
        
        L 1 Reply Last reply
        0
        • V venomation

          Thanks for that I think I have solved the looping (not looping through everything) but now a new problem arises, i have to refresh the control every time I scroll to see the updated grid !

          using System.Drawing;
          using System.Windows.Forms;

          namespace TileMapper.Presentation.Controls
          {
          public partial class MapRenderer : UserControl
          {
          private Point _tileBound;
          private Point _rowsColumns;
          private bool _initalized = false;
          Rectangle[,] _renderBounds;
          public MapRenderer()
          {
          InitializeComponent();
          this.DoubleBuffered = true;

                  // Activates double buffering 
                  this.SetStyle(ControlStyles.DoubleBuffer |
                     ControlStyles.OptimizedDoubleBuffer |
                     ControlStyles.UserPaint |
                     ControlStyles.AllPaintingInWmPaint, true);
                  this.UpdateStyles();
          
          
              }
          
              public void InitalizeGrid(int tileWidth, int tileHeight, int rows, int columns)
              {
                  \_tileBound = new Point(tileWidth, tileHeight);
                  \_rowsColumns = new Point(rows, columns);
                  panel1.AutoScrollMinSize = new Size(\_tileBound.X \* \_rowsColumns.X, \_tileBound.Y \* \_rowsColumns.Y);
                  \_initalized = true;
                  \_pen = new Pen(Color.Black);
                  \_tile = new Rectangle(0, 0, \_tileBound.X, \_tileBound.Y);
                  \_im = Image.FromFile(@"D:/tile.png");
          
                  \_scrollValue = new Point(panel1.HorizontalScroll.Value, panel1.VerticalScroll.Value);
          
                  \_renderBounds = new Rectangle\[\_tileBound.X, \_tileBound.Y\];
          
                  for (int x = 0; x < \_tileBound.X; x++)
                  {
                      for (int y = 0; y < \_tileBound.Y; y++)
                      {
                          \_renderBounds\[x,y\] = new Rectangle(x \* \_tileBound.X
                             , y \* \_tileBound.Y, \_tileBound.X, \_tileBound.Y);
                      }
                  }
              }
          
              private Point \_scrollValue;
          
              Pen \_pen;
              Rectangle \_tile;
              private Image \_im;
              void DrawGrid(Graphics e, Rectangle bound)
              {
                  int start = bound.X / \_tileBound.X;
                  int end = bound.X/\_tileBound.X + bound.Width/\_tileBound.X;
          
                  int startY = bound.Y / \_tileBound.Y;
                  int endY = bound.Y + bound.Height/\_tileBound.Y;
          
                  for (int x = start; x < end; x++)
                  {
                      for (int y = startY; y < endY; y++)
                      {
                         e.DrawImage(\_im, \_renderB
          
          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #6

          I take it flickering is acceptable now? yes you now must paint while scrolling; I'd guess a panel1.Invalidate() should do it, Refresh() would be more expensive as it waits for the repaint to finish IIRC. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read formatted code with indentation, so please use PRE tags for code snippets.


          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


          V 1 Reply Last reply
          0
          • L Luc Pattyn

            I take it flickering is acceptable now? yes you now must paint while scrolling; I'd guess a panel1.Invalidate() should do it, Refresh() would be more expensive as it waits for the repaint to finish IIRC. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read formatted code with indentation, so please use PRE tags for code snippets.


            I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


            V Offline
            V Offline
            venomation
            wrote on last edited by
            #7

            Ok I invalidated the panel on the Scroll event but it flickers allot when it move the scroll slider it just looks plain ugly ! Is there another way as its visual effects is the same as when I use "panel1.Refresh()" ? Thanks for your help anyway I apologize for my incompetence xD

            L 1 Reply Last reply
            0
            • V venomation

              Ok I invalidated the panel on the Scroll event but it flickers allot when it move the scroll slider it just looks plain ugly ! Is there another way as its visual effects is the same as when I use "panel1.Refresh()" ? Thanks for your help anyway I apologize for my incompetence xD

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #8

              it is high time you describe your overall goal, and the set-up of your controls. How many panels (or other Controls) are there, what is their purpose and relationship? etc. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read formatted code with indentation, so please use PRE tags for code snippets.


              I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


              V 1 Reply Last reply
              0
              • L Luc Pattyn

                it is high time you describe your overall goal, and the set-up of your controls. How many panels (or other Controls) are there, what is their purpose and relationship? etc. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read formatted code with indentation, so please use PRE tags for code snippets.


                I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                V Offline
                V Offline
                venomation
                wrote on last edited by
                #9

                My goal is to create a simple control that can render a 2 dimensional grid. Its inputs are (tilewidth,tileheight,rows,columns) and the idea is that it can somehow generate the images like a grid inside a panel which can be scrolled left,right, up and down. This should allow me to be able to create a grid that is larger than the form and scroll across it to see different tiles. The problem with the one I made so far (with your assistance) is that it flickers when I scroll, on the "Scroll" event of the panel I force it to "Invalidate()" but this seems to make It flicker. If I do not use "Invalidate" or "Refresh" in the Scroll event of the panel then it does not show the updated grid that is drawn in the Paint event in the panel.

                L 1 Reply Last reply
                0
                • V venomation

                  My goal is to create a simple control that can render a 2 dimensional grid. Its inputs are (tilewidth,tileheight,rows,columns) and the idea is that it can somehow generate the images like a grid inside a panel which can be scrolled left,right, up and down. This should allow me to be able to create a grid that is larger than the form and scroll across it to see different tiles. The problem with the one I made so far (with your assistance) is that it flickers when I scroll, on the "Scroll" event of the panel I force it to "Invalidate()" but this seems to make It flicker. If I do not use "Invalidate" or "Refresh" in the Scroll event of the panel then it does not show the updated grid that is drawn in the Paint event in the panel.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #10

                  0. I'm not sure I understand the role of the images in said grid. Is this a board game? 1. You did not answer "How many panels (or other Controls) are there, what is their purpose and relationship?" 2. Whatever approach, while scrolling, the view will update; and you could then complain for it to be sluggish i.e. not tracking your scroll movements fast enough, or flickering which may (or may not) mean it is following your scroll movement too nervously. 3. From what you told, did you consider having a (large) DataGridView inside a (smaller) Panel, with Panel.AutoScroll=true? :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read formatted code with indentation, so please use PRE tags for code snippets.


                  I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                  V 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    0. I'm not sure I understand the role of the images in said grid. Is this a board game? 1. You did not answer "How many panels (or other Controls) are there, what is their purpose and relationship?" 2. Whatever approach, while scrolling, the view will update; and you could then complain for it to be sluggish i.e. not tracking your scroll movements fast enough, or flickering which may (or may not) mean it is following your scroll movement too nervously. 3. From what you told, did you consider having a (large) DataGridView inside a (smaller) Panel, with Panel.AutoScroll=true? :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read formatted code with indentation, so please use PRE tags for code snippets.


                    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                    V Offline
                    V Offline
                    venomation
                    wrote on last edited by
                    #11

                    0. It is going to be used for a basic 2D Map editor so that I can make levels for games in XNA easier. Each tile can have an image and various properties associated with it and as map sizes vary I could have one map at 50*50 tiles or another at 100*100 tiles (x,y) 1. The panel is the only control running,its part of a custom control that can be dragged and dropped from the toolbox, literally just a panel component with features to render the grid using the System.Draw. Its on a test form on its own, im testing it with a 100 * 100 tile grid at 32*32 width *height. 2. I have played with the speed of which it scrolls, it appears that even a very small change in its scroll values cause the panel to flicker due to it being Invalidated() on the "Scroll" event of the panel. 3. I dont know how a data-grid can help as this it visual drawing to the panel, im only drawing the grid to the panel as I need something that can scroll easily. Hope this helps and thanks ! :-D

                    L 1 Reply Last reply
                    0
                    • V venomation

                      0. It is going to be used for a basic 2D Map editor so that I can make levels for games in XNA easier. Each tile can have an image and various properties associated with it and as map sizes vary I could have one map at 50*50 tiles or another at 100*100 tiles (x,y) 1. The panel is the only control running,its part of a custom control that can be dragged and dropped from the toolbox, literally just a panel component with features to render the grid using the System.Draw. Its on a test form on its own, im testing it with a 100 * 100 tile grid at 32*32 width *height. 2. I have played with the speed of which it scrolls, it appears that even a very small change in its scroll values cause the panel to flicker due to it being Invalidated() on the "Scroll" event of the panel. 3. I dont know how a data-grid can help as this it visual drawing to the panel, im only drawing the grid to the panel as I need something that can scroll easily. Hope this helps and thanks ! :-D

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #12

                      OK, I now have a better view on your app. Thanks. So I take it your entire UserControl is showing (part of) the gridded map, and has its AutoScroll set true. I did some experiments with a Form holding a small autoscroll Panel which holds a much larger panel, and that seems to scroll quite well. Maybe you still have something causing lots of CPU cycles getting wasted, but I don't recall having seen so in your latest Paint handler. Or is what you have shown only a simplified snippet, and some more expensive operations are also present? BTW: I have no XNA experience, however I somehow do expect someone to pop in and tell you now to use XNA for this editor too. :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      I only read formatted code with indentation, so please use PRE tags for code snippets.


                      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                      V 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        OK, I now have a better view on your app. Thanks. So I take it your entire UserControl is showing (part of) the gridded map, and has its AutoScroll set true. I did some experiments with a Form holding a small autoscroll Panel which holds a much larger panel, and that seems to scroll quite well. Maybe you still have something causing lots of CPU cycles getting wasted, but I don't recall having seen so in your latest Paint handler. Or is what you have shown only a simplified snippet, and some more expensive operations are also present? BTW: I have no XNA experience, however I somehow do expect someone to pop in and tell you now to use XNA for this editor too. :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                        I only read formatted code with indentation, so please use PRE tags for code snippets.


                        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                        V Offline
                        V Offline
                        venomation
                        wrote on last edited by
                        #13

                        Well this is my latest code:

                        using System.Drawing;
                        using System.Windows.Forms;

                        namespace TileMapper.Presentation.Controls
                        {
                        public partial class MapRenderer : UserControl
                        {
                        private Point _tileBound;
                        private Point _rowsColumns;
                        private bool _initalized = false;
                        Rectangle[,] _renderBounds;
                        public MapRenderer()
                        {
                        InitializeComponent();
                        this.DoubleBuffered = true;

                                // Activates double buffering 
                                this.SetStyle(
                                   ControlStyles.OptimizedDoubleBuffer , true);
                                this.UpdateStyles();
                                panel1.HorizontalScroll.LargeChange = 1;
                        
                        
                            }
                        
                            public void InitalizeGrid(int tileWidth, int tileHeight, int rows, int columns)
                            {
                                \_tileBound = new Point(tileWidth, tileHeight);
                                \_rowsColumns = new Point(rows, columns);
                                panel1.AutoScrollMinSize = new Size(\_tileBound.X \* \_rowsColumns.X, \_tileBound.Y \* \_rowsColumns.Y);
                                \_initalized = true;
                                \_pen = new Pen(Color.Black);
                                \_tile = new Rectangle(0, 0, \_tileBound.X, \_tileBound.Y);
                                \_im = Image.FromFile(@"C:/box.png");
                        
                                \_scrollValue = new Point(panel1.HorizontalScroll.Value, panel1.VerticalScroll.Value);
                        
                                \_renderBounds = new Rectangle\[\_tileBound.X, \_tileBound.Y\];
                        
                                for (int x = 0; x < \_tileBound.X; x++)
                                {
                                    for (int y = 0; y < \_tileBound.Y; y++)
                                    {
                                        \_renderBounds\[x,y\] = new Rectangle(x \* \_tileBound.X
                                           , y \* \_tileBound.Y, \_tileBound.X, \_tileBound.Y);
                                    }
                                }
                            }
                        
                            private Point \_scrollValue;
                        
                            Pen \_pen;
                            Rectangle \_tile;
                            private Image \_im;
                            void DrawGrid(Graphics e, Rectangle bound)
                            {
                                int start = bound.X / \_tileBound.X;
                                int end = bound.X/\_tileBound.X + bound.Width/\_tileBound.X;
                        
                                int startY = bound.Y / \_tileBound.Y;
                                int endY = bound.Y + bound.Height/\_tileBound.Y;
                        
                                for (int x = start; x < end; x++)
                                {
                                    for (int y = startY; y < endY; y++)
                                    {
                                       e.DrawImage(\_im, \_renderBounds\[x,y\], \_tile, GraphicsUnit.Pixel);
                                    }
                        
                                }
                        
                            }
                        
                        
                        
                        
                        
                            private void panel1\_Paint\_1(object sender, PaintEventArgs e)
                            {
                                if (\_initalized)
                                {
                        
                        L 1 Reply Last reply
                        0
                        • V venomation

                          Well this is my latest code:

                          using System.Drawing;
                          using System.Windows.Forms;

                          namespace TileMapper.Presentation.Controls
                          {
                          public partial class MapRenderer : UserControl
                          {
                          private Point _tileBound;
                          private Point _rowsColumns;
                          private bool _initalized = false;
                          Rectangle[,] _renderBounds;
                          public MapRenderer()
                          {
                          InitializeComponent();
                          this.DoubleBuffered = true;

                                  // Activates double buffering 
                                  this.SetStyle(
                                     ControlStyles.OptimizedDoubleBuffer , true);
                                  this.UpdateStyles();
                                  panel1.HorizontalScroll.LargeChange = 1;
                          
                          
                              }
                          
                              public void InitalizeGrid(int tileWidth, int tileHeight, int rows, int columns)
                              {
                                  \_tileBound = new Point(tileWidth, tileHeight);
                                  \_rowsColumns = new Point(rows, columns);
                                  panel1.AutoScrollMinSize = new Size(\_tileBound.X \* \_rowsColumns.X, \_tileBound.Y \* \_rowsColumns.Y);
                                  \_initalized = true;
                                  \_pen = new Pen(Color.Black);
                                  \_tile = new Rectangle(0, 0, \_tileBound.X, \_tileBound.Y);
                                  \_im = Image.FromFile(@"C:/box.png");
                          
                                  \_scrollValue = new Point(panel1.HorizontalScroll.Value, panel1.VerticalScroll.Value);
                          
                                  \_renderBounds = new Rectangle\[\_tileBound.X, \_tileBound.Y\];
                          
                                  for (int x = 0; x < \_tileBound.X; x++)
                                  {
                                      for (int y = 0; y < \_tileBound.Y; y++)
                                      {
                                          \_renderBounds\[x,y\] = new Rectangle(x \* \_tileBound.X
                                             , y \* \_tileBound.Y, \_tileBound.X, \_tileBound.Y);
                                      }
                                  }
                              }
                          
                              private Point \_scrollValue;
                          
                              Pen \_pen;
                              Rectangle \_tile;
                              private Image \_im;
                              void DrawGrid(Graphics e, Rectangle bound)
                              {
                                  int start = bound.X / \_tileBound.X;
                                  int end = bound.X/\_tileBound.X + bound.Width/\_tileBound.X;
                          
                                  int startY = bound.Y / \_tileBound.Y;
                                  int endY = bound.Y + bound.Height/\_tileBound.Y;
                          
                                  for (int x = start; x < end; x++)
                                  {
                                      for (int y = startY; y < endY; y++)
                                      {
                                         e.DrawImage(\_im, \_renderBounds\[x,y\], \_tile, GraphicsUnit.Pixel);
                                      }
                          
                                  }
                          
                              }
                          
                          
                          
                          
                          
                              private void panel1\_Paint\_1(object sender, PaintEventArgs e)
                              {
                                  if (\_initalized)
                                  {
                          
                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #14

                          OK, the one thing I would simplify is the Graphics.DrawImage; when you specify both source and destination rectangles it will perform a scaling, whereas you probably don't need one. Drawing an entire bitmap at some location can be handled swiftly by Graphics.DrawImage(image, x, y); I am surprised your Panel is AutoScroll; your UserControl is too I assume (but I don't see that). As I said, (non-scrolling large) panel in small autoscrolling panel works fine for me, so I assume non-scrolling large panel in autoscrolling UserControl should too. :)

                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                          I only read formatted code with indentation, so please use PRE tags for code snippets.


                          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                          V 1 Reply Last reply
                          0
                          • L Luc Pattyn

                            OK, the one thing I would simplify is the Graphics.DrawImage; when you specify both source and destination rectangles it will perform a scaling, whereas you probably don't need one. Drawing an entire bitmap at some location can be handled swiftly by Graphics.DrawImage(image, x, y); I am surprised your Panel is AutoScroll; your UserControl is too I assume (but I don't see that). As I said, (non-scrolling large) panel in small autoscrolling panel works fine for me, so I assume non-scrolling large panel in autoscrolling UserControl should too. :)

                            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                            I only read formatted code with indentation, so please use PRE tags for code snippets.


                            I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                            V Offline
                            V Offline
                            venomation
                            wrote on last edited by
                            #15

                            Thanks for your help I should be able to get it sorted ! :-D

                            L 1 Reply Last reply
                            0
                            • V venomation

                              Thanks for your help I should be able to get it sorted ! :-D

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #16

                              You're welcome. :)

                              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                              I only read formatted code with indentation, so please use PRE tags for code snippets.


                              I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups