SizeConversion.cs 4.9 KB
using System;
using System.Collections.Generic;
using System.Drawing;

namespace Model
{
    public static class ObjConversion
    {
        private const float MULTIPLE = 100f;

        public static SizeF MmToPx(SizeF size)
        {
            return new SizeF(size.Width / 25.4f * MULTIPLE, size.Height / 25.4f * MULTIPLE);
        }

        public static RectangleF MmToPx(RectangleF rect)
        {
            float x = rect.X / 25.4f * MULTIPLE;
            float y = rect.Y / 25.4f * MULTIPLE;
            float w = rect.Width / 25.4f * MULTIPLE;
            float h = rect.Height / 25.4f * MULTIPLE;
            return new RectangleF(x, y, w, h);
        }

        public static RectangleF PxToMm(RectangleF rect)
        {
            float x = rect.X / MULTIPLE * 25.4f;
            float y = rect.Y / MULTIPLE * 25.4f;
            float w = rect.Width / MULTIPLE * 25.4f;
            float h = rect.Height / MULTIPLE * 25.4f;
            return new RectangleF(x, y, w, h);
        }

        public static SizeF PxToMm(Size size)
        {
            return new SizeF(Convert.ToSingle(size.Width / MULTIPLE * 25.4f), Convert.ToSingle(size.Height / MULTIPLE * 25.4f));
        }

        public static PrintLabelFieldType StrToFieldType(string s)
        {
            PrintLabelFieldType type = (PrintLabelFieldType)Enum.Parse(typeof(PrintLabelFieldType), s);
            return type;
        }

        public static RectangleF StrToRectF(string s)
        {
            RectangleF rect = new();
            string[] arr = s.Split(',');
            if (arr.Length == 4)
            {
                if (float.TryParse(arr[0], out float result))
                    rect.X = result;
                if (float.TryParse(arr[1], out result))
                    rect.Y = result;
                if (float.TryParse(arr[2], out result))
                    rect.Width = result;
                if (float.TryParse(arr[3], out result))
                    rect.Height = result;
            }
            return rect;
        }

        public static string RectFToStr(RectangleF rect)
        {
            return string.Format("{0},{1},{2},{3}", rect.X, rect.Y, rect.Width, rect.Height);
        }

        public static Font StrToFont(string s)
        {
            string[] t = s.Split(',');
            float emSize = Convert.ToSingle(t[1]);
            FontStyle style = FontStyle.Regular;
            if (t[2] == "B") style |= FontStyle.Bold;
            if (t[3] == "I") style |= FontStyle.Italic;
            Font font = new(t[0], emSize, style);
            return font;
        }

        public static string FontToStr(Font font)
        {
            string[] s = new string[4];
            s[0] = font.Name;
            s[1] = font.Size.ToString();
            if (font.Bold) s[2] = "B";
            if (font.Italic) s[3] = "I";
            return string.Join(",", s);
        }

        public static SizeF StrToSizeF(string s)
        {
            SizeF size = new();
            string[] arr = s.Split(',');
            if (arr.Length == 2)
            {
                if (float.TryParse(arr[0], out float result))
                    size.Width = result;
                if (float.TryParse(arr[1], out result))
                    size.Height = result;
            }
            return size;
        }

        public static string SizeFToStr(SizeF size)
        {
            return string.Format("{0},{1}", size.Width, size.Height);
        }

        public static string StrRemoveKey(string s)
        {
            string rtn = "";
            bool into = false;

            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == '[')
                    into = true;
                else if (s[i] == ']')
                    into = false;
                else if (!into)
                    rtn += s[i];
            }
            return rtn;
        }

        public static string[] StrGetKey(string s)
        {
            string tt = "";
            List<string> rtn = new();
            bool into = false;

            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == '[')
                {
                    into = true;
                    tt = "";
                }
                else if (s[i] == ']')
                {
                    into = false;
                    rtn.Add(tt);
                }
                else if (into)
                {
                    tt += s[i];
                }
            }
            return rtn.ToArray();

        }


        public static Bitmap ReadImageFile(string path)
        {
            if (!System.IO.File.Exists(path))
                return null;

            using System.IO.FileStream file = new(path, System.IO.FileMode.Open);
            byte[] b = new byte[file.Length];
            file.Read(b, 0, b.Length);
            System.IO.MemoryStream stream = new(b);
            return new Bitmap(Image.FromStream(stream));

        }

    }
}