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. C#
  4. A problem with System.Drawing.Drawing2D

A problem with System.Drawing.Drawing2D

Scheduled Pinned Locked Moved C#
graphicsquestioncsscomtools
6 Posts 3 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.
  • P Offline
    P Offline
    Pedram Behroozi
    wrote on last edited by
    #1

    Hi everyone, I created an application which simply create a rectangle on a PictureBox, here's the code:

    void DrawRectangle(IntPtr hWnd)
    {
    Rectangle rectSquare;
    GraphicsPath graphPath;
    PathGradientBrush brushSquare;
    Graphics gameGraphics;

    gameGraphics = Graphics.FromHwnd(WinHandle);
    graphPath = new GraphicsPath();
    rectSquare = new Rectangle(100, 100, 100, 100);
    graphPath.AddRectangle(rectSquare);
    
    brushSquare = new PathGradientBrush(graphPath);
    brushSquare.CenterColor = Color.Red;
    brushSquare.SurroundColors = new Color\[\] { Color.Blue };
    
    gameGraphics.FillPath(brushSquare, graphPath);
    

    }

    Also I have a button:

    private void button1_Click(object sender, EventArgs e)
    {
    DrawRectangle(pictureBox1.Handle);
    }

    They're working fine but when I minimize my Form and then restore, the rectangle would disappear! I tried calling DrawRectangle in pictureBox1 Paint event but nothing changed. How can I redraw my rectangle? Thanks.

    I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

    L 1 Reply Last reply
    0
    • P Pedram Behroozi

      Hi everyone, I created an application which simply create a rectangle on a PictureBox, here's the code:

      void DrawRectangle(IntPtr hWnd)
      {
      Rectangle rectSquare;
      GraphicsPath graphPath;
      PathGradientBrush brushSquare;
      Graphics gameGraphics;

      gameGraphics = Graphics.FromHwnd(WinHandle);
      graphPath = new GraphicsPath();
      rectSquare = new Rectangle(100, 100, 100, 100);
      graphPath.AddRectangle(rectSquare);
      
      brushSquare = new PathGradientBrush(graphPath);
      brushSquare.CenterColor = Color.Red;
      brushSquare.SurroundColors = new Color\[\] { Color.Blue };
      
      gameGraphics.FillPath(brushSquare, graphPath);
      

      }

      Also I have a button:

      private void button1_Click(object sender, EventArgs e)
      {
      DrawRectangle(pictureBox1.Handle);
      }

      They're working fine but when I minimize my Form and then restore, the rectangle would disappear! I tried calling DrawRectangle in pictureBox1 Paint event but nothing changed. How can I redraw my rectangle? Thanks.

      I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

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

      Hi, 1. yes all painting should be performed in Paint handlers; one of the reasons is exactly the problem you encounter: it is the Paint handlers that will get fired by Windows when ever there is reason to repaint (part of) a Control (a Form is a Control); restoring from Minimized is a very good reason to repaint everything. 2. Graphics.FromHwnd() creates a NEW Graphics, which you must Dispose() off when you are done. It is a slow and costly operation that can be avoided. 3. Why take the long route of taking a handle, asking for a new Graphics, then drawing; all you have to do is set up some data fields that describe the rectangle you want, then Invalidate() the Control (in your case PictureBox), so its Paint handler gets executed again (and draws a rectangle according to the relevant data fields). The Graphics you find in the PaintEventArgs is free, it is not expensive, and you SHOULD NOT Dispose() of that one, since you did not cause its creation, so it isn't your Graphics object. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Fixturized forever. :confused:


      G 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, 1. yes all painting should be performed in Paint handlers; one of the reasons is exactly the problem you encounter: it is the Paint handlers that will get fired by Windows when ever there is reason to repaint (part of) a Control (a Form is a Control); restoring from Minimized is a very good reason to repaint everything. 2. Graphics.FromHwnd() creates a NEW Graphics, which you must Dispose() off when you are done. It is a slow and costly operation that can be avoided. 3. Why take the long route of taking a handle, asking for a new Graphics, then drawing; all you have to do is set up some data fields that describe the rectangle you want, then Invalidate() the Control (in your case PictureBox), so its Paint handler gets executed again (and draws a rectangle according to the relevant data fields). The Graphics you find in the PaintEventArgs is free, it is not expensive, and you SHOULD NOT Dispose() of that one, since you did not cause its creation, so it isn't your Graphics object. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Fixturized forever. :confused:


        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        4. The graphics object that you get in the Paint event is clipped to the screen area that you may draw on. This keeps you from drawing on top of other windows when your window is partially covered. :)

        Despite everything, the person most likely to be fooling you next is yourself.

        P 1 Reply Last reply
        0
        • G Guffa

          4. The graphics object that you get in the Paint event is clipped to the screen area that you may draw on. This keeps you from drawing on top of other windows when your window is partially covered. :)

          Despite everything, the person most likely to be fooling you next is yourself.

          P Offline
          P Offline
          Pedram Behroozi
          wrote on last edited by
          #4

          Hi guys, Sorry I'm late, I had a busy day. I got it, thank both of you a lot. I just put all of my code in Pain event and that's work :) Just another question: I want to draw my rectangle by a button in my form, I thought about this kind of code:

          void pictureBox_Pain(object sender, PainEventArgs e)
          {
          if(sender is Button)
          {
          // Code for drawing a rectangle...
          }
          }

          Am I right? Or there's another (faster, better,...) way? Thanks :)

          I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

          G 1 Reply Last reply
          0
          • P Pedram Behroozi

            Hi guys, Sorry I'm late, I had a busy day. I got it, thank both of you a lot. I just put all of my code in Pain event and that's work :) Just another question: I want to draw my rectangle by a button in my form, I thought about this kind of code:

            void pictureBox_Pain(object sender, PainEventArgs e)
            {
            if(sender is Button)
            {
            // Code for drawing a rectangle...
            }
            }

            Am I right? Or there's another (faster, better,...) way? Thanks :)

            I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            No, that is not the correct way. If you want to draw something on a control, that is done in two steps: 1. Store information about what to draw in some variable(s) in the form. Call the Invalidate method on the control that you are drawing on. 2. In the Paint event for the control you use the information that you stored to do the actual drawing. Example:

            private bool _showRectangle = false;

            void someButton_Click(object sender, EventArgs e) {
            _showRectangle = true;
            pictureBox.Invalidate();
            }

            void picturebox_Paint(object sender, PaintEventArgs e) {
            if (_showRectangle) {
            e.Graphics.DrawRectangle(Pens.Black,10,10,10,10);
            }
            }

            Despite everything, the person most likely to be fooling you next is yourself.

            P 1 Reply Last reply
            0
            • G Guffa

              No, that is not the correct way. If you want to draw something on a control, that is done in two steps: 1. Store information about what to draw in some variable(s) in the form. Call the Invalidate method on the control that you are drawing on. 2. In the Paint event for the control you use the information that you stored to do the actual drawing. Example:

              private bool _showRectangle = false;

              void someButton_Click(object sender, EventArgs e) {
              _showRectangle = true;
              pictureBox.Invalidate();
              }

              void picturebox_Paint(object sender, PaintEventArgs e) {
              if (_showRectangle) {
              e.Graphics.DrawRectangle(Pens.Black,10,10,10,10);
              }
              }

              Despite everything, the person most likely to be fooling you next is yourself.

              P Offline
              P Offline
              Pedram Behroozi
              wrote on last edited by
              #6

              Thanks friend, Helped me a lot :)

              I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

              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