參數是圖片路徑:返回Byte[]類型:
//參數是圖片的路徑 public byte[] GetPictureData(string imagePath) { FileStream fs = new FileStream(imagePath, FileMode.Open); byte[] byteData = new byte[fs.Length]; fs.Read(byteData, 0, byteData.Length); fs.Close(); return byteData; }
參數類型是Image對象,返回Byte[]類型
//將Image轉換成流數據,並保存為byte[] public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto) { MemoryStream mstream = new MemoryStream(); imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp); byte[] byData = new Byte[mstream.Length]; mstream.Position = 0; mstream.Read(byData, 0, byData.Length); mstream.Close(); return byData; }
參數是Byte[]類型,返回值是Image對象
public System.Drawing.Image ReturnPhoto(byte[] streamByte) { System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte); System.Drawing.Image img = System.Drawing.Image.FromStream(ms); return img; }
參數是Byte[] 類型,沒有返回值(ASP.NET輸出圖片)
public void WritePhoto(byte[] streamByte) { // Response.ContentType 的默認值為默認值為“text/html” Response.ContentType = "image/GIF"; //圖片輸出的類型有: image/GIF image/JPEG Response.BinaryWrite(streamByte); }