欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#在sql中存取圖片image示例

 更新時間:2014年03月12日 16:03:36   作者:  
這篇文章主要介紹了c#在sql中存取圖片image示例,需要的朋友可以參考下

(1)控制臺應(yīng)用程序下演示插入圖片

復(fù)制代碼 代碼如下:

public void InsertIMG()
{
//將需要存儲的圖片讀取為數(shù)據(jù)流
FileStream fs = new FileStream(@"E:\c.jpg", FileMode.Open,FileAccess.Read);
Byte[] btye2 = new byte[fs.Length];
fs.Read(btye2 , 0, Convert.ToInt32(fs.Length));
fs.Close();

using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insert into T_Img(imgfile) values(@imgfile)";
SqlParameter par = new SqlParameter("@imgfile", SqlDbType.Image);
par.Value = bt;
cmd.Parameters.Add(par);

int t=(int)(cmd.ExecuteNonQuery());
if (t > 0)
{
Console.WriteLine("插入成功");
}
conn.Close();
}
}

(2)控制臺應(yīng)用程序下讀出并生成圖片到物理位置

復(fù)制代碼 代碼如下:

public void Read()
{
byte[] MyData = new byte[0];
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MyData = (byte[])sdr["ImgFile"];//讀取第一個圖片的位流
int ArraySize= MyData.GetUpperBound(0);//獲得數(shù)據(jù)庫中存儲的位流數(shù)組的維度上限,用作讀取流的上限

FileStream fs = new FileStream(@"c:\00.jpg", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0, ArraySize);
fs.Close();   //-- 寫入到c:\00.jpg。
conn.Close();
Console.WriteLine("讀取成功");//查看硬盤上的文件
}
}

(3)Web下picshow.aspx頁將圖片讀取出來并寫入到瀏覽器上呈現(xiàn)

復(fù)制代碼 代碼如下:

public void Read()
{
byte[] MyData = new byte[0];
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MyData = (byte[])sdr["ImgFile"];
Response.ContentType = "image/gif";
Response.BinaryWrite(MyData);
conn.Close();
Response.Write("讀取成功");
}

(4)在web中可以如上picshow.aspx頁面讀取并顯示圖片,而真正引用該圖片時如下示例

復(fù)制代碼 代碼如下:

<img src="picshow.aspx" width="500" height="300" />

 (5)Winform下將圖片寫入到sql數(shù)據(jù)庫image類型字段中的方法和以上方法基本一致,僅區(qū)別于可以利用多個對話框來幫助選取存儲圖片等,各個屬性可以方便的利用上

(6)Winform下讀取圖片在picturebox控件中顯示出來

方法一:利用MemoryStream 和System.Drawing.Image

復(fù)制代碼 代碼如下:

public void Read()
{
byte[] MyData = new byte[0];
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MyData = (byte[])sdr["ImgFile"];

MemoryStream mystream = new MemoryStream(MyData);
//用指定的數(shù)據(jù)流來創(chuàng)建一個image圖片
System.Drawing.Image img = System.Drawing.Image.FromStream(mystream, true);

System.Windows.Forms.PictureBox picbox = new PictureBox();
picbox.Image = img;
picbox.Left = 30;
picbox.Top = 80;
picbox.Width = 800;
picbox.Height = 500;
this.Controls.Add(picbox);

mystream.Close();
conn.Close();
}
}

方法二:將流直接讀取成圖片并寫入到物理位置,然后再行利用該圖片呈現(xiàn)

復(fù)制代碼 代碼如下:

