简体   繁体   English

从字符串转换为 system.drawing.point c#

[英]Convert from string to system.drawing.point c#

I want to convert from string {X=-24,Y=10} which generated by Point.ToString();我想从Point.ToString();生成的字符串{X=-24,Y=10} 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.我在保存模式下将字符串值保存在 xml 文件中,我想在读取模式下再次读取它。

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]));

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.Drawing.Point根本没有定义Parse方法 - 您需要编写自己的可以采用这种格式并返回Point结构的方法。

System.Windows.Point does have a Parse method and may be more suitable for your needs. System.Windows.Point确实有Parse方法,可能更适合您的需要。

However, since you are outputting to XML, non of this should be needed.但是,由于您要输出到 XML,因此不需要这些。 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.您应该序列化和反序列化对象图,它会自动处理此问题,而无需担心解析和格式化。

你可以试试这个Point.Parse

Point pointResult = Point.Parse("-24,10");

Hans Passant had the right solution: Don't use Point.ToString() , which gives you that crazy, un-reusable string (MSDN calls it " human-readable "). Hans Passant 有正确的解决方案:不要使用Point.ToString() ,它会给你那个疯狂的、不可重用的字符串(MSDN 称之为“人类可读的”)。 Use the PointConverter class instead.请改用PointConverter类。

To generate the string:生成字符串:

Dim myPoint As New Point(0, 0)
Dim pointConverter As System.ComponentModel.TypeConverter = _
    System.ComponentModel.TypeDescriptor.GetConverter(GetType(Point))
Dim pointAsString As String = pointConverter.ConvertToString(myPoint)

And to interpret the above string:并解释上述字符串:

Dim pointConverter As System.ComponentModel.TypeConverter = _
    System.ComponentModel.TypeDescriptor.GetConverter(GetType(Point))
Dim myNewPoint As New Point = pointConverter.ConvertFromString(pointAsString)

I needed this functionality today, myself, so I just coded it.我自己今天需要这个功能,所以我只是编码它。 Here is a very picky parser, using a "TryParse" approach.这是一个非常挑剔的解析器,使用“TryParse”方法。 I don't like what I call 'lazy' parsing where "blah4,9anything" would get parsed as a point.我不喜欢我所谓的“懒惰”解析,其中“blah4,9anything”会被解析为一个点。 And I don't like throwing errors.而且我不喜欢抛出错误。 The 'TryParse' methods of data types are very robust to me.数据类型的“TryParse”方法对我来说非常健壮。 So here's my implementation for any to use.所以这是我的任何使用的实现。 My only request is if you find a bug, please let me know!我唯一的要求是如果您发现错误,请告诉我! :) :)

public static bool TryParsePoint(string s, out System.Drawing.Point p)
{   p = new System.Drawing.Point();
    string s1 = "{X=";
    string s2 = ",Y=";
    string s3 = "}";
    int x1 = s.IndexOf(s1, StringComparison.OrdinalIgnoreCase);
    int x2 = s.IndexOf(s2, StringComparison.OrdinalIgnoreCase);
    int x3 = s.IndexOf(s3, StringComparison.OrdinalIgnoreCase);
    if (x1 < 0 || x1 >= x2 || x2 >= x3) { return false; }
    s1 = s.Substring(x1 + s1.Length, x2 - x1 - s1.Length);
    s2 = s.Substring(x2 + s2.Length, x3 - x2 - s2.Length); int i = 0;
    if (Int32.TryParse(s1, out i) == false) { return false; } p.X = i;
    if (Int32.TryParse(s2, out i) == false) { return false; } p.Y = i;
    return true;
} // public static bool TryParsePoint(string s, out System.Drawing.Point p)

Note you may also want to remove or change the public or static modifier(s) of the method.请注意,您可能还想删除或更改方法的publicstatic修饰符。 But I used that method in the Program class, so mine needed to be public static .但是我在 Program 类中使用了该方法,所以我的需要是public static Suit yourself tho.适合自己。

请参阅PointConverter ConvertFrom 方法。

try this, its what I use in my project.试试这个,它是我在我的项目中使用的。

string str = "-24,10";
Point pt = new Point(Int32.Parse(str.Split(',')[0]), Int32.Parse(str.Split(',')[1]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM