C # Convert string to size

Hey, is there a way to convert a string variable into a Size data type?

for example, if I had this variable

string tempSize;

How can I convert this to a DataType for reading?

+3
source share
8 answers

What size do you mean?

If you mean System.Windows.Size , there is Size.Parse (string) .

If you mean System.Drawing.Size , there is no built-in parsing method, you have to parse the string yourself.

+5
source

We are talking about the System.Drawing.Size structure that is used in Winforms, right?

, Parse(). . - :

// format a custom string to encode your size
string mySizeString = string.Format("{0}:{1}", mySize.Width, mySize.Height);

// parse your custom string and make a new size
string[] sizeParts = mySizeString.Split(':');
int height = int.Parse(sizeParts[0]);
int width = int.Parse(sizeParts[1]);
Size mySize = new Size(height, width);

TypeConverter :

// convert a Size to a string using a type converter
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Size));
string mySizeString = tc.ConvertToString(mySize);

// convert a string to a Size using a type converter
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Size));
Size mySize = (Size)tc.ConvertFromString(mySizeString);

, - (har har). , - , Size. !

+3

System.Windows.Size.Parse , "1100,700".

+1

Parse string:

public static Size Parse(this string str) ...

, , URL WxH, . 1024x768:

namespace MySize
{
    public static class Extensions
    {
        public static Size Parse(this string str)
        {
            try {
                var a = str.Split(new char[] { 'x' });
                return new Size() { 
                    Width = int.Parse(a[0]), 
                    Height = int.Parse(a[1]) 
                };
            }
            catch(Exception) { }
            return Size.nosize;
        }
    }
    public class Size
    {
        public int Width { get; set; }
        public int Height { get; set; }
        public override string ToString()
        {
            return Width.ToString() + "x" + Height.ToString();
        }
        public Size(System.Drawing.Size from)
        {
            Width = from.Width;
            Height = from.Height;
        }
        public Size()
        {
        }
        public static Size nosize = new Size(new System.Drawing.Size(-1, -1));
    }
//[..]
}

ToString() Parse ( String). :

    [TestMethod]
    public void TestSizeParse()
    {
        var s1 = new Size(1024, 768);
        var s1Str = s1.ToString();
        Size s2 = s1Str.Parse();
        Assert.AreEqual(s1.Width, s2.Width);
        Assert.AreEqual(s1.Height, s2.Height);
        Assert.AreEqual(s1, s2, "s1 and s2 are supposed to be equal");
    }
+1

:

int size=Convert.ToInt32(tempSize);

tempSize int, :

Size = New Drawing.Size(xSize, ySize) 
0

- .

, Size (, ).

, Int32.parse . .

0

IMHO JSON.

So you can use JavaScriptSerializerto convert an object of size from and to JSON. And since the standard implementation of Size.ToString () also gives pretty good output, you can use:

        var json = new JavaScriptSerializer();

        var s1 = new Size(5, 125);
        var serialized = s1.ToString().Replace("=",":"); 

        Console.WriteLine(serialized);

        var s2 = json.Deserialize<Size>(serialized);

        Console.WriteLine(s2);

This outputs the following result:

  {Width:5, Height:125}
  {Width=5, Height=125}
0
source

I assume that you have a string in the format "123; 456", since the format used by the Size structure.

To parse this on System.Drawing.Size , you can do something like this:

string s = "123; 456";
int width, height;
string[] dims = s.Split(';');
int.TryParse(dims[0], out width);
int.TryParse(dims[1], out height);

System.Drawing.Size size = new System.Drawing.Size(width, height);

This will fail if the string does not have ;and will return 0 for numbers if the strings cannot be parsed.

0
source

All Articles