一、圖片轉換二進制
public byte[] GetPictureData() { string path = System.AppDomain.CurrentDomain.BaseDirectory; //獲取程序的基目錄 path += "\\PrintImage\\test001.png"; // 需要轉換爲二進制的圖片地址 FileStream Fs = new FileStream(path, FileMode.Open); // 使用文件流打開,並保存爲byte[] byte[] ByData = new byte[Fs.Length]; Fs.Read(ByData, 0, ByData.Length); Fs.Close(); // 關閉當前流並釋放資源 return ByData; }
二、二進制轉換圖片
public System.Drawing.Image ReturnPhoto(byte[] ByData) { System.IO.MemoryStream ms = new System.IO.MemoryStream(ByData); // 創建內存流 System.Drawing.Image img = System.Drawing.Image.FromStream(ms); // 從指定的內存流創建image ms.Close(); // 關閉當前流並釋放資源 return img; }