PDA

View Full Version : length and measure


Mindset
11-12-2009, 12:33 PM
Could someone please explain how to convert a text.length to the correct value for a rectangle width?

The int value is 28 which is the no of characters but my rectangle is 28 pixels - I think. I have looked through msdn but it is just defeating me and I know it will be simple :-)

NinjaTrader_Josh
11-12-2009, 12:54 PM
Unfortunately this is beyond the support we can provide. Hopefully another forum member with experience in this area can help you.

Ralph
11-12-2009, 01:58 PM
Mindset, you have to use ChartGraphics.MeasureString(...) for that purpose.

Regards
Ralph

Mindset
11-13-2009, 12:40 AM
Hi Ralph

That is what I thought with my research but I am missing a piece of the puzzle

width = ChartGraphics.MeasureString(tempstr , textFontBold);

I get the error that graphics doesn't exist. I have a plot override but this statement is outside it - is that not ok?


Could you show an example?

Ralph
11-13-2009, 01:29 AM
Hi Mindset,

I made a mistake in my description. "ChartGraphics" is the name of a local variable, and therefore meaningless to you. "ChartGraphics" is the reference to first parameter of the Plot() method (Graphics object). Finally I use "ChartGraphics" outside Plot(), but I set it inside Plot(), every time Plot() is invoked.

Regards
Ralph

Mindset
11-13-2009, 09:43 AM
Ralph

I think I sort of get some of what you are saying. Having had no formal programme training my knowledge is mostly from library books and cutting/pasting other people's code to fit together to do what I want it to do.

I have learnt to a very basic level along the way.

There is in fact a ChartGraphics.Graphics Property ( just to add to the confusion in my head) on msdn (lol).

I shall get there in the end but your comment about outside plot() and inside plot every time it's invoked left me puzzled.

Ralph
11-13-2009, 09:54 AM
Hi Mindset,

I appreciate your comment.
Let's clarify some of the issues. The first is: I can't find a "ChartGraphics" in the MSDN Doc. Do you have a link for that?

Regards
Ralph

