获得图片的属性 - GetImageAttribute

2017-02-26 15:29:00  访问(1426) 赞(0) 踩(0)

  • 
            /// <summary>
            /// 获得图片的属性 - GetImageAttribute
            /// </summary>
            /// <param name="imageFileName"></param>
            /// <returns></returns>
            public static ImageAttribute GetImageAttribute
                (
                    string imageFileName
                )
            {
                if (imageFileName == null || imageFileName.Length == 0)
                {
                    new Exception("string imageFileName 为空。");
                }
    
                FileInfo info = new FileInfo(imageFileName);
    
                if(!info.Exists)
                    new Exception(imageFileName + " 文件不存在。");
                    
    
                string strExt = info.Extension;
    
    
                if(strExt == null||strExt.Length == 0)
                    new Exception(imageFileName + " 后缀不是图片格式");
                    
    
                strExt = strExt.Trim().ToLower();
                
                SlowX.Functions.Enums.ImageExt.EmImageExt[] 
                    emA 
                    = 
                    Enums.ImageExt.EmArray;
    
                bool isFind = false;
                SlowX.Functions.Enums.ImageExt.EmImageExt 
                    EmImageExtValue 
                    = 
                    Enums.ImageExt.EmImageExt.JPEG;
    
                foreach (SlowX.Functions.Enums.ImageExt.EmImageExt em in emA)
                {
                    if (strExt == "." + em.ToString().ToLower())
                    {
                        EmImageExtValue = em;
                        isFind = true;
                        break;
                    }
                }
    
                if(!isFind)
                    new Exception( imageFileName + " 后缀不是图片格式");
    
                System.Drawing.Image img 
                    = 
                    System.Drawing.Image.FromFile(imageFileName);
    
                int iW = img.Width;
                int iH = img.Height;
    
                // 销毁图片资源,避免io锁 //
                img.Dispose();
    
                return new ImageAttribute(iW, iH, EmImageExtValue);
    
            }
    

  • 
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Reflection;
    
    namespace SlowX.Functions.Enums
    {
        /// <summary>
        /// 图片后缀类型
        /// </summary>
        [Serializable]
        public class ImageExt
        {
    
            #region 枚举 显示表达式
    
            /// <summary>
            /// 显示表达式
            /// </summary>
            public enum EmShowExpress
            {
                
                /// <summary>
                /// 默认
                /// </summary>
                默认 = 1
            }
    
            #endregion 枚举 显示表达式
    
            #region Enum
    
            /// <summary>
            /// 图片后缀类型
            /// </summary>
            public enum EmImageExt
            {
    
                /// <summary>
                /// JPG
                /// </summary>
                JPG = 1,
                /// <summary>
                /// JPEG
                /// </summary>
                JPEG,
                /// <summary>
                /// PNG
                /// </summary>
                PNG,
                /// <summary>
                /// GIF
                /// </summary>
                GIF,
                /// <summary>
                /// BMP
                /// </summary>
                BMP,
                /// <summary>
                /// PSD
                /// </summary>
                PSD,
                /// <summary>
                /// TIF
                /// </summary>
                TIF,
                /// <summary>
                /// TIFF
                /// </summary>
                TIFF
            }
    
            #endregion Enum
    
            /// <summary>
            /// 构造函数
            /// </summary>
            public ImageExt()
            {
    
            }
    
            /// <summary>
            /// 数组
            /// </summary>
            public readonly static EmImageExt[] EmArray = new EmImageExt[]
            {
    EmImageExt.JPG,
    EmImageExt.JPEG,
    EmImageExt.PNG,
    EmImageExt.GIF,
    EmImageExt.BMP,
    EmImageExt.PSD,
    EmImageExt.TIF,
    EmImageExt.TIFF
    
            };
    
    
            /// <summary>
            /// typeof(ImageExt) 当前ImageExt的type值
            /// </summary>
            public readonly static Type curType = typeof(ImageExt);
    
            /// <summary>
            /// typeof(ImageExt).FullName 
            /// </summary>
            public readonly static string curTypeFullName = curType.FullName;
    
    
            /// <summary>
            /// typeof(EmImageExt) 当前枚举EmImageExt的type值
            /// </summary>
            public readonly static Type curEnumType = typeof(EmImageExt);
    
            /// <summary>
            /// typeof(EmImageExt).FullName
            /// </summary>
            public readonly static string curEnumTypeFullName = curEnumType.FullName;
    
    
            /// <summary>
            /// 默认的枚举值
            /// </summary>
            public static readonly EmImageExt defaultEm = EmImageExt.JPG;
    
            /// <summary>
            /// 默认的枚举值
            /// </summary>
            private static Dictionary<EmShowExpress, DataTable> dictionaryDataTableValue = CreateDataTable();
    
            /// <summary>
            /// 获得Text的数组
            /// </summary>
            /// <returns></returns>
            public static string[] GetTextArray()
            {
                EmImageExt[] emA = EmArray;
                int iLen = emA.Length;
                string[] theResult = new string[iLen];
    
                for (int i = 0; i < iLen; ++i)
                {
                    theResult[i] = emA[i].ToString();
                }
    
                return theResult;
            }
    
            /// <summary>
            /// 获得显示名称
            /// </summary>
            /// <param name="em"></param>
            /// <returns></returns>
            public static string GetShowName
                (
                    EmImageExt em
                )
            {
                return em.ToString();
            }
    
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="em"></param>
            /// <param name="curEmShowExpress"></param>
            /// <returns></returns>
            public static string GetShowName
                (
                    EmImageExt em,
                    EmShowExpress curEmShowExpress
                )
            {
       
                    return em.ToString();
    
            }
    
    
            /// <summary>
            /// 创建数据表
            /// </summary>
            /// <returns></returns>
            private static Dictionary<EmShowExpress, DataTable> CreateDataTable()
            {
                Dictionary<EmShowExpress, DataTable> theResult = new Dictionary<EmShowExpress, DataTable>();
    
                theResult.Add(EmShowExpress.默认, CreateDataTableByEmShowExpress(EmShowExpress.默认));
    
    
                return theResult;
            }
    
            /// <summary>
            /// 创建数据表
            /// </summary>
            /// <param name="curEmShowExpress"></param>
            /// <returns></returns>
            private static DataTable CreateDataTableByEmShowExpress(EmShowExpress curEmShowExpress)
            {
                DataTable dt = new DataTable();
    
                dt.Columns.Add("ID", typeof(int));
                dt.Columns.Add("TheName");
    
                EmImageExt[] emA = EmArray;
    
                DataRow dr = null;
    
                foreach (EmImageExt em in emA)
                {
                    dr = dt.NewRow();
    
                    dt.Rows.Add(dr);
    
                    dr["ID"] = (int)em;
                    dr["TheName"] = GetShowName(em, curEmShowExpress);
                }
    
                return dt;
            }
    
            /// <summary>
            /// 获得DataTable
            /// </summary>
            /// <returns></returns>
            public static DataTable ReturnDataTable()
            {
                return dictionaryDataTableValue[EmShowExpress.默认];
            }
    
            /// <summary>
            /// 获得DataTable
            /// </summary>
            /// <returns></returns>
            public static DataTable GetDataTable()
            {
                return dictionaryDataTableValue[EmShowExpress.默认];
            }
    
            /// <summary>
            /// 获得DataTable
            /// </summary>
            /// <param name="curEmShowExpress"></param>
            /// <returns></returns>
            public static DataTable GetDataTable(EmShowExpress curEmShowExpress)
            {
                return dictionaryDataTableValue[curEmShowExpress];
            }
    
            /// <summary>
            /// 获得数据集
            /// </summary>
            /// <param name="fromCache"></param>
            /// <returns></returns>
            public static DataTable GetDataTable(bool fromCache)
            {
                if (fromCache)
                    return dictionaryDataTableValue[EmShowExpress.默认];
                else
                    return CreateDataTableByEmShowExpress(EmShowExpress.默认);
            }
    
    
            /// <summary>
            /// 获得数据集
            /// </summary>
            /// <param name="curEmShowExpress"></param>
            /// <param name="fromCache"></param>
            /// <returns></returns>
            public static DataTable GetDataTable(EmShowExpress curEmShowExpress,bool fromCache)
            {
                if (fromCache)
                    return dictionaryDataTableValue[curEmShowExpress];
                else
                    return CreateDataTableByEmShowExpress(curEmShowExpress);
            }
    
            /// <summary>
            /// 获得枚举
            /// </summary>
            /// <param name="theValue"></param>
            /// <returns></returns>
            public static EmImageExt GetEmByInt(int theValue)
            {
                EmImageExt[] emA = EmArray;
    
                foreach (EmImageExt em in emA)
                {
                    if (theValue == (int)em)
                        return em;
                }
    
                MethodBase methodBaseValue = MethodBase.GetCurrentMethod();
    
                throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> " + "theValue = " + theValue.ToString() + "没有找到对应的枚举值。");
            }
    
    
            /// <summary>
            /// 获得枚举
            /// </summary>
            /// <param name="theValue"></param>
            /// <returns></returns>
            public static EmImageExt GetEmByLong(long theValue)
            {
                EmImageExt[] emA = EmArray;
    
                foreach (EmImageExt em in emA)
                {
                    if (theValue == (long)em)
                        return em;
                }
    
                MethodBase methodBaseValue = MethodBase.GetCurrentMethod();
    
                throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> " + "theValue = " + theValue.ToString() + "没有找到对应的枚举值。");
            }
    
            /// <summary>
            /// 获得枚举
            /// </summary>
            /// <param name="theValue"></param>
            /// <returns></returns>
            public static EmImageExt GetEmByString(string theValue)
            {
                EmImageExt[] emA = EmArray;
    
                foreach (EmImageExt em in emA)
                {
                    if (theValue == ((int)em).ToString())
                        return em;
                }
    
                MethodBase methodBaseValue = MethodBase.GetCurrentMethod();
    
                throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> " + "theValue = " + theValue.ToString() + "没有找到对应的枚举值。");
    
            }
    
            /// <summary>
            /// 获得枚举
            /// </summary>
            /// <param name="theValue"></param>
            /// <returns></returns>
            public static EmImageExt GetEmByObj(object theValue)
            {
                MethodBase methodBaseValue = null;
    
                if (theValue == null)
                {
                    methodBaseValue = MethodBase.GetCurrentMethod();
    
                    throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> " + "theValue为null,没有找到对应的枚举值。");
                }
    
                if (theValue is EmImageExt)
                    return (EmImageExt)theValue;
    
                string strValue = theValue.ToString();
    
                if (strValue.Length == 0)
                {
                    methodBaseValue = MethodBase.GetCurrentMethod();
    
                    throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> " + "theValue为空,没有找到对应的枚举值。");
                }
    
    
                EmImageExt[] emA = EmArray;
    
                foreach (EmImageExt em in emA)
                {
                    if (strValue == ((int)em).ToString())
                        return em;
                }
    
                methodBaseValue = MethodBase.GetCurrentMethod();
    
                throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> " + "theValue = " + strValue.ToString() + "没有找到对应的枚举值。");
            }
    
            /// <summary>
            /// 获得枚举
            /// </summary>
            /// <param name="theValue"></param>
            /// <returns></returns>
            public static EmImageExt GetDefaultEmByInt(int theValue)
            {
                EmImageExt[] emA = EmArray;
    
                foreach (EmImageExt em in emA)
                {
                    if (theValue == (int)em)
                        return em;
                }
    
                return defaultEm;
            }
    
            /// <summary>
            /// 获得枚举
            /// </summary>
            /// <param name="theValue"></param>
            /// <returns></returns>
            public static EmImageExt GetDefaultEmByLong(long theValue)
            {
                EmImageExt[] emA = EmArray;
    
                foreach (EmImageExt em in emA)
                {
                    if (theValue == (long)em)
                        return em;
                }
    
                return defaultEm;
            }
    
    
            /// <summary>
            /// 获得枚举
            /// </summary>
            /// <param name="theValue"></param>
            /// <returns></returns>
            public static EmImageExt GetDefaultEmByString(string theValue)
            {
                EmImageExt[] emA = EmArray;
    
                foreach (EmImageExt em in emA)
                {
                    if (theValue == ((int)em).ToString())
                        return em;
                }
    
                return defaultEm;
            }
    
            /// <summary>
            /// 获得枚举
            /// </summary>
            /// <param name="theValue"></param>
            /// <returns></returns>
            public static EmImageExt GetDefaultEmByObj(object theValue)
            {
                if (theValue == null)
                    return defaultEm;
    
                if (theValue is EmImageExt)
                    return (EmImageExt)theValue;
    
                string strValue = theValue.ToString();
    
                if (strValue.Length == 0)
                    return defaultEm;
    
                EmImageExt[] emA = EmArray;
    
                foreach (EmImageExt em in emA)
                {
                    if (strValue == ((int)em).ToString())
                        return em;
                }
    
                return defaultEm;
            }
    
            /// <summary>
            /// 获得枚举
            /// </summary>
            /// <param name="theValue"></param>
            /// <param name="defaultValue"></param>
            /// <returns></returns>
            public static EmImageExt GetDefaultEmByInt(int theValue, EmImageExt defaultValue)
            {
                EmImageExt[] emA = EmArray;
    
                foreach (EmImageExt em in emA)
                {
                    if (theValue == (int)em)
                        return em;
                }
    
                return defaultValue;
            }
    
            /// <summary>
            /// 获得枚举
            /// </summary>
            /// <param name="theValue"></param>
            /// <param name="defaultValue"></param>
            /// <returns></returns>
            public static EmImageExt GetDefaultEmByLong(long theValue, EmImageExt defaultValue)
            {
                EmImageExt[] emA = EmArray;
    
                foreach (EmImageExt em in emA)
                {
                    if (theValue == (long)em)
                        return em;
                }
    
                return defaultValue;
            }
    
    
            /// <summary>
            /// 获得枚举
            /// </summary>
            /// <param name="theValue"></param>
            /// <param name="defaultValue"></param>
            /// <returns></returns>
            public static EmImageExt GetDefaultEmByString(string theValue, EmImageExt defaultValue)
            {
                EmImageExt[] emA = EmArray;
    
                foreach (EmImageExt em in emA)
                {
                    if (theValue == ((int)em).ToString())
                        return em;
                }
    
                return defaultValue;
            }
    
            /// <summary>
            /// 获得枚举
            /// </summary>
            /// <param name="theValue"></param>
            /// <param name="defaultValue"></param>
            /// <returns></returns>
            public static EmImageExt GetDefaultEmByObj(object theValue, EmImageExt defaultValue)
            {
                if (theValue == null)
                    return defaultValue;
    
                string strValue = theValue.ToString();
    
                if (strValue.Length == 0)
                    return defaultValue;
    
                EmImageExt[] emA = EmArray;
    
                foreach (EmImageExt em in emA)
                {
                    if (strValue == ((int)em).ToString())
                        return em;
                }
    
                return defaultValue;
            }
    
            /// <summary>
            /// 获得 Text 获得 EmImageExt 
            /// </summary>
            /// <param name="theText"></param>
            /// <returns></returns>
            public static EmImageExt GetEmByText(string theText)
            {
                MethodBase methodBaseValue = null;
    
                if (theText == null)
                {
                    methodBaseValue = MethodBase.GetCurrentMethod();
    
                    throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> " + "theText为null,没有找到对应的枚举值。");
                }
    
                if (theText.Length == 0)
                {
                    methodBaseValue = MethodBase.GetCurrentMethod();
    
                    throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> " + "theText为空,没有找到对应的枚举值。");
                }
    
                EmImageExt[] emA = EmArray;
    
                foreach (EmImageExt em in emA)
                {
                    if (theText == (em).ToString())
                        return em;
                }
    
                methodBaseValue = MethodBase.GetCurrentMethod();
    
                throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> " + "theText = " + theText.ToString() + "没有找到对应的枚举值。");
            }
    
    
            /// <summary>
            /// 获得 Text 获得 EmImageExt 
            /// </summary>
            /// <param name="theText"></param>
            /// <param name="defaultValue"></param>
            /// <returns></returns>
            public static EmImageExt GetDefaultEmByText(string theText, EmImageExt defaultValue)
            {
                if (theText == null)
                    return defaultValue;
    
                if (theText.Length == 0)
                    return defaultValue;
    
                EmImageExt[] emA = EmArray;
    
                foreach (EmImageExt em in emA)
                {
                    if (theText == (em).ToString())
                        return em;
                }
    
                return defaultValue;
            }
    
            /// <summary>
            /// 获得枚举
            /// </summary>
            /// <param name="theValue"></param> 
            /// <returns></returns>
            public static EmImageExt GetEmByAllObj(object theValue)
            {
                MethodBase methodBaseValue = null;
    
                if (theValue == null)
                {
                    methodBaseValue = MethodBase.GetCurrentMethod();
    
                    throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> " + "theValue为null,没有找到对应的枚举值。");
                }
    
                if (theValue is EmImageExt)
                    return (EmImageExt)theValue;
    
                string strValue = theValue.ToString();
    
                if (strValue.Length == 0)
                {
                    methodBaseValue = MethodBase.GetCurrentMethod();
    
                    throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> " + "theValue为空,没有找到对应的枚举值。");
                }
    
    
                EmImageExt[] emA = EmArray;
    
                foreach (EmImageExt em in emA)
                {
                    if (strValue == ((int)em).ToString() || strValue == em.ToString())
                        return em;
                }
    
                methodBaseValue = MethodBase.GetCurrentMethod();
    
                throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> " + "theValue = " + strValue.ToString() + "没有找到对应的枚举值。");
            
            }
    
            /// <summary>
            /// 获得枚举
            /// </summary>
            /// <param name="theValue"></param>
            /// <param name="defaultValue"></param>
            /// <returns></returns>
            public static EmImageExt GetDefaultEmByAllObj(object theValue, EmImageExt defaultValue)
            {
                if (theValue == null)
                    return defaultValue;
    
                string strValue = theValue.ToString();
    
                if (strValue.Length == 0)
                    return defaultValue;
    
                EmImageExt[] emA = EmArray;
    
                foreach (EmImageExt em in emA)
                {
                    if (strValue == ((int)em).ToString() || strValue == em.ToString())
                        return em;
                }
    
                return defaultValue;
            }
    
        }
    
            
    }
    
    
    
    
  • using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace SlowX.Functions.Classes
    {
        /// <summary>
        /// 图片属性 - ImageAttribute +
        /// </summary>
        [Serializable]
        public class ImageAttribute
            :
            WidthHeightClass
        {
            /// <summary>
            /// 图片属性
            /// </summary>
            public ImageAttribute()
            {
    
            }
    
          
            /// <summary>
            /// 图片属性
            /// </summary>
            /// <param name="_Width"></param>
            /// <param name="_Height"></param>
            /// <param name="_EmImageExtValue"></param>
            public ImageAttribute
                (
                    int _Width, 
                    int _Height,
                    SlowX.Functions.Enums.ImageExt.EmImageExt _EmImageExtValue
                )
                :
                base(_Width,_Height)
            {
                m_EmImageExtValue = _EmImageExtValue;
            }
    
            /// <summary>
            /// 图片类型
            /// </summary>
            protected SlowX.Functions.Enums.ImageExt.EmImageExt m_EmImageExtValue 
                = 
                Enums.ImageExt.EmImageExt.JPEG;
    
            /// <summary>
            /// 图片类型
            /// </summary>
            public SlowX.Functions.Enums.ImageExt.EmImageExt EmImageExtValue
            {
                get
                {
                    return m_EmImageExtValue;
                }
                set
                {
                    m_EmImageExtValue = value;
                }
            }
    
        }
    }
    
    

标签:获得图片的属性 - GetImageAttribute 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)