GDI+ Clip Region allows 0.0f width pen to draw through

Tech-Archive recommends: Fix windows errors by optimizing your registry



Here is my setup:
Windows Vista Ultimate
Visual Studio 2005
Intel Core 2 CPU 6600 @ 2.40 GHz
RAM: 2045 MB
..NET 2.0

The problem is:
Using a zero width pen I can draw into areas that are not part of the clip
region.

Create a simple Windows Forms application.
On the form place a panel.
Attach a method to the Paint event.
Inside the draw create two rectangles.
The first rectangle does not fit in the panel in this very specific manner:
the rectangle's right edge is outside the panel and the left edge is inside
the panel. In other words create a rectangle that is a wider than the panel.
The second rectangle does not intersect the first rectangle and is
completely inside the panel.
Create a region and make it infinite. Exclude rectangle one and rectangle
two from the region.
Set the clip of the graphics instance (found in the event args) to the
region just created using a combine mode of Intersect.

Now fill a rectangle that is bigger than the panel with some color to see if
the two rectangles are excluded from the fill, which they are.

Create a pen that has a zero width.

In a loop draw vertical lines across the panel. Notice how the lines draw
through rectangle one but not rectangle two.

If you change the pen size the lines will not draw through rectangle one or
rectangle two.

Any suggestions on how to still use a zero width pen and not draw inside the
rectangle one?

Is there a specific group for GDI+ to which I should post this problem?

Geoffrey Slinker

--- here is the code---

<DESIGNER CODE>
namespace ClipBug
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose( bool disposing )
{
if( disposing && ( components != null ) )
{
components.Dispose( );
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent( )
{
this.panel1 = new System.Windows.Forms.Panel( );
this.SuspendLayout( );
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.White;
this.panel1.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Location = new System.Drawing.Point( 52, 44 );
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size( 491, 326 );
this.panel1.TabIndex = 0;
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(
this.panel1_Paint );
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF( 6F, 13F );
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size( 625, 440 );
this.Controls.Add( this.panel1 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout( false );

}

#endregion

private System.Windows.Forms.Panel panel1;
}
}



<FORM CODE>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ClipBug
{
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( );
}

private void panel1_Paint( object sender, PaintEventArgs e )
{
Graphics graphics = e.Graphics;
graphics.PageUnit = GraphicsUnit.Inch;

RectangleF rectA = new RectangleF( 3.5f, 0.5f, 2.0f, 1.0f );
RectangleF rectB = new RectangleF( 3.5f, 2.0f, .5f, .5f );

//graphics.FillRectangle( Brushes.Beige, rect );
Region r = new Region( );
r.MakeInfinite( );
r.Exclude( rectA );
r.Exclude( rectB );

Region oldClip = graphics.Clip;

graphics.SetClip( r,
System.Drawing.Drawing2D.CombineMode.Intersect );

float x = 0.0f;

graphics.FillRectangle( Brushes.LightCoral, 0.0f, 0.0f, 1000.0f,
1000.0f );

Pen pen = new Pen( Brushes.Black, 0.0f );

for( int i = 0; i < 10000; i++ )
{
graphics.DrawLine( pen, x, 0.0f, x, 100.0f );
x += 0.1f;
}

pen.Dispose( );
}
}
}
.



Relevant Pages

  • RE: GDI+ Clip Region allows 0.0f width pen to draw through
    ... private void panel1_Paint ... I took the previous code I posted and made the panel anchor to all four ... Using a zero width pen I can draw into areas that are not part of the clip ... The first rectangle does not fit in the panel in this very specific manner: ...
    (microsoft.public.win32.programmer.gdi)
  • RE: GDI+ Clip Region allows 0.0f width pen to draw through
    ... Just to make this more outrageous if you draw the vertical lines from the ... I took the previous code I posted and made the panel anchor to all four ... private void panel1_SizeChanged ... In other words create a rectangle that is a wider than the panel. ...
    (microsoft.public.win32.programmer.gdi)
  • Re: My earlier post was Horizontal Line
    ... The code below is the exact of my Report module. ... It can draw the table ... starting point for the line or rectangle. ... QBColor function to specify the color. ...
    (microsoft.public.access.formscoding)
  • Zero Width Pen Draws through clipped region...
    ... Exclude the rectangle from the graphics clip region. ... Draw a line down the panel. ... IF you use a pen that is more than one pixel the clipping works correctly. ...
    (microsoft.public.dotnet.framework.drawing)
  • RE: TreeView multiple selections
    ... But with this behavior I can't draw a node to normal after selecting twice ... .Net2.0 has added the build-in support for custom-draw. ... rectangle based on your requirement. ... Dim focusPen As New Pen ...
    (microsoft.public.dotnet.general)