【语言-c#】Rectangle 与 String 互相转换

Rectangle 转 String

public static string xzString(this System.Drawing.Rectangle src, string defVal = "")
{
    string ret = defVal;
    try
    {
        System.Drawing.RectangleConverter rectConverter = new Drawing.RectangleConverter();
        ret = rectConverter.ConvertToString(src);
    }
    catch (Exception)
    {

    }
    return ret;
}

String 转 Rectangle

public static System.Drawing.Rectangle xzRectangle(this string src)
{
    System.Drawing.Rectangle ret = System.Drawing.Rectangle.Empty;
    try
    {
        System.Drawing.RectangleConverter rectConverter = new Drawing.RectangleConverter();
        object retO = rectConverter.ConvertFromString(src);
        if(retO!=null)
        {
            ret = (System.Drawing.Rectangle)retO;
        }
    }
    catch (Exception)
    {

    }
    return ret;
}

测试

Demo

System.Drawing.Rectangle rect = new System.Drawing.Rectangle(10,10,100,100);
string xzStr = rect.xzString();//xzStr="10, 10, 100, 100"
System.Drawing.Rectangle xzRect = xzStr.xzRectangle();

结果

【语言-c#】Rectangle 与 String 互相转换_第1张图片

你可能感兴趣的:(语言-c#)