Windows Develop Bookmark and Share   
 index > Windows Forms General > Graphics.SetClip not clipping at the given boundary and clips far inside
 

Graphics.SetClip not clipping at the given boundary and clips far inside

Hi
In my code I'm trying to clip the drawing with the specified rectangle. But the line is not clipped exactly at the boundary but clipped more inside the boundary.

Please, find the code demonstrating the problem.
private void Form1_Paint(object sender, PaintEventArgs e)
        {
            // Bounding (Clipping Rectangle)
            Rectangle rcBnd = new Rectangle(45, 31, 757, 757);

            // Draw Clipping rectangle
            e.Graphics.DrawRectangle(Pens.Purple, rcBnd);

            // Set Clipping area
            e.Graphics.SetClip(rcBnd, CombineMode.Replace);

            // Draw a line between p1 & p2
            Point p1 = new Point(457, 421);
            Point p2 = new Point(458, 1038);
            e.Graphics.DrawLine(Pens.Blue, p1, p2);

            // Reset clipping
            e.Graphics.ResetClip();
        }

NOTE: Clipping works changing the coordinates by 1 pixel. But this can't be a fix as these arrived part of a graphical calculations.

OS:Windows XP Pro,Screen Resolution:1280x1024

Thanks and Regards
R R Raja

R R Raja  Friday, September 18, 2009 12:00 PM
Hi,

I tried your code on my machine (XP Pro as well) and saw what you describe.

I don't know why it happens, but one option might be to set the graphics smoothing mode to high quality or anti-alias before doing your drawing.

e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; or,
e.Graphics.SmoothingMode = SmoothingMode.HighQuality; ,

before drawing the line worked on my machine, with the line clipped correctly.
  • Marked As Answer byR R Raja Friday, September 18, 2009 3:27 PM
  •  
Chris50  Friday, September 18, 2009 2:09 PM
With WF GDI+ Graphics, WYSIWYG. It's not going to be changed. It's been around too long and fixing the bugs, of which there are many, would break too many apps. Also, it is essentially a dead API as all development ceased about 4 years ago. You'll have to work around any bugs you find. It wouldn't hurt to post to Connect, if it isn't already posted.
JohnWein  Friday, September 18, 2009 1:48 PM
Hi,

I tried your code on my machine (XP Pro as well) and saw what you describe.

I don't know why it happens, but one option might be to set the graphics smoothing mode to high quality or anti-alias before doing your drawing.

e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; or,
e.Graphics.SmoothingMode = SmoothingMode.HighQuality; ,

before drawing the line worked on my machine, with the line clipped correctly.
  • Marked As Answer byR R Raja Friday, September 18, 2009 3:27 PM
  •  
Chris50  Friday, September 18, 2009 2:09 PM

You can use google to search for other answers

Custom Search

More Threads

• ERROR: Exception generated when setting ComboBox.SelectedItem
• Unhandled exception using webBrowser
• MouseMove: Cursor "slips off / drops" control
• help with drawing on windows form and scrolling problem
• for DataGridView Control ,How to Get Cell's coordinate
• how to create more curve instead of using hard code
• How to extract text from string
• dsoFramer 1.3 cannot open two Excel documents
• ListViewItem - odd behavior
• Filling region with alternating colors