文件-- 字節相互轉換(word、圖片、pdf...)


方式一:

  /// <summary>
        /// word文件轉換二進制數據(用於保存數據庫)
        /// </summary>
        /// <param name="wordPath">word文件路徑</param>
        /// <returns>二進制</returns>
        public byte[] WordConvertByte(string wordPath)
        {
            if (!File.Exists(wordPath))
            {
                return null;
            }
            byte[] bytContent = null;
            System.IO.FileStream fs = null;
            System.IO.BinaryReader br = null;
            try
            {
                fs = new FileStream(wordPath, System.IO.FileMode.Open);
                br = new BinaryReader((Stream)fs);
                bytContent = br.ReadBytes((Int32)fs.Length);
                fs.Close();
                br.Close();
            }
            catch
            {
                fs.Close();
                br.Close();
                return null;
            }
            return bytContent;
        }

        /// <summary>
        ///二進制數據轉換為word文件
        /// </summary>
        /// <param name="data">二進制數據</param>
        /// <param name="fileName">word文件名</param>
        /// <returns>word保存的相對路徑</returns>
        public string ByteConvertWord(byte[] data, string fileName)
        {
            if (data == null) return string.Empty;

            FileStream fs = null;
            BinaryWriter br = null;
            try
            {
                fs = new FileStream(fileName, FileMode.OpenOrCreate);
                br = new BinaryWriter(fs);
                br.Write(data, 0, data.Length);
                br.Close();
                fs.Close();
            }
            catch
            {
                br.Close();
                fs.Close();
                return string.Empty;
            }
            return fileName;
        }

方式二:

   // <summary>
        /// 根據圖片路徑返回圖片的字節流byte[]
        /// </summary>
        /// <param name="imagePath">圖片路徑</param>
        /// <returns>返回的字節流</returns>
        private byte[] ImageToByte(string imagePath)
        {
            FileStream files = new FileStream(imagePath, FileMode.Open);
            byte[] imgByte = new byte[files.Length];
            files.Read(imgByte, 0, imgByte.Length);
            files.Close();
            return imgByte;
        }

        /// <summary>
        /// 字節數組生成圖片
        /// </summary>
        /// <param name="Bytes">字節數組</param>
        /// <returns>圖片</returns>
        private Image ByteArrayToImage(byte[] Bytes)
        {
            MemoryStream ms = new MemoryStream(Bytes);
            return Bitmap.FromStream(ms, true);
        }

  程序運行效果:

//將image轉換成byte[]數據
private byte[] ImageToByte(System.Drawing.Image img)
{
    MemoryStream ms = new MemoryStream();
    img.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
    return  ms.ToArray();
}

//將byte[]數據轉換成image
private Image ByteToImage(byte[]  bytes)
{
     MemoryStream ms = new MemoryStream(bytes);
     Image img= Image.FromStream(ms);
     return img;
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM