【语言-c#】常用类型转化

1、 转化说明

对象 与System.String互转方法名称  字符串格式 优缺点
System.Drawing.Rectangle

xzString

xzRectangle

X, Y, Width, Height  
System.Drawing.Point

xzString

xzPoint

X, Y  
System.Drawing.Color

xzString

xzColor

A, R, G, B(A≠255)

R, G, B(A=255)

 
System.Drawing.Color

xzStringHtml

xzColorHtml

#RRGGBB

 丢失透明属性

System.Drawing.Size

xzString

xzSize

Width, Height  
System.Drawing.SizeF

xzString

xzSizeF

Width, Height  
System.Drawing.Font

xzString

xzFont

Name, SizeUnit[, style=[Bold][, Italic][, Underline][, Strikeout]] 详细信息见1.1,格式Format
System.Drawing.Icon

xzBase64String

xzBase64Icon

data:image/ico;base64,.....  
System.Drawing.Image

xzBase64String

xzBase64Image

data:image/Format;base64,..... 详细信息见1.2,格式Format
System.Drawing.Imaging.ImageFormat

xzString

xzImageFormat

Bmp|Emf|Exif|Gif|Icon|Jpeg|MemoryBmp|Png|Tiff|Wmf|

详细信息见1.2
System.Guid

xzString

xzGuid

dddddddd-dddd-dddd-dddd-dddddddddddd

 
System.Windows.Forms.Padding

xzString

xzPadding

Left, Top, Right, Bottom  

1.1、System.Drawing.Font

1.1.1、Unit - System.Drawing.GraphicsUnit

enum  Value Format  备注
World 0 world  
Display 1   参数无效
Pixel 2 px  
Point 3 pt  默认
Inch 4  in  
Document 5 doc  
Millimeter 6  mm  

1.1.1、style - System.Drawing.FontStyle

enum  Value  Format  备注
Regular 0   普通文本
Bold 1 style=Bold 加粗文本
Italic 2 style=Italic 倾斜文本
Underline 4 style=Underline  带下划线的文本
Strikeout 8 style=Strikeout 中间有直线通过的文本
Bold|Italic|Underline|Strikeout F style=Bold, Italic, Underline, Strikeout 加粗、倾斜、带下划线、中间有直线通过的文本

1.2、System.Drawing.Imaging.ImageFormat - Format 

enum  Guid  Format  备注
Bmp       {b96b3cab-0728-11d3-9d7b-0000f81ef32e}   data:image/bmp;base64,..... .bmp
Emf    {b96b3cac-0728-11d3-9d7b-0000f81ef32e}       不支持
Exif   {b96b3cb2-0728-11d3-9d7b-0000f81ef32e}       不支持
Gif     {b96b3cb0-0728-11d3-9d7b-0000f81ef32e}     data:image/gif;base64,..... .gif
Icon  {b96b3cb5-0728-11d3-9d7b-0000f81ef32e}     data:image/ico;base64,..... .ico
Jpeg  {b96b3cae-0728-11d3-9d7b-0000f81ef32e}     data:image/jpg;base64,..... .jpg、.jpeg
MemoryBmp   {b96b3caa-0728-11d3-9d7b-0000f81ef32e}       不支持
Png {b96b3caf-0728-11d3-9d7b-0000f81ef32e}     data:image/png;base64,..... .png
Tiff   {b96b3cb1-0728-11d3-9d7b-0000f81ef32e}       不支持
Wmf   {b96b3cad-0728-11d3-9d7b-0000f81ef32e}    不支持

1.3、Guid

包含下面任一格式的 GUID 的字符串(“d”表示忽略大小写的十六进制数字):

所有内置的空格都将被忽略。 组中的所有前导零都将被忽略。 组中显示的数字为可在该组显示的有意义数字的最大数目。 您可以指定从 1 到为组显示的位数。
指定的位数被认为是该组低序位的位数。

格式描述 支持的Format  备注
32 个连续的数字 dddddddddddddddddddddddddddddddd  
8、4、4、4 和 12 位数字的分组,各组之间有连线符。