Mindset
11-13-2009, 10:22 AM
I found that property here (http://msdn.microsoft.com/en-us/library/system.web.ui.datavisualization.charting.chartgrap hics.graphics(VS.100).aspx)

I am a teensie bit further forward.
I have moved my string to the variables section and I can now get a SizeF.:D

I understand this to give 4 floating point nos delineating the string in a box (rectangle)

When I then try and define a new rectangle it says it can't convert from float to int?? Ha Ha!!

Ralph
11-13-2009, 12:40 PM
How do you want to use the SizeF? To create an rectangle? Is your system based on double, float, or integer?

Regards
Ralph

Mindset
11-14-2009, 10:33 AM
Appreciate this Ralph. My SizeF is called stringsize.

I assumed this would work if I created a rectangle using SizeF

Rectangle rect = new Rectangle(5,stringsize.Height,stringsize.Width,tex tFontBold.Height);

params 2 and 3 are the problem. 1 and 4 seem fine.

My reference book (Learn c# in 21 days (Sams)) has no info on this either.

I am just not getting the whole concept here - does SizeF not contain all the info necessary to create a whole rectangle as it holds 4 points?

On msdn it looks like I should use RectangleF and then just add a start point

eg RectangleF rectF =
new RectangleF(myLocation, stringSize);

but that doesn't work either.

You can probably sense I am getting quite frustrated with this.

Ralph
11-14-2009, 10:59 AM
Hi Mindset,

hard to judge at which day they would teach the SizeF in their books, obviously after day 21 :|

Let's make an end with your frustration.
SizeF contains 2 values, what ever that means, you could use such structure whereever you need a value pair. These values are float. A RectangleF needs 4 coordinates or a PointF and a SizeF:
http://msdn.microsoft.com/en-gb/library/system.drawing.rectanglef.rectanglef.aspx
I guess myLocation isn't a SizeF?
Rectangle needs 4 coordinates or a Point and a Size, all integers:
http://msdn.microsoft.com/en-gb/library/system.drawing.rectangle.rectangle.aspx

I do operate with integers preferable. If you do too, I would recommend to convert the SizeF to a Size and then your Rectangle-Construction should work. Here are 2 examples how to convert:
SizeF strSizeF = Graphics.MeasureString(str, stringFont);
Size strSize = Size.Ceiling(strSizeF);
Size strSize = Size.Truncate(strSizeF);

Let me know, if that works.

Regards
Ralph

Mindset
11-23-2009, 02:03 AM
Ralph

Apologies - have been away and still struggling with this Size and SizeF thing.
appreciate all your help but I will take this info, go into my cave, and try and get it all right in my head.

Mindset
11-24-2009, 08:50 AM
ok - I have started to get some of the concepts of the rectangle. Still don't get what the difference is between canvas and bounds. Actually I don't really get bounds either but never mind - I can use it.


Thanks for the explanation of coridinates vs integers - that was a major stumbling block for me before you explained so succinctly.:)
my problem now is the width of the string. i am using stringbuilder to compose a string and my size keeps getting a very small measurement.
I am measuring as the stringbuilder is finished - is there something special I am missing/ need to do?

Ralph
11-24-2009, 09:19 AM
Hi Mindset,

bounds is the rectangle describing the dimension of your canvas, nothing more. The graphics object is the instantiation of a graphical class containing all the functionality needed for the interaction with the canvas (i.e. measurestring()).

Measurestring returns that SizeF structure, wich contains 2 values. One is the width and one is the hight of your string. Since you have to hand over a font object as second parameter, MeasureString() knows how to calculate the width correctly. It doesn't matter with which tools you constructed the string.

Regards
Ralph

Mindset
11-24-2009, 10:12 AM
Hi Ralph

Just spent a v frustrating hour or so with this.


int height2 = (textFontBold.Height * 3);
width3 = graphics.MeasureString(incomingpath ,textFontBold); //SizeF
int pointx = ChartControl.Bounds.Left;
int pointy = ChartControl.Bounds.Top;
int width4 = Size.Ceiling(width3);// error??
Rectangle rectangle1 = new Rectangle(pointx,pointy,width4,height2);
graphics.DrawRectangle(borderPen,rectangle1);



I get an implicit conversion error on the width4 variable.

Ralph
11-24-2009, 10:28 AM
Hi Mindset,

you could try something like this:


SizeF mySizeF = graphics.MeasureString(incomingpath ,textFontBold);
Size mySize = Size.Ceiling(mySizeF);
Point myPoint = new Point(ChartControl.Bounds.Left, ChartControl.Bounds.Top);
Rectangle rectangle1 = new Rectangle(myPoint,mySize);
graphics.DrawRectangle(borderPen,rectangle1);


Let me know how that works
Ralph

Mindset
11-24-2009, 10:58 AM
Interesting that Point can take x and y cords together.

However like you I would prefer to work with integers so how do I convert

my take is that I declare 2 points for the X and Y co ords.
Then a height which I shall take off the text.height - but it all comes back to the width which is a SizeF or a Size but not an integer?

I feel a lightbulb moment coming but right now I am in the darkest hole :D
I feel quite annoyed asking these questions really - most things I have been able to work out but this has just got me.

Ralph
11-24-2009, 12:32 PM
Hi Mindset,

You can derive the width and heigth integers from the Size struct.
int height = mySize.Height
int width = mysize.Width

Regarding to your rectangle you need to make a decision between a Point() and a Size() or 2 coordinate-points. What you always have is a Size(). Then you either construct a Point() as in my example or you calculate the second coordinate-point like pointx+width and pointy+height.

Regards
Ralph

Mindset
11-24-2009, 01:28 PM
Many thanks for your patience Ralph.

I believe I have got the rudiments of it now.
Width and height are properties of the Size - Doh! a real Homer moment.

Ralph
11-24-2009, 01:50 PM
I could have pointed out that earlier, but sometimes it is not easy for me to recognise through the internet what exactly is the core of the issue at the other end of the cable.

Btw, the other way around is also a good looking solution. You could instantiate a Point struct for your class and then you load it with the x nad y coordinate and finaly use the Rectangle(Point, Size):

Point myPoint = new Point();
...
myPoint.X = pointx
myPoint.Y = pointy
Rectangle rectangle1 = new Rectangle(myPoint, mySize)

You are now spoilt for choice, is this expression correct?

Mindset
11-26-2009, 07:05 AM
Yes I am now spoilt for choice.
Your command of English is excellent!

Mindset
11-29-2009, 10:37 AM
and then thud...
I have spent most of Sunday afternoon trying to solve this riddle.. and I am just not getting it?

How do I get my PointF or even better the Y co ordinate to place my rectangle?


private StringAlignment BoxX = StringAlignment.Near;
private StringAlignment BoxY = StringAlignment.Near;


// case BoxPosition.BottomLeft:
//
// BoxX = StringAlignment.Near;
// BoxY = StringAlignment.Far;
// line1locale = 32;
// line2locale = 16;
// break;
// }



line 1 and line 2 are built using StringBuilder and then I use DrawTextAbsolute for the 2 separate lines.

I measure my largest string


mySizeF = graphics.MeasureString(str1,textFontBold);
mySizeF2 = graphics.MeasureString(str2,textFontBold);

Size mySize = Size.Ceiling(mySizeF);
Size mySize2 = Size.Ceiling(mySizeF2);

int myWidth = Math.Max(mySize.Width,mySize2.Width);
int height = (textFontBold.Height * 2) +5;



I am then completely confused about how to get the Y start co ordinate.

Point point1 = new Point(line1locale) doesn't give me the correct value.

I prefer the integer route so
If I manually use ChartControl.CanvasTop +2 - that works
but the bottom is Chartcontrol.CanvasBottom - 289 - I only got this by manually deprecating nos until it looked right.

There has to be a simple way to get this point as the program has already placed the text - but how do I recover this value???

Ralph
11-29-2009, 12:59 PM
Hi Mindset,

not sure what exactly your problem is, so I try my 2 cents.

Your point-structure consists of 2 coordinates, a X and a Y. I would set that in relation to the Canvas.X and Canvas.Y or Canvas.Top and Canvas.Left respectively. These pairs all describe the upper left corner of an rectangle and you could set these pairs into relationship easily.

That means:
Point.Y = Canvas.Top: Your rectangle is placed at the top edge of your canvas.
Point.Y = Canvas.Bottom - myRectangle.Height: Your rectangle is placed at the bottom edge of your canvas.

Btw, moving shapes around on a canvas is an exciting untertaking :)

Regards
Ralph