C# PictureBox Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • walentys
    New Member
    • Jul 2008
    • 9

    C# PictureBox Help

    I have a PictureBox that draws an image and then a rectangle. However, annoyingly, any other window that I put over the rectangle causes it not to redraw. (I can cut out half of the rectangle by opening Notepad and placing it over half of it, or all of it by minimizing and then maximizing)

    Here is my code:

    Code:
            private void Refresh_PictureBox()
            {
                Graphics g = pictureBox1.CreateGraphics();
    
                g.DrawImage(pictureBox1.Image,
                new Rectangle(0, 0, pictureBox1.Right - vScrollBar1.Width,
                pictureBox1.Bottom - hScrollBar1.Height),
                new Rectangle(hScrollBar1.Value, vScrollBar1.Value,
                pictureBox1.Right - vScrollBar1.Width,
                pictureBox1.Bottom - hScrollBar1.Height),
                GraphicsUnit.Pixel);
    
                foreach (NPC npc in NPCs)
                {
                    if (npc.ID == NPCSelectedID)
                    {
                        Point p1 = new Point(npc.startx - hScrollBar1.Value, npc.starty - vScrollBar1.Value);
                        g.DrawRectangle(new Pen(Color.Red), new Rectangle(p1, new Size(npc.width, npc.height)));
                    }
                }
                
                pictureBox1.Update();
    
                g.Dispose();
            }
    Don't worry about the values of the coordinates or the NPC stuff. There must be something wrong with my ordering or my structuring of creating graphics specifically.

    This function gets called on HandleScroll and Click for the picturebox, as well as on the form Resize. And, certainly enough, the rectangle comes back when any of these events are done. But why would the rectangle disappear and not the rest of the image? That's what bothers me.

    If you would like to see more code just say so.

    Thanks,
    Scott
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Attach a handler to the Paint event and put your code in there.

    Comment

    • walentys
      New Member
      • Jul 2008
      • 9

      #3
      Thanks for the advice.

      It's weird now. Now it does redraw, but only when I move the Notepad window slowly enough. If I move it fast, it never comes back.

      Comment

      • walentys
        New Member
        • Jul 2008
        • 9

        #4
        Changing the actual pixels of the picturebox's image is very rigid, but also very sluggish. I'd rather use the Draw methods of the Graphics class, but I'm still having problems with redrawing. Any ideas to get it close to as rigid as changing the actual pixels?

        Any more help would be appreciated,
        Scott

        Comment

        • walentys
          New Member
          • Jul 2008
          • 9

          #5
          I just went down to the byte level of the image instead of the sluggish Get and SetPixel(), and now it works much faster. If anyone wants help with how I did this, feel free to ask.

          Comment

          Working...