判断文件是否为图片

   #region Judge the Uploaded file is picrute or not
        //Judge the Uploaded file is picture or not
        /// <summary>
        /// Judge the Uploaded file is picrute or not
        /// </summary>
        /// <param name="filePath">filePath</param>
        /// <returns></returns>
        public static bool IsImg(string filePath)
        {
            bool v = false;
            if (File.Exists(filePath))
            {
                try
                {
                    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    BinaryReader reader = new BinaryReader(fs);
                    string fileClass;
                    byte buffer;
                    byte[] b = new byte[2];
                    buffer = reader.ReadByte();
                    b[0] = buffer;
                    fileClass = buffer.ToString();
                    buffer = reader.ReadByte();
                    b[1] = buffer;
                    fileClass += buffer.ToString();


                    reader.Close();
                    fs.Close();
                    //255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar   
                    if (fileClass == "255216" || fileClass == "7173" || fileClass == "6677" || fileClass == "13780")
                    {
                        v = true;
                    }
                    else
                    {
                        v = false;
                    }
                }
                catch
                {
                    v = false;
                }
            }
            else
            {
                v = false;
            }
            return v;
        }
        #endregion

posted @ 2010-04-24 22:51  think_fish  Views(604)  Comments(0Edit  收藏  举报