title: How To InvalidateRect With GDIPlus
body: <p>All of the GDIPlus demo code I can find draws without invalidation.  So how do you invalidate a rectangle in GDIPlus API when drawing with MouseMove with TImage on a TScrollbox?</p>
  
  <pre><code>function NormalizeRect ( R: TRect ): TRect;
  begin
  
    // This routine normalizes a rectangle. It makes sure that the Left,Top
    // coords are always above and to the left of the Bottom,Right coords.
  
    with R do
    begin
  
      if Left &gt; Right then
        if Top &gt; Bottom then
          Result := Rect ( Right, Bottom, Left, Top )
        else
          Result := Rect ( Right, Top, Left, Bottom )
      else if Top &gt; Bottom then
        Result := Rect ( Left, Bottom, Right, Top )
      else
        Result := Rect ( Left, Top, Right, Bottom );
  
    end;
  
  end;
  
  procedure TFormMain.Image1MouseDown ( Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer );
  begin
    if Line1.Down then
    begin
      GPPointStart := MakePoint ( X, Y );
    end;
  end;
  
  procedure TFormMain.Image1MouseMove ( Sender: TObject; Shift: TShiftState; X, Y: Integer );
  var
    graphics: TGPGraphics;
    pen: TGPPen;
    SolidBrush: TGPSolidBrush;
    rgbTriple: windows.RGBTRIPLE;
    iRect: TRect;
  begin  
    if Line1.Down then
    begin
      if ssLeft in Shift then
      begin
        iRect := NormalizeRect ( Rect ( X, Y, Image1.Picture.Bitmap.Width, Image1.Picture.Bitmap.Height ) );
        InvalidateRect ( ScrollBox1.Handle, @iRect, TRUE );
        graphics := TGPGraphics.Create ( Image1.Picture.Bitmap.Canvas.Handle );
        graphics.Flush ( FlushIntentionFlush );
        GPPointEnd := MakePoint ( X, Y );
        rgbTriple := ColorToRGBTriple ( ColorBox1.Selected );
        pen := TGPPen.Create ( MakeColor ( StrToInt ( Alpha1.Text ), rgbTriple.rgbtRed, rgbTriple.rgbtGreen, rgbTriple.rgbtBlue )
          );
        pen.SetWidth ( StrToInt ( Size1.Text ) );
        graphics.DrawLine ( pen, GPPointStart.X, GPPointStart.Y, GPPointEnd.X, GPPointEnd.Y );
        graphics.Free;
        Image1.Refresh;
      end;
     end;
  end;
  </code></pre>
  
  <p>This is what it looks like:
  <img src="https://i.stack.imgur.com/3MxXc.png" alt="enter image description here"></p>
  
  <p>Using GDIPlus Library from <a href="http://www.progdigy.com" rel="nofollow noreferrer">http://www.progdigy.com</a> with Delphi 2010.</p>