[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Problem with Graphics Path...

Posted on 2010-03-22
9
Medium Priority
?
654 Views
Last Modified: 2013-12-17
Hi everyone....
I'm trying to create templates. Each template contains a rectangle, text, small boxes and many bubbles. There are characters inside each bubble.
I'm using graphics path to draw the objects on to the panel.  I want characters inside the bubble to have the pen width 1, also text to be filled completely and it should not affect the other shapes . But the problem is that while changing the pen width of text to 1, it affects the other shapes too. Other shapes are also completely filled, i want only text to be filled completely.
Please any one help me with this.....
Thanks in advance.....


public void PaintShapes(Graphics graphics)
        {
            if (graphicsPath != null)
            {                    
                    graphics.SmoothingMode = SmoothingMode.HighSpeed;
                    graphics.FillPath(Brushes.Transparent, graphicsPath);
                    if (itemtype == "REPLCHAR")
                    {                                               
                        graphics.DrawPath(new Pen(Color.Black, 2), graphicsPath);
                        graphicsPath.FillMode = FillMode.Alternate;
                        
                        //e.Graphics.DrawPath(new Pen(Color.Gray, 1), graphicsPath);
                        //e.Graphics.DrawPath(new Pen(Color.Black, 1), graphicsPath); 
                    }
                    else                    
                    {                        
                        graphics.DrawPath(new Pen(Color.Black, 2), graphicsPath);                       
                    }                
            }
        }

Open in new window

OMR.bmp
0
Comment
Question by:nandithaa
  • 6
  • 3
9 Comments
 
LVL 16

Expert Comment

by:CuteBug
ID: 28322578
Use separate pens to draw the bubble and to draw the characters.
Instead of creating a Pen each time, have two member variables of type Pen. Initialize one pen with the width of 1 and the other with the width of 2.
Use the first pen to draw the rectangles and bubbles. Use the second pen to draw the characters.
0
 
LVL 6

Author Comment

by:nandithaa
ID: 28330348
I tried it, but not working.... Please help me....
0
 
LVL 16

Expert Comment

by:CuteBug
ID: 28331183
Can you provide more code?

If I am understanding correctly, if (itemtype == "REPLCHAR") then it means you are drawing a Character, right?

In your code, you are calling graphics.FillPath(Brushes.Transparent, graphicsPath); first, this will fill the path no matter if the path is a rectangle or a character or a bubble.

So I think graphics.FillPath(Brushes.Transparent, graphicsPath); should be either within the "if" condition or within the "else" condition based on what (itemtype == "REPLCHAR") means.

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 6

Author Comment

by:nandithaa
ID: 28411690
"REPLCHAR" indicates the characters inside the bubble.
I haven already tried giving FillPath inside if condition, but still not working.

I'm attaching the code along with this.... Plz go though it....

Thanks in advance....
addTools.txt
0
 
LVL 16

Expert Comment

by:CuteBug
ID: 28417297
try this way
public void PaintShapes(Graphics graphics)
        {
            if (graphicsPath != null)
            {                    
                    graphics.SmoothingMode = SmoothingMode.HighQuality;

                    if (itemtype == "REPLCHAR")
                    {                                               
                        graphics.FillPath(Brushes.Black, graphicsPath);
                        graphics.DrawPath(new Pen(Color.Black, 1), graphicsPath);
                    }
                    else                    
                    {                        
                        graphics.DrawPath(new Pen(Color.Black, 2), graphicsPath);                       
                    }                
            }
        }

Open in new window

0
 
LVL 6

Author Comment

by:nandithaa
ID: 28420242
I've tried this, but not working...
0
 
LVL 16

Accepted Solution

by:
CuteBug earned 2000 total points
ID: 28424086
I tried a sample application with the above piece of code and it is working fine.

I have attached the code... when the code is executed it shows the number 2 in a bubble.

    public partial class Form1 : Form
    {
        GraphicsPath m_TextPath = null;
        GraphicsPath m_BubblePath = null;

        public Form1()
        {
            InitializeComponent();

            m_TextPath = GetGraphicsPath();

            m_BubblePath = new GraphicsPath();
            m_BubblePath.AddEllipse(30, 30, 20, 20);
        }

        private GraphicsPath GetGraphicsPath()
        {
            StringFormat format = StringFormat.GenericDefault;

            FontFamily fontfamily = new FontFamily("Arial");

            GraphicsPath gp = new GraphicsPath();
            gp.AddString("2", fontfamily, 1, 15, new Point(33, 32), format);

            return gp;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;

            if ((m_TextPath != null) && (m_BubblePath != null))
            {
                e.Graphics.FillPath(Brushes.Black, m_TextPath);
                e.Graphics.DrawPath(new Pen(Color.Black, 1), m_TextPath);
                e.Graphics.DrawPath(new Pen(Color.Black, 2), m_BubblePath);
            }

        }
    }

Open in new window

0
 
LVL 16

Expert Comment

by:CuteBug
ID: 28424439
After seeing your code, I can suggest some ways of improving the efficiency of the code.

For eg you are calling CreateGraphics of the pnlDraw each time you want to add a tool. This is not efficient. Instead you should try to obtain the reference to the Graphics object associated with the pnlDraw. In order to get that you should override the OnPaint method of pnlDraw. The PaintEventArgs parameter to this method has a property called Graphics which will give you the required reference.

Also, each type of tool that you are creating (rectangle, bubble, text) - have classes representing them which will store only the itemtype, origin and size and content(in case of text). Do not create a control for each.

Maintain a List in which you can fill instances of these classes based upon your requirement.

In the overridden method OnPaint() of pnlDraw, you can iterate through the List and draw them using the e.Graphics.
0
 
LVL 16

Expert Comment

by:CuteBug
ID: 28424495
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question