void Read()
{
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();

byte[] Image_img = (byte[])sdr["ImgFile"];
if (Image_img.Length == 0)
{
return;
}
int filelength = Image_img.Length;
string imageName = "1.jpg";
string myUrl = Environment.CurrentDirectory + "\\" + imageName;
FileStream fs = new FileStream(myUrl, FileMode.OpenOrCreate,FileAccess.Write);
BinaryWriter BW = new BinaryWriter(fs);
BW.BaseStream.Write(Image_img, 0, filelength);
BW.Flush();
BW.Close();
System.Windows.Forms.PictureBox picbox = new PictureBox();

//為picbox添加圖片方法一
//picbox.ImageLocation = myUrl;
//picbox.Width = 800;
//picbox.Height = 300;

 

//為picbox添加圖片方法二
Bitmap bitmap = new Bitmap(myUrl);
picbox.Width = 100;//bitmap.Width;
picbox.Height = 80;//bitmap.Height;
picbox.Image = (Image)bitmap;
picbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
picbox.Left = 20;
picbox.Top = 30;

this.Controls.Add(picbox);
conn.Close();

}
}

相關(guān)文章

  • c# 備忘錄模式

    c# 備忘錄模式

    備忘錄模式:在不破壞封裝的前提下,捕獲一個對象的內(nèi)部狀態(tài),并在這個對象之外的地方保存這個狀態(tài),這樣以后就可將該對象恢復(fù)到原來保存的狀態(tài)了
    2012-10-10
  • C# 實現(xiàn)dataGridView選中一行右鍵出現(xiàn)菜單的示例代碼

    C# 實現(xiàn)dataGridView選中一行右鍵出現(xiàn)菜單的示例代碼

    這篇文章主要介紹了C# 實現(xiàn)dataGridView選中一行右鍵出現(xiàn)菜單,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • C#使用IronPython調(diào)用Python

    C#使用IronPython調(diào)用Python

    這篇文章主要給大家介紹了關(guān)于C#使用IronPython調(diào)用Python的相關(guān)資料, c#利用IronPython調(diào)用python的過程中總會遇到種種問題,這里給大家總結(jié)下,需要的朋友可以參考下
    2023-07-07
  • Unity3D快速入門教程

    Unity3D快速入門教程

    本文講述了Unity3D如何快速入門,包含Unity3D游戲引擎介紹,學(xué)習(xí)歷程和Unity3D快速入門的途徑,通過該篇文章的講解,希望能夠讓你更好的去學(xué)習(xí)Unity3D引擎
    2021-06-06
  • C#實現(xiàn)QQ聊天窗口

    C#實現(xiàn)QQ聊天窗口

    這篇文章主要為大家詳細(xì)介紹了C#實現(xiàn)QQ聊天窗口,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C#實現(xiàn)winform用子窗體刷新父窗體及子窗體改變父窗體控件值的方法

    C#實現(xiàn)winform用子窗體刷新父窗體及子窗體改變父窗體控件值的方法

    這篇文章主要介紹了C#實現(xiàn)winform用子窗體刷新父窗體及子窗體改變父窗體控件值的方法,涉及C#窗體交互的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • WPF如何自定義ProgressBar滾動條樣式

    WPF如何自定義ProgressBar滾動條樣式

    這篇文章主要給大家介紹了關(guān)于WPF如何自定義ProgressBar滾動條樣式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用WPF具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • C#學(xué)習(xí)筆記- 隨機(jī)函數(shù)Random()的用法詳解

    C#學(xué)習(xí)筆記- 隨機(jī)函數(shù)Random()的用法詳解

    下面小編就為大家?guī)硪黄狢#學(xué)習(xí)筆記- 隨機(jī)函數(shù)Random()的用法詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08
  • C#中Timer定時器類的簡單使用

    C#中Timer定時器類的簡單使用

    定時器就是經(jīng)過固定時間,執(zhí)行固定任務(wù),本文主要介紹了C#中Timer定時器類的簡單使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • C# 以MDF文件鏈接數(shù)據(jù)庫的示例代碼

    C# 以MDF文件鏈接數(shù)據(jù)庫的示例代碼

    本篇文章主要介紹了C# 以MDF文件鏈接數(shù)據(jù)庫的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09

最新評論