主頁 > 知識庫 > asp.net(c#)實現(xiàn)從sqlserver存取二進制圖片的代碼

asp.net(c#)實現(xiàn)從sqlserver存取二進制圖片的代碼

熱門標簽:Mysql連接數(shù)設(shè)置 Linux服務(wù)器 銀行業(yè)務(wù) 服務(wù)器配置 電子圍欄 團購網(wǎng)站 科大訊飛語音識別系統(tǒng) 阿里云
下面說說主要實現(xiàn)思路:
1、存取圖片
(1)、將圖片文件轉(zhuǎn)換為二進制并直接存進sql server
復(fù)制代碼 代碼如下:

//UploadHelper.cs
/// summary>
/// 將圖片轉(zhuǎ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)換成二進制碼的圖片保存到數(shù)據(jù)庫中
/// /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)部的庫調(diào)用,插入或者更新照片,這里不透露細節(jié)
return flag;
}
catch (Exception ex)
{
throw ex;
}
}

(2)、在網(wǎng)頁中上傳圖片
復(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)
{
//("上傳的照片格式不對");
}
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)成二進制
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ù)庫取出照片(返回格式Image)
復(fù)制代碼 代碼如下:

//UploadHelper.cs
/// summary>
/// 將二進制轉(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;
}

上面的這個方法取出來之后,如果在winform下,直接給一個PictureBox的Image屬性賦值就可以了??墒莣eb下沒有這么強大的控件,所以,就有了下面的步驟。
2、直接在網(wǎng)頁中以流的形式顯示圖片
(1)、生成圖片流頁面(ImgHelper .aspx)
這個頁面的設(shè)計頁面什么也沒有,類文件如下:
復(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"])) //需要顯示照片的頁面?zhèn)鬟f的員工id
{
int employeeId = int.Parse(Request["employeeId"]);
Employee model = //EmployeeService.GetEmployeeByCondition(new Employee(employeeId))[0] as Employee; //內(nèi)部函數(shù) 查找一個員工 不透漏細節(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);//寫入二進制流
Response.End();
}
catch
{
Response.End();
}
}
}
}

(2)、顯示照片的頁面調(diào)用ImgHelper .aspx
在頁面加載的時候,給圖片控件賦值如下:
復(fù)制代碼 代碼如下:

this.imgPhoto.ImageUrl = "/ImgHelper.aspx?employeeId="+tmpEmployee.Id.ToString(); //imgPhoto是圖片控件

總體來說,一存一取,對于winform是很方便的,但是對于webform,我們需要稍微有一個轉(zhuǎn)化的思路。如果有牛人寫出像winform下那種直接綁定Image對象的控件更好了。上面代碼測試通過,希望對你有幫助。
您可能感興趣的文章:
  • SQL Server中調(diào)用C#類中的方法實例(使用.NET程序集)
  • C#實現(xiàn)連接SQL Server2012數(shù)據(jù)庫并執(zhí)行SQL語句的方法
  • C#連接到sql server2008數(shù)據(jù)庫的實例代碼
  • C#實現(xiàn)的sqlserver操作類實例
  • C#訪問SQLServer增刪改查代碼實例
  • C#獲取所有SQL Server數(shù)據(jù)庫名稱的方法
  • C#如何實現(xiàn)對sql server數(shù)據(jù)庫的增刪改查
  • C#獲取存儲過程返回值和輸出參數(shù)值的方法
  • C# 調(diào)用存儲過程簡單完整的實例代碼
  • C#中如何執(zhí)行存儲過程方法
  • C# Ado.net實現(xiàn)讀取SQLServer數(shù)據(jù)庫存儲過程列表及參數(shù)信息示例

標簽:江蘇 廣元 萍鄉(xiāng) 大理 衡水 棗莊 衢州 蚌埠

巨人網(wǎng)絡(luò)通訊聲明:本文標題《asp.net(c#)實現(xiàn)從sqlserver存取二進制圖片的代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266