Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert from string to system.drawing.point c#

I want to convert from string {X=-24,Y=10} which generated by Point.ToString(); to Point again?

I save the string value in xml file in save mode and I want read it back to point again in read mode.

like image 250
kartal Avatar asked Jan 17 '23 20:01

kartal


2 Answers

var myStringWhichCantBeChanged="{X=-24,Y=10}";
var g=Regex.Replace(myStringWhichCantBeChanged,@"[\{\}a-zA-Z=]", "").Split(',');

Point pointResult = new Point(
                  int.Parse (g[0]),
                  int.Parse( g[1]));
like image 93
Royi Namir Avatar answered Jan 25 '23 16:01

Royi Namir


System.Drawing.Point doesn't define a Parse method at all - you will need to write your own that can take this format and return a Point structure.

System.Windows.Point does have a Parse method and may be more suitable for your needs.

However, since you are outputting to XML, non of this should be needed. You should be serializing and deserializng the object graph, which would take care of this automatically without you needing to worry about parsing and formatting.

like image 34
Oded Avatar answered Jan 25 '23 15:01

Oded