dddddddd-dddd-dddd-dddd-dddddddddddd

{dddddddd-dddd-dddd-dddd-dddddddddddd}

(dddddddd-dddd-dddd-dddd-dddddddddddd)

也可以用一对大括号或者圆括号将整个 GUID 括起来
8、4 和 4 位数字的分组,和一个 8 组 2 位数字的子集,每组都带有前缀“0x”或“0X”,以逗号分隔。 {0xdddddddd, 0xdddd, 0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}}

整个 GUID 和子集用大括号括起来。

所有大括号、逗号和“0x”前缀都是必需的。

2、测试

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace xzConvert
{
    static class Program
    {
        /// 
        /// 应用程序的主入口点。
        /// 
        [STAThread]
        static void Main()
        {
            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();

            System.Drawing.Point pnt = new System.Drawing.Point(10, 10);
            xzStr = pnt.xzString();//xzStr="10, 10"
            System.Drawing.Point xzPnt = xzStr.xzPoint();

            System.Drawing.Color clr = System.Drawing.Color.FromArgb(10, 10, 10, 10);
            xzStr = clr.xzString();//xzStr="10, 10, 10, 10"
            System.Drawing.Color xzClr = xzStr.xzColor();

            System.Drawing.Color clrHtml = System.Drawing.Color.FromArgb(10, 10, 10, 10);
            xzStr = clrHtml.xzStringHtml();//xzStr="10, 10, 10, 10"
            System.Drawing.Color xzClrHtml = xzStr.xzColorHtml();

            System.Drawing.Size sz = new System.Drawing.Size(10, 10);
            xzStr = sz.xzString();//xzStr="10, 10"
            System.Drawing.Size xzSZ = xzStr.xzSize();

            System.Drawing.SizeF SZF = new System.Drawing.SizeF(10.1f, 10.1f);
            xzStr = SZF.xzString();//xzStr="10.1, 10.1"
            System.Drawing.SizeF xzSZF = xzStr.xzSize();

            System.Drawing.Font font = new System.Drawing.Font("宋体", 12.5f, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Strikeout | System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point);
            xzStr = font.xzString();
            System.Drawing.Font xzFont = xzStr.xzFont();

            System.Drawing.Icon icon = new System.Drawing.Icon("1.ico");
            xzStr = icon.xzBase64String();
            System.Drawing.Icon xzIcon = xzStr.xzBase64Icon();
            icon.xzFile("a.ico");

            System.Drawing.Image image = System.Drawing.Bitmap.FromFile("1.tiff");
            xzStr = image.xzBase64String();
            System.Drawing.Image xzImage = xzStr.xzBase64Image();
            xzImage.Save("a.png");


            System.Drawing.Imaging.ImageFormat imageFormat = System.Drawing.Imaging.ImageFormat.Wmf;
            xzStr = imageFormat.xzString();
            System.Drawing.Imaging.ImageFormat xzImageFormat = xzStr.xzImageFormat();

            System.Guid guid = System.Drawing.Imaging.ImageFormat.Jpeg.Guid;
            xzStr = guid.xzString();
            System.Guid xzGuid = xzStr.xzGuid();

            System.Windows.Forms.Padding padding = new Padding(3, 4, 5, 6);
            xzStr = padding.xzString();
            System.Windows.Forms.Padding xzPadding = xzStr.xzPadding();

            System.Windows.Forms.TreeNode treeNode = new TreeNode("解决方案");
            {
                System.Windows.Forms.TreeNode treeNode1 = new TreeNode("xzConvert");
                {
                    System.Windows.Forms.TreeNode treeNode21 = new TreeNode("Properties");
                    System.Windows.Forms.TreeNode treeNode22 = new TreeNode("引用");
                    System.Windows.Forms.TreeNode treeNode23 = new TreeNode("base64");
                    System.Windows.Forms.TreeNode treeNode24 = new TreeNode("Form1");
                    System.Windows.Forms.TreeNode treeNode25 = new TreeNode("Program");
                    System.Windows.Forms.TreeNode treeNode26 = new TreeNode("xzHelper");
                    treeNode1.Nodes.Add(treeNode21);
                    treeNode1.Nodes.Add(treeNode22);
                    treeNode1.Nodes.Add(treeNode23);
                    treeNode1.Nodes.Add(treeNode24);
                    treeNode1.Nodes.Add(treeNode25);
                    treeNode1.Nodes.Add(treeNode26);
                }
                treeNode.Nodes.Add(treeNode1);
            }
            xzStr = treeNode.xzString();
            System.Windows.Forms.TreeNode TreeNode = xzStr.xzTreeNode();

            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

    }
}

3、源码 xzHelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System
{
    public static class xzHelper
    {
        #region System.Drawing.Rectangle
        /// 
        /// Rectangle 转 String
        /// 
        /// 
        /// 
        /// X, Y, Width, Height
        public static string xzString(this System.Drawing.Rectangle src, string defVal = "")
        {
            string ret = defVal;
            try
            {
                System.Drawing.RectangleConverter converter = new Drawing.RectangleConverter();
                ret = converter.ConvertToString(src);
            }
            catch (Exception)
            {

            }
            return ret;
        }
        /// 
        /// String 转 Rectangle
        /// 
        /// X, Y, Width, Height
        /// 
        public static System.Drawing.Rectangle xzRectangle(this string src)
        {
            System.Drawing.Rectangle ret = System.Drawing.Rectangle.Empty;
            try
            {
                System.Drawing.RectangleConverter converter = new Drawing.RectangleConverter();
                object retO = converter.ConvertFromString(src);
                if (retO != null)
                {
                    ret = (System.Drawing.Rectangle)retO;
                }
            }
            catch (Exception)
            {

            }
            return ret;
        }
        #endregion

        #region System.Drawing.Point
        /// 
        /// Point 转 String
        /// 
        /// 
        /// 
        /// X, Y
        public static string xzString(this System.Drawing.Point src, string defVal = "")
        {
            string ret = defVal;
            try
            {
                System.Drawing.PointConverter converter = new Drawing.PointConverter();
                ret = converter.ConvertToString(src);
            }
            catch (Exception)
            {

            }
            return ret;
        }
        /// 
        /// String 转 Point
        /// 
        /// X, Y
        /// 
        public static System.Drawing.Point xzPoint(this string src)
        {
            System.Drawing.Point ret = System.Drawing.Point.Empty;
            try
            {
                System.Drawing.PointConverter converter = new Drawing.PointConverter();
                object retO = converter.ConvertFromString(src);
                if (retO != null)
                {
                    ret = (System.Drawing.Point)retO;
                }
            }
            catch (Exception)
            {

            }
            return ret;
        }
        #endregion

        #region System.Drawing.Color
        /// 
        /// Color 转 String
        /// 
        /// 
        /// 
        /// 
        /// A==255|A, R, G, B
        /// A!=255|R, G, B
        /// 
        public static string xzString(this System.Drawing.Color src, string defVal = "")
        {
            string ret = defVal;
            try
            {
                System.Drawing.ColorConverter converter = new System.Drawing.ColorConverter();
                ret = converter.ConvertToString(src);
            }
            catch (Exception)
            {

            }
            return ret;
        }
        /// 
        /// String 转 Color
        /// 
        /// 
        /// A==255|A, R, G, B
        /// A!=255|R, G, B
        /// 
        /// 
        public static System.Drawing.Color xzColor(this string src)
        {
            System.Drawing.Color ret = System.Drawing.Color.Empty;
            try
            {
                System.Drawing.ColorConverter converter = new System.Drawing.ColorConverter();
                object retO = converter.ConvertFromString(src);
                if (retO != null)
                {
                    ret = (System.Drawing.Color)retO;
                }
            }
            catch (Exception)
            {

            }
            return ret;
        }
        /// 
        /// String(Html格式) 转 Color
        /// 缺点丢失Alpha(透明) 特性
        /// 
        /// 
        /// 
        /// 
        /// #RRGGBB
        /// 
        public static string xzStringHtml(this System.Drawing.Color src, string defVal = "")
        {
            string ret = defVal;
            try
            {
                ret = System.Drawing.ColorTranslator.ToHtml(src);
            }
            catch (Exception)
            {

            }
            return ret;
        }
        /// 
        /// Color 转 String(Html格式)
        /// 
        /// 
        /// #RRGGBB
        /// 
        /// 
        public static System.Drawing.Color xzColorHtml(this string src)
        {
            System.Drawing.Color ret = System.Drawing.Color.Empty;
            try
            {
                if (string.IsNullOrEmpty(src) == false)
                {
                    ret = System.Drawing.ColorTranslator.FromHtml(src);
                }
            }
            catch (Exception)
            {

            }
            return ret;
        }
        #endregion

        #region System.Drawing.Size
        /// 
        /// Size 转 String
        /// 
        /// 
        /// 
        /// Width, Height
        public static string xzString(this System.Drawing.Size src, string defVal = "")
        {
            string ret = defVal;
            try
            {
                System.Drawing.SizeConverter converter = new System.Drawing.SizeConverter();
                ret = converter.ConvertToString(src);
            }
            catch (Exception)
            {

            }
            return ret;
        }
        /// 
        /// String 转 Size
        /// 
        /// 
        /// 
        public static System.Drawing.Size xzSize(this string src)
        {
            System.Drawing.Size ret = System.Drawing.Size.Empty;
            try
            {
                System.Drawing.SizeConverter converter = new System.Drawing.SizeConverter();
                object retO = converter.ConvertFromString(src);
                if (retO != null)
                {
                    ret = (System.Drawing.Size)retO;
                }
            }
            catch (Exception)
            {

            }
            return ret;
        }
        #endregion

        #region System.Drawing.SizeF
        /// 
        /// SizeF 转 String
        /// 
        /// 
        /// 
        /// Width, Height
        public static string xzString(this System.Drawing.SizeF src, string defVal = "")
        {
            string ret = defVal;
            try
            {
                System.Drawing.SizeFConverter converter = new System.Drawing.SizeFConverter();
                ret = converter.ConvertToString(src);
            }
            catch (Exception)
            {

            }
            return ret;
        }
        /// 
        /// String 转 SizeF
        /// 
        /// 
        /// 
        public static System.Drawing.SizeF xzSizeF(this string src)
        {
            System.Drawing.SizeF ret = System.Drawing.Size.Empty;
            try
            {
                System.Drawing.SizeFConverter converter = new System.Drawing.SizeFConverter();
                object retO = converter.ConvertFromString(src);
                if (retO != null)
                {
                    ret = (System.Drawing.SizeF)retO;
                }
            }
            catch (Exception)
            {

            }
            return ret;
        }
        #endregion

        #region System.Drawing.Font
        /// 
        /// Font 转 String
        /// 
        /// 
        /// 
        /// Width, Height
        public static string xzString(this System.Drawing.Font src, string defVal = "")
        {
            string ret = defVal;
            try
            {
                System.Drawing.FontConverter converter = new System.Drawing.FontConverter();
                ret = converter.ConvertToString(src);
            }
            catch (Exception)
            {

            }
            return ret;
        }
        /// 
        /// String 转 Font
        /// 
        /// 
        /// 
        public static System.Drawing.Font xzFont(this string src)
        {
            System.Drawing.Font ret = new System.Drawing.Font("宋体", 12.0f);
            try
            {
                System.Drawing.FontConverter converter = new System.Drawing.FontConverter();
                object retO = converter.ConvertFromString(src);
                if (retO != null)
                {
                    ret = (System.Drawing.Font)retO;
                }
            }
            catch (Exception)
            {

            }
            return ret;
        }
        #endregion

        #region System.Drawing.Icon
        /// 
        /// Icon 转 string(base64)
        /// 
        /// 
        /// 
        /// 
        public static string xzBase64String(this System.Drawing.Icon src, string defVal = "")
        {
            string ret = defVal;
            try
            {
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                src.Save(ms);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                return xzBase64Header(Drawing.Imaging.ImageFormat.Icon) + Convert.ToBase64String(arr);
            }
            catch (Exception exp)
            {
                System.Windows.Forms.MessageBox.Show(exp.Message);
            }
            return ret;
        }

        /// 
        /// string(base64) 转 Icon
        /// 
        /// 
        /// 
        public static System.Drawing.Icon xzBase64Icon(this string src)
        {
            System.Drawing.Icon ret = null;
            try
            {
                int index = src.IndexOf(',');
                if (index >= 0)
                {
                    src = src.Substring(index + 1);
                }
                byte[] arr = Convert.FromBase64String(src);
                System.IO.MemoryStream ms = new System.IO.MemoryStream(arr);
                System.Drawing.Icon bmp = new System.Drawing.Icon(ms);
                ms.Close();
                return bmp;
            }
            catch (Exception)
            {

            }
            return ret;
        }
        /// 
        /// 另存图标为
        /// 
        /// 
        /// 
        public static void xzFile(this System.Drawing.Icon src, string path)
        {
            try
            {
                System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.OpenOrCreate);
                src.Save(fs);
                fs.Close();
            }
            catch (Exception)
            {

            }
        }

        #endregion

        #region System.Drawing.Image
        /// 
        /// Image 转 string(base64)
        /// 支持格式:.jpg/.jpeg/.gif/.bmp/.png
        /// 
        /// 
        /// 
        /// 
        public static string xzBase64String(this System.Drawing.Image src, string defVal = "")
        {
            string ret = defVal;
            try
            {
                if (src.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Icon.Guid)
                {
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    src.Save(ms, Drawing.Imaging.ImageFormat.Icon);
                    byte[] arr = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(arr, 0, (int)ms.Length);
                    ms.Close();
                    return xzBase64Header(src.RawFormat) + Convert.ToBase64String(arr);
                }
                else
                {
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    src.Save(ms, src.RawFormat);
                    byte[] arr = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(arr, 0, (int)ms.Length);
                    ms.Close();
                    return xzBase64Header(src.RawFormat) + Convert.ToBase64String(arr);
                }

            }
            catch (Exception exp)
            {
                System.Windows.Forms.MessageBox.Show(exp.Message);
            }
            return ret;
        }
        /// 
        /// string(base64) 转 Image
        /// 
        /// 
        /// 
        public static System.Drawing.Image xzBase64Image(this string src)
        {
            System.Drawing.Image ret = null;
            try
            {
                int index = src.IndexOf(',');
                if (index >= 0)
                {
                    src = src.Substring(index + 1);
                }
                byte[] arr = Convert.FromBase64String(src);
                System.IO.MemoryStream ms = new System.IO.MemoryStream(arr);
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
                ms.Close();
                return bmp;
            }
            catch (Exception)
            {

            }
            return ret;
        }
        #endregion

        #region Base64
        /// 
        /// 根据加载的图像格式,确定 base64 的 head 信息
        /// 
        /// 
        /// 
        private static string xzBase64Header(System.Drawing.Imaging.ImageFormat path)
        {
            string ret = "";
            if (path.Guid == System.Drawing.Imaging.ImageFormat.Bmp.Guid)
            {
                ret = "data:image/bmp;base64,";
            }
            else if (path.Guid == System.Drawing.Imaging.ImageFormat.Png.Guid)
            {
                ret = "data:image/png;base64,";
            }
            else if (path.Guid == System.Drawing.Imaging.ImageFormat.Jpeg.Guid)
            {
                ret = "data:image/jpg;base64,";
            }
            else if (path.Guid == System.Drawing.Imaging.ImageFormat.Gif.Guid)
            {
                ret = "data:image/gif;base64,";
            }
            else if (path.Guid == System.Drawing.Imaging.ImageFormat.Icon.Guid)
            {
                ret = "data:image/ico;base64,";
            }
            else if (path.Guid == System.Drawing.Imaging.ImageFormat.Tiff.Guid)
            {
                ret = "data:image/tif;base64,";
            }
            return ret;
        }
        /// 
        ///  根据原始的后缀名,确定 base64 的 head 信息
        /// 
        /// 
        /// 
        private static string xzBase64Header(string path)
        {
            string ret = "";
            string extension = System.IO.Path.GetExtension(path).ToLower();
            switch (extension)
            {
                case "bmp": ret = "data:image/bmp;base64,"; break;
                case "png": ret = "data:image/png;base64,"; break;
                case "jpeg": ret = "data:image/jpeg;base64,"; break;
                case "jpg": ret = "data:image/jpg;base64,"; break;
                case "gif": ret = "data:image/gif;base64,"; break;
                case "ico": ret = "data:image/ico;base64,"; break;
                case "tif": ret = "data:image/tif;base64,"; break;
                default: break;
            }
            return ret;
        }
        /// 
        /// 更具 base64格式获取图像后缀
        /// 
        /// 
        /// 
        private static string xzBase64ImageFormat(string base64Image)
        {
            string ret = "";
            string s1 = "data:image/";
            string s2 = ";base64,";
            int index1 = base64Image.IndexOf(s1);
            int index2 = base64Image.IndexOf(s2);
            if (index1 < 0 || index2 < 0) return "";

            string extension = base64Image.Substring(index1 + s1.Length, index2 - index1 - s1.Length);

            switch (extension)
            {
                case "bmp": ret = ".bmp"; break;
                case "png": ret = ".png"; break;
                case "jpeg": ret = ".jpeg"; break;
                case "jpg": ret = ".jpg"; break;
                case "gif": ret = ".gif"; break;
                case "ico": ret = ".ico"; break;
                case "tif": ret = ".tif"; break;
                case "tiff": ret = ".tiff"; break;
                default: break;
            }
            return ret;
        }
        /// 
        /// 将base64 格式转化成图像文件
        /// 
        /// data:image/extension;base64,
        /// 
        public static void xzBase64File(this string src, string path)
        {
            try
            {
                string exe = xzBase64ImageFormat(src);
                if (string.IsNullOrEmpty(exe) == false)
                {
                    path = System.IO.Path.GetFileNameWithoutExtension(path) + exe;
                }
                if (exe == ".ico")
                {
                    using (System.Drawing.Icon icon = src.xzBase64Icon())
                    {
                        icon.xzFile(path);
                    }
                }
                else if (exe == ".gif" || exe == ".tif" || exe == ".tiff")
                {
                    using (System.Drawing.Image image = src.xzBase64Image())
                    {
                        image.Save(path);
                    }
                }
                else
                {
                    using (System.Drawing.Image image = src.xzBase64Image())
                    using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image))
                    {
                        bitmap.Save(path);
                    }
                }

            }
            catch (Exception)
            {

            }
        }
        /// 
        /// 将图像文件转化为 Base64 字符串
        /// 
        /// 图像路径
        /// 
        public static string xzBase64String(this string path)
        {
            string ret = "";
            if (System.IO.File.Exists(path) == false)
            {
                return "文件不存在!";
            }
            string extension = System.IO.Path.GetExtension(path);
            switch (extension)
            {
                case "bmp":
                case "png":
                case "jpeg":
                case "jpg":
                case "gif":
                    using (System.Drawing.Image image = System.Drawing.Image.FromFile("1.ico"))
                    {
                        ret = image.xzBase64String();
                    };
                    break;
                case "ico":
                    using (System.Drawing.Icon icon = new System.Drawing.Icon("1.ico"))
                    {
                        ret = icon.xzBase64String();
                    }
                    break;
                default: break;
            }
            return "*请上传小于300KB的.jpg/.jpeg/.gif/.bmp/.png/.ico格式图片,不建议将大图转换。";
        }
        #endregion

        #region System.Drawing.Imaging.ImageFormat
        /// 
        /// ImageFormat 转 String
        /// 
        /// 
        /// 
        /// Width, Height
        public static string xzString(this System.Drawing.Imaging.ImageFormat src, string defVal = "")
        {
            string ret = defVal;
            try
            {
                System.Drawing.ImageFormatConverter converter = new System.Drawing.ImageFormatConverter();
                ret = converter.ConvertToString(src);
            }
            catch (Exception)
            {

            }
            return ret;
        }
        /// 
        /// String 转 ImageFormat
        /// 
        /// 
        /// 
        public static System.Drawing.Imaging.ImageFormat xzImageFormat(this string src)
        {
            System.Drawing.Imaging.ImageFormat ret = System.Drawing.Imaging.ImageFormat.MemoryBmp;
            try
            {
                System.Drawing.ImageFormatConverter converter = new System.Drawing.ImageFormatConverter();
                object retO = converter.ConvertFromString(src);
                if (retO != null)
                {
                    ret = (System.Drawing.Imaging.ImageFormat)retO;
                }
            }
            catch (Exception)
            {

            }
            return ret;
        }
        #endregion

        #region System.Guid
        /// 
        /// Guid 转 String
        /// 
        /// 
        /// 
        /// 
        public static string xzString(this System.Guid src, string defVal = "")
        {
            string ret = defVal;
            try
            {
                ret = src.ToString();
            }
            catch (Exception)
            {

            }
            return ret;
        }
        /// 
        /// String 转 Guid
        /// 
        /// 
        /// 
        public static System.Guid xzGuid(this string src)
        {
            System.Guid ret = System.Guid.Empty;
            //System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(src, @"^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            //if (m.Success)
            {
                try
                {
                    ret = new Guid(src);
                }
                catch (Exception)
                {

                }
            }
            return ret;
        }
        #endregion

        #region System.Windows.Forms.Padding
        /// 
        /// Padding 转 String
        /// 
        /// 
        /// 
        /// 
        public static string xzString(this System.Windows.Forms.Padding src, string defVal = "")
        {
            string ret = defVal;
            try
            {
                System.Windows.Forms.PaddingConverter converter = new System.Windows.Forms.PaddingConverter();
                ret = converter.ConvertToString(src);
            }
            catch (Exception)
            {

            }
            return ret;
        }
        /// 
        /// String 转 Padding
        /// 
        /// 
        /// 
        public static System.Windows.Forms.Padding xzPadding(this string src)
        {
            System.Windows.Forms.Padding ret = System.Windows.Forms.Padding.Empty;
            try
            {
                System.Windows.Forms.PaddingConverter converter = new System.Windows.Forms.PaddingConverter();
                object retO = converter.ConvertFromString(src);
                if (retO != null)
                {
                    ret = (System.Windows.Forms.Padding)retO;
                }
            }
            catch (Exception)
            {

            }
            return ret;
        }
        #endregion

        #region System.Windows.Forms.TreeNode
        /// 
        /// 无效
        /// 
        /// 
        /// 
        /// 
        public static string xzString(this System.Windows.Forms.TreeNode src, string defVal = "")
        {
            string ret = defVal;
            try
            {
                System.Windows.Forms.TreeNodeConverter converter = new System.Windows.Forms.TreeNodeConverter();
                ret = converter.ConvertToInvariantString(src);
            }
            catch (Exception)
            {

            }
            return ret;
        }
        /// 
        /// 无效
        /// 
        /// 
        /// 
        public static System.Windows.Forms.TreeNode xzTreeNode(this string src)
        {
            System.Windows.Forms.TreeNode ret = null;
            try
            {
                System.Windows.Forms.TreeNodeConverter converter = new System.Windows.Forms.TreeNodeConverter();
                object retO = converter.ConvertFromString(src);
                if (retO != null)
                {
                    ret = (System.Windows.Forms.TreeNode)retO;
                }
            }
            catch (Exception)
            {

            }
            return ret;
        }
        #endregion
    }
}

 

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