天天看點

轉 詳解C#資料庫存取圖檔三大方式

2010年09月25日12:45 來源:蘇飛的部落格 作者:蘇飛 編輯:胡銘娅 評論:0條 本文Tag: c# 資料庫 【IT168 評論】第一種方式 檔案夾與資料庫配合   近來做了不少關于這塊的功能 ,随着網絡的飛速發展,網絡存取圖檔已不再是神話,而成為了一種時尚,如果是你 是用Asp.net開發的話,可能更多的人會考慮使用資料庫存儲圖檔的路經,而在檔案夾是存儲圖檔的方式。這種方式主要的方法有兩個一個就是怎麼樣讀取圖檔,怎麼樣存儲圖上,讀取的話我就不多說的這個是最簡單的了,隻要大家把位址=給存儲圖檔的對象就行了,在取的時候一般要使用相對位址也就是“~” 如下所示 ImageUrl="../CardDeal/SellCardZhi.jpg' ImageUrl="~/CardDeal/SellCardZhi.jpg'   當然這前面要加上你自己的圖檔所在伺服器的檔案夾的名稱   我們來看是一下是怎麼存儲的吧,我常用的一個方法是這樣的 /// /// 上傳圖檔 /// ///

FileUpload對象 ///

圖檔要放到的目錄名稱 /// 如果FileUpload不為空則傳回上傳後的圖檔位置,否則傳回為空字元 public static string uploadImage(FileUpload FUSShopURL, string UpladURL) { if (FUSShopURL.HasFile) { //擷取目前的時間,一當作圖檔的名字 string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString(); //擷取圖檔的擴充名 string Extent = System.IO.Path.GetExtension(FUSShopURL.PostedFile.FileName); //重命名圖檔 fileName += Extent; //設定上傳圖檔儲存的檔案夾 string dir = System.Web.HttpContext.Current.Server.MapPath(UpladURL); //指定圖檔的路徑及檔案名 string path = dir + "//" + fileName; //把上傳得圖檔儲存到指定的檔案加中 FUSShopURL.PostedFile.SaveAs(path); return fileName; } else { return ""; } }   這個方法是與FileUpload控件 一起使用的,方法很簡單大家一看就明白了。   方法傳回的就是一個相對的路經可以直接存儲的資料裡,然後從前台調用就可以了   第二種方式 直接把圖檔的Base64String碼進行存取   這種方法很友善,直接轉化一下就行了,不需要書寫很麻煩的路經問題   先看一下是怎麼存儲到資料庫的吧 //選擇圖檔 private void button1_Click(object sender, EventArgs e) { OpenFileDialog openfile = new OpenFileDialog(); openfile.Title = "請選擇用戶端longin的圖檔"; openfile.Filter = "Login圖檔(*.jpg;*.bmp;*png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*"; if (DialogResult.OK == openfile.ShowDialog()) { try { Bitmap bmp = new Bitmap(openfile.FileName); pictureBox1.Image = bmp; pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); byte[] arr = new byte[ms.Length]; ms.Position = 0; ms.Read(arr, 0, (int)ms.Length); ms.Close(); //直接返這個值放到資料就行了 pic = Convert.ToBase64String(arr); } catch { } } }   讀取的方法也很簡單, pic就是我們得到的圖檔字元串隻要我們存儲到資料庫裡,從下面的方法裡讀取就可以了   需要注意的地方我都加的有注釋 //加載圖檔 private void Form1_Load(object sender, EventArgs e) { try { // pic=........這一句換成從資料庫裡讀取就可以了 //判斷是否為空,為空時的不執行 if (!string.IsNullOrEmpty(pic)) { //直接返Base64碼轉成數組 byte[] imageBytes = Convert.FromBase64String(pic); //讀入MemoryStream對象 MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length); memoryStream.Write(imageBytes, 0, imageBytes.Length); //轉成圖檔 Image image = Image.FromStream(memoryStream); //memoryStream.Close();//不要加上這一句否則就不對了 // 将圖檔放置在 PictureBox 中 this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; this.pictureBox1.Image = image; } } catch { } }  大家看一下效果吧   在這裡我們隻要單擊選擇圖檔直接就可以更換。這些很簡單但是我個人感覺還是很常用的,而且網上關于這塊的例子着實不少,不過真正能幫上忙的還真不多,因為我們的好幾個項目裡用到了這些方法,或多或少的還是有些員工不怎麼會, 在這裡貼一貼友善新手檢視吧。呵呵   下面的本例子的所有代碼 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Threading; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string pic = ""; //加載圖檔 private void Form1_Load(object sender, EventArgs e) { try { if (!string.IsNullOrEmpty(pic)) { byte[] imageBytes = Convert.FromBase64String(pic); MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length); memoryStream.Write(imageBytes, 0, imageBytes.Length); Image image = Image.FromStream(memoryStream); // 将圖檔放置在 PictureBox 中 this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; this.pictureBox1.Image = image; } } catch { } } //選擇圖檔 private void button1_Click(object sender, EventArgs e) { OpenFileDialog openfile = new OpenFileDialog(); openfile.Title = "請選擇用戶端longin的圖檔"; openfile.Filter = "Login圖檔(*.jpg;*.bmp;*png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*"; if (DialogResult.OK == openfile.ShowDialog()) { try { Bitmap bmp = new Bitmap(openfile.FileName); pictureBox1.Image = bmp; pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); byte[] arr = new byte[ms.Length]; ms.Position = 0; ms.Read(arr, 0, (int)ms.Length); ms.Close(); pic = Convert.ToBase64String(arr); } catch { } } } } }