DrawRectangle (NET core) as border with equable size?

Please refer to follow code:

PdfPen pn = new PdfPen(Color.Red, 10);
RectangleF rec=new RectangleF(0, 0, 100, 50);
graphics.DrawRectangle(pn, rec);

The result is:

Image_3474_1719990610189

As you can see. The thickness size is not the same. some sides wide from other.

How can get it write?


7 Replies

JT Jeyalakshmi Thangamarippandian Syncfusion Team July 3, 2024 11:37 AM UTC

Hi Matanya,

We were able to reproduce the reported issue with the provided details on our end, and currently, we are analyzing it. We will provide further details on July 5, 2024.

Regards,

Jeyalakshmi T



MC Matanya Cohen July 4, 2024 07:40 AM UTC

I realize, that  the problem disappeared if I set:

RectangleF rec=new RectangleF(3, 3, 100, 50);

Instead:

RectangleF rec=new RectangleF(0, 0, 100, 50);

It's seem like that point 0,0 is outside the view??




JT Jeyalakshmi Thangamarippandian Syncfusion Team July 5, 2024 09:01 AM UTC

Hi Matanya,

Upon further analysis, we observed that the rectangle's rendered size is affected by the pen width when its location is initialized at (0,0). This behavior is consistent with GDI+. To achieve equal widths on all sides, you can adjust the rectangle's location accordingly.

 

GDI+

 

Result

System.Drawing.Bitmap bitmap = new Bitmap(300, 300);

System.Drawing.Graphics gr = Graphics.FromImage(bitmap);

Pen pen = new Pen(Color.Red, 10);

gr.DrawRectangle(pen, new Rectangle(0, 0, 100, 50));

gr.Dispose();

bitmap.Save("Rectangle.png");



Regards,

Jeyalakshmi T



MC Matanya Cohen July 7, 2024 06:32 AM UTC

Did your PDF Lib write to GDI+, And then converting to PDF? What happened on Linux?

(I do expect that the result PDF is not the same across all platforms) 




RS RaashithAhamed SahulHameed Syncfusion Team July 8, 2024 10:51 AM UTC

Hi Matanya,

On further analysis,we have internally draw path for the rectangle like GDI+ in all platforms.This issue occurs while assigning the location (0,0) clipregion cut the width of the upperside and left side of the rectangle Because the pen width is 10, the sides of the rectangle are drawn 10 points wide, such that 2 points are drawn on the boundary itself, 4 points are drawn on the inside, and 4 points are drawn on the outside.


you can adjust the location by highlighted line in below image


Regards,

Raashith Ahamed S




MC Matanya Cohen July 9, 2024 09:20 AM UTC

Thanks. It's interest me if those info documented some ware?


I try understand, please refer to more Q:

A:

This behavior is only for Draw with Pen. All Draw without Pen as parameter, not effected.

For example, Draw Rectangle with Brushes (only). That's true?


B:

You wrote:

" Because the pen width is 10, the sides of the rectangle are drawn 10 points wide, such that 2 points are drawn on the boundary itself, 4 points are drawn on the inside, and 4 points are drawn on the outside. "  

What mean  boundary itself?   (the expression: pn.Width/2 = 5 not 4)


C:

What meaning have to Pen and Brushes regard to DrawString?

 DrawString(string s, PdfFont font, PdfPen pen, PdfBrush brush, PointF point)


JT Jeyalakshmi Thangamarippandian Syncfusion Team July 10, 2024 09:22 AM UTC

Hi Matanya,

This behavior is only for Draw with Pen. All Draw without Pen as parameter, not effected.

For example, Draw Rectangle with Brushes (only). That's true?

Yes, while drawing the rectangle using a brush, the brush fills the rectangle, so it does not draw like a pen.

You wrote:

" Because the pen width is 10, the sides of the rectangle are drawn 10 points wide, such that 2 points are drawn on the boundary itself, 4 points are drawn on the inside, and 4 points are drawn on the outside. "

What mean boundary itself?   (the expression: pn.Width/2 = 5 not 4)

Boundary itself means the location of the rectangle. If the location is 0,0, the 2 points are drawn on the boundary, 4 points are drawn inside the rectangle, and 4 points are outside the rectangle. While drawing 4 points outside the rectangle for the location (0,0), the rectangle gets cut off outside of the rectangle.

 

For more information,

How to: Use a Pen to Draw Rectangles - Windows Forms .NET Framework | Microsoft Learn

 

The expression is only to adjust the location to cut off the rectangle for that purpose only.

What meaning have to Pen and Brushes regard to DrawString?

Pen: Used to outline the text. This is particularly useful if you want to draw text with a border around each character.

Brush: Used to fill the text with a solid color or gradient.

 

Regards,

Jeyalakshmi T


Loader.
Up arrow icon