下面說(shuō)說(shuō)主要實(shí)現(xiàn)思路:
1、存取圖片
(1)、將圖片文件轉(zhuǎn)換為二進(jìn)制并直接存進(jìn)sql server
復(fù)制代碼 代碼如下:
//UploadHelper.cs
/// summary>
/// 將圖片轉(zhuǎn)化為長(zhǎng)二進(jìn)制
/// /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.Length];
file.Read(byteData, 0, byteData.Length);
file.Close();
return byteData;
}
/// summary>
/// 將轉(zhuǎn)換成二進(jìn)制碼的圖片保存到數(shù)據(jù)庫(kù)中
/// /summary>
public static bool SaveEmployeeImg2Db(Employee model, string path)
{
try
{
Byte[] imgBytes = SetImgToByte(path);
model.Photo = imgBytes;
bool flag=EmployeeService.SaveEmployeePhoto(model); //EmployeeService是公司內(nèi)部的庫(kù)調(diào)用,插入或者更新照片,這里不透露細(xì)節(jié)
return flag;
}
catch (Exception ex)
{
throw ex;
}
}
(2)、在網(wǎng)頁(yè)中上傳圖片
復(fù)制代碼 代碼如下:
/// 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)
{
//("上傳的照片格式不對(duì)");
}
else if(fi.Length > 2* 1024 * 1024)
{
//圖片大于2M,重新處理
}
else
{
string saveFilePath = serverPath + DateTime.Now.ToString("yyyyMMddHHmmss") + fileName;
try
{
//先存圖片到服務(wù)器
this.fuPhoto.PostedFile.SaveAs(saveFilePath);
//轉(zhuǎn)成二進(jìn)制
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)、從數(shù)據(jù)庫(kù)取出照片(返回格式Image)
復(fù)制代碼 代碼如下:
//UploadHelper.cs
/// summary>
/// 將二進(jìn)制轉(zhuǎn)化為圖片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;
}
上面的這個(gè)方法取出來(lái)之后,如果在winform下,直接給一個(gè)PictureBox的Image屬性賦值就可以了??墒莣eb下沒有這么強(qiáng)大的控件,所以,就有了下面的步驟。
2、直接在網(wǎng)頁(yè)中以流的形式顯示圖片
(1)、生成圖片流頁(yè)面(ImgHelper .aspx)
這個(gè)頁(yè)面的設(shè)計(jì)頁(yè)面什么也沒有,類文件如下:
復(fù)制代碼 代碼如下:
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"])) //需要顯示照片的頁(yè)面?zhèn)鬟f的員工id
{
int employeeId = int.Parse(Request["employeeId"]);
Employee model = //EmployeeService.GetEmployeeByCondition(new Employee(employeeId))[0] as Employee; //內(nèi)部函數(shù) 查找一個(gè)員工 不透漏細(xì)節(jié)
try
{
Byte[] byteImg = model.Photo;
Stream stream = new MemoryStream(byteImg);
System.Drawing.Bitmap img =(System.Drawing.Bitmap) System.Drawing.Bitmap.FromStream(stream, false); //轉(zhuǎn)換成Bitmap
Response.Buffer = false;
Response.ContentType = "image/jpg";
Response.AddHeader("Content-Disposition", "attachment;filename=photo.jpg");//照片名稱叫photo.jpg
Response.BinaryWrite(byteImg);//寫入二進(jìn)制流
Response.End();
}
catch
{
Response.End();
}
}
}
}
(2)、顯示照片的頁(yè)面調(diào)用ImgHelper .aspx
在頁(yè)面加載的時(shí)候,給圖片控件賦值如下:
復(fù)制代碼 代碼如下:
this.imgPhoto.ImageUrl = "/ImgHelper.aspx?employeeId="+tmpEmployee.Id.ToString(); //imgPhoto是圖片控件
總體來(lái)說(shuō),一存一取,對(duì)于winform是很方便的,但是對(duì)于webform,我們需要稍微有一個(gè)轉(zhuǎn)化的思路。如果有牛人寫出像winform下那種直接綁定Image對(duì)象的控件更好了。上面代碼測(cè)試通過(guò),希望對(duì)你有幫助。
您可能感興趣的文章:- SQL Server中調(diào)用C#類中的方法實(shí)例(使用.NET程序集)
- C#實(shí)現(xiàn)連接SQL Server2012數(shù)據(jù)庫(kù)并執(zhí)行SQL語(yǔ)句的方法
- C#連接到sql server2008數(shù)據(jù)庫(kù)的實(shí)例代碼
- C#實(shí)現(xiàn)的sqlserver操作類實(shí)例
- C#訪問(wèn)SQLServer增刪改查代碼實(shí)例
- C#獲取所有SQL Server數(shù)據(jù)庫(kù)名稱的方法
- C#如何實(shí)現(xiàn)對(duì)sql server數(shù)據(jù)庫(kù)的增刪改查
- C#獲取存儲(chǔ)過(guò)程返回值和輸出參數(shù)值的方法
- C# 調(diào)用存儲(chǔ)過(guò)程簡(jiǎn)單完整的實(shí)例代碼
- C#中如何執(zhí)行存儲(chǔ)過(guò)程方法
- C# Ado.net實(shí)現(xiàn)讀取SQLServer數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程列表及參數(shù)信息示例