下面說說主要實現思路:
1、存取圖片
(1)、將圖片檔案轉換為二進位並直接存進sql server
複製代碼 代碼如下://UploadHelper.cs
/// <summary>
/// 將圖片轉化為長二進位
/// </summary>
/// <param name="photopath"></param>
/// <returns></returns>
public static Byte[] SetImgToByte(string imgPath)
{
FileStream file = new FileStream(imgPath, FileMode.Open, FileAccess.Read);
Byte[] byteData = new Byte;
file.Read(byteData, 0, byteData.Length);
file.Close();
return byteData;
}
/// <summary>
/// 將轉換成二進位碼的圖片儲存到資料庫中
/// </summary>
public static bool SaveEmployeeImg2Db(Employee model, string path)
{
try
{
Byte[] imgBytes = SetImgToByte(path);
model.Photo = imgBytes;
bool flag=EmployeeService.SaveEmployeePhoto(model); //EmployeeService是公司內部的庫調用,插入或者更新照片,這裡不透露細節
return flag;
}
catch (Exception ex)
{
throw ex;
}
}
(2)、在網頁中上傳圖片 複製代碼 代碼如下:/// <summary>
/// 上傳圖片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnUpload_Click(object sender, EventArgs e)
{
string serverPath = Server.MapPath("~/images/");
if (this.fuPhoto.HasFile) //fuPhoto是fileupload控制項
{
string fileName = this.fuPhoto.PostedFile.FileName;
FileInfo fi = new FileInfo(fileName);
string mimeType = this.fuPhoto.PostedFile.ContentType.ToLower();
if (mimeType.IndexOf("image") < 0)
{
//("上傳的照片格式不對");
}
else if(fi.Length > 2* 1024 * 1024)
{
//圖片大於2M,重新處理
}
else
{
string saveFilePath = serverPath + DateTime.Now.ToString("yyyyMMddHHmmss") + fileName;
try
{
//先存圖片到伺服器
this.fuPhoto.PostedFile.SaveAs(saveFilePath);
//轉成二進位
Employee model = new Employee(int.Parse(id)); //id是EmployeeId,這裡是類比欄位
bool flag = UploadHelper.SaveEmployeeImg2Db(model, saveFilePath);
}
catch
{
//("照片上傳失敗");
}
finally
{
//最後刪掉該圖片
if (System.IO.File.Exists(saveFilePath))
{
System.IO.File.Delete(saveFilePath);
}
}
}
}
else
{
//("全選擇要上傳的照片");
}
}
(3)、從資料庫取出照片(返回格式Image) 複製代碼 代碼如下://UploadHelper.cs
/// <summary>
/// 將二進位轉化為圖片Image
/// </summary>
/// <param name="photopath"></param>
/// <returns></returns>
public static System.Drawing.Image GetImgFromByte(Employee model)
{
System.Drawing.Image img = null;
try
{
Stream stream = new MemoryStream(model.Photo);
img = System.Drawing.Image.FromStream(stream,false);
}
catch
{
img = null;
}
return img;
}
上面的這個方法取出來之後,如果在winform下,直接給一個PictureBox的Image屬性賦值就可以了。可是web下沒有這麼強大的控制項,所以,就有了下面的步驟。
2、直接在網頁中以流的形式顯示圖片
(1)、產生圖片流頁面(ImgHelper .aspx)
這個頁面的設計頁面什麼也沒有,類檔案如下: 複製代碼 代碼如下:using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.IO;
/// <summary>
/// 圖片輔助類
/// </summary>
public partial class ImgHelper : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request["employeeId"])) //需要顯示照片的頁面傳遞的員工id
{
int employeeId = int.Parse(Request["employeeId"]);
Employee model = //EmployeeService.GetEmployeeByCondition(new Employee(employeeId))[0] as Employee; //內建函式 尋找一個員工 不透漏細節
try
{
Byte[] byteImg = model.Photo;
Stream stream = new MemoryStream(byteImg);
System.Drawing.Bitmap img =(System.Drawing.Bitmap) System.Drawing.Bitmap.FromStream(stream, false); //轉換成Bitmap
Response.Buffer = false;
Response.ContentType = "image/jpg";
Response.AddHeader("Content-Disposition", "attachment;filename=photo.jpg");//照片名稱叫photo.jpg
Response.BinaryWrite(byteImg);//寫入二進位流
Response.End();
}
catch
{
Response.End();
}
}
}
}
(2)、顯示照片的頁面調用ImgHelper .aspx
在頁面載入的時候,給圖片控制項賦值如下: 複製代碼 代碼如下:this.imgPhoto.ImageUrl = "/ImgHelper.aspx?employeeId="+tmpEmployee.Id.ToString(); //imgPhoto是圖片控制項
總體來說,一存一取,對於winform是很方便的,但是對於webform,我們需要稍微有一個轉化的思路。如果有牛人寫出像winform下那種直接綁定Image對象的控制項更好了。上面代碼測試通過,希望對你有協助。