當前位置:首頁 » 操作系統 » c讀取資料庫中圖片

c讀取資料庫中圖片

發布時間: 2025-02-28 09:52:50

A. 資料庫以img存儲,如何讀取圖片

直接使用企業管理器好像沒有辦法操作吧,通過軟體或自己做個小軟體讀取。

#region //讀取資料庫中圖片到內存.並顯示
public void LoadToMemoryAndDisable(string serverAdress, string database)
{
//讀取資料庫中圖片到內存.並顯示
sqlConnection conn = new SqlConnection("server=" + serverAdress + ";integrated security = sspi;database = " + database);
SqlCommand cmd = new SqlCommand("select * from imgtable where imgname like '%bmp%'", conn);
conn.Open();
SqlDataReader dr;
try
{
dr = cmd.ExecuteReader();
dr.Read();
System.Data.SqlTypes.SqlBinary sb = dr.GetSqlBinary(2);
//或byte[] imageData = (byte[])dr[2];
MemoryStream ms = new MemoryStream(sb.Value);//在內存中操作圖片數據
Bitmap bmp = new Bitmap(Bitmap.FromStream(ms));
this.pictureBox1.Image = bmp;
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
#endregion

B. C#中從資料庫下載文件

首先要把資料庫裡面對應的那個欄位讀出來 然後進行處理
下面的例子是我從資料庫讀取圖片的
OracleConnection conn = new OracleConnection(connstring);
conn.Open();
label1.Text = "開始時間:" + DateTime.Now;
string begin = "資料庫連接正常,開始時間" + DateTime.Now;
SetInfo("", begin);
string SQL = "select ID from bs_buylicense_attach t where t.file_size>204800 order by t.file_size asc"; //rownum<100 and
string ConnString = SQL;
OracleDataAdapter sda = new OracleDataAdapter(ConnString, conn);
DataTable table = new DataTable();
sda.Fill(table);
List<object> lst = ModelConvertHelper.DtCovertIList(table);
foreach (object o in lst)
{
string path = "C:\\1.png"; ;
string lst_ID = o.ToString(); //查詢返回來對應的數據的ID
string filter = "select * from bs_buylicense_attach where ID='" + lst_ID + "'";
OracleCommand cmd = new OracleCommand(filter, conn);
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
if (reader["DATA"] != DBNull.Value)
{
string ID = "數據ID對應值ID " + reader["ID"].ToString();
string length = "原文件大小" + reader["FILE_SIZE"].ToString();
SetInfo(ID, length);
System.IO.MemoryStream stream = new System.IO.MemoryStream((byte[])reader["DATA"]);//把照片讀到MemoryStream里
Image ImageBlob = Image.FromStream(stream, true); //從資料庫中讀到的圖片
int height = ImageBlob.Height; //原來圖片的高度
int width = ImageBlob.Width;//原來圖片的寬度
//開始進行圖片的壓縮
GetPicThumbnail1(ImageBlob, path, height, width, 60);
Image SaveImage = im; //得到返回來壓縮之後的圖片
byte[] buffer = imageToByteArray(SaveImage);
string UpdateSql = "update bs_buylicense_attach set DATA =:image where ID='" + lst_ID + "'";
OracleCommand cd = new OracleCommand();
cd = new OracleCommand(UpdateSql, conn);
cd.CommandText = UpdateSql;
cd.Parameters.Add("image", System.Data.OracleClient.OracleType.Blob, buffer.Length).Value = buffer;
cd.ExecuteNonQuery();
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
string YAsuo = Convert.ToString(file.Length);
SetInfo("壓縮為:", YAsuo);
stream.Close();
stream.Dispose();
file.Dispose();
file.Close();
File.Delete(@path);
}
}
}
conn.Close();
string end = "所有數據執行完畢關閉資料庫鏈接!" + DateTime.Now;
SetInfo("", end);
SaveLog();
label2.Text = "結束時間:" + DateTime.Now;
}

C. C#做c/s開發,是怎麼讀取遠程主機的圖像文件的

圖片都存在伺服器上, cs程序端也是通過URL形式訪問圖片,如System.Net.WebClient類可以讀取遠程圖片, 本質上和BS結構一樣,只不過BS結構是瀏覽器幫你實現讀取遠程圖片這一步。

當然,伺服器端得架設一個web站點提供服務。

D. C#怎樣讀取資料庫中Image類型在線等!!

存進去了就可以用流文件讀出來 然後繪制到屏幕上.
class MyBitmap
{
private int width;//點陣圖的寬

public int Width
{
get { return width; }
set { width = value; }
}

private int height;//點陣圖的高

public int Height
{
get { return height; }
set { height = value; }
}

private int postColor;//顏色深度

public int PostColor
{
get { return postColor; }
set { postColor = value; }
}

private Bitmap mBitmap;//點陣圖內置對象,引用系統內置Bitmap

public Bitmap MBitmap
{
get { return mBitmap; }
set { mBitmap = value; }
}

/// <summary>
/// 構造方法,構造一個Bitmap類
/// </summary>
/// <param name="fileurl">文件路徑,包含文件名.</param>
public MyBitmap(string fileurl)
{
FileStream fs = new FileStream(fileurl, FileMode.Open);//引用文件操作流
fs.Seek(18, 0);//將流的初始值設為19,即跳過18位.
int wbmp1 = fs.ReadByte();//讀取寬的第一位
int wbmp2 = fs.ReadByte();//讀取寬的第二位
int wbmp3 = fs.ReadByte();//讀取寬的第三位
fs.ReadByte();//第四位在這里用不到.
int hbmp1 = fs.ReadByte();//讀取高的第一位
int hbmp2 = fs.ReadByte();//讀取高的第二位
int hbmp3 = fs.ReadByte();//讀取高的第三位,第四位依然用不到.
Width = wbmp1 | wbmp2 << 8 | wbmp3 << 16;//位移運算.得到三個位組成的一個int類型的變數,即點陣圖的寬.
Height = hbmp1 | hbmp2 << 8 | hbmp3 << 16;//位移運算.得到三個位組成的一個int類型的變數,即點陣圖的高.
//取得顏色深度,即為多少位的圖.在文件頭的29位.
fs.Seek(28, 0);//跳過28位.
postColor = fs.ReadByte();//讀取第29位,即顏色深度.
MBitmap = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);//實例化Bitmap類
fs.Seek(54, 0);//跳過文件頭,即從第55位開始.
for (int Ycount = 0; Ycount < MBitmap.Height; Ycount++)//雙層循環,遍歷bmp數組,分別調用SetPixel方法
{
for (int Xcount = 0; Xcount < MBitmap.Width; Xcount++)
{
int a = fs.ReadByte();
int b = fs.ReadByte();
int c = fs.ReadByte();
int color = a | b << 8 | c << 16;
//因為行序是顛倒的,所以要讓它倒回來,需要用行高減去它當前的行數再減一,防止越界.
MBitmap.SetPixel(Xcount, MBitmap.Height - Ycount - 1, Color.FromArgb(color));
}
}
fs.Close();//關閉文件流
}

/// <summary>
/// 繪制點陣圖
/// </summary>
/// <param name="g">Graphics對象g</param>
public void Draw(Graphics g,Bitmap bmp)
{
g.DrawImage(bmp, 0, 0);//繪制bmp圖
}

public Bitmap SmallBitmap(Bitmap b)
{
int w = b.Width / 2;
int h = b.Height / 2;
Bitmap bmp = new Bitmap(b, w, h);
for (int Ycount = 0; Ycount < h; Ycount++)
{
for (int Xcount = 0; Xcount < w; Xcount++)
{
Color c1 = b.GetPixel(Xcount * 2, Ycount * 2);
Color c2 = b.GetPixel(Xcount * 2, Ycount * 2 + 1);
Color c3 = b.GetPixel(Xcount * 2 + 1, Ycount * 2);
Color c4 = b.GetPixel(Xcount * 2 + 1, Ycount * 2 + 1);
int c1r = (16711680 & c1.ToArgb()) >> 16;
int c1g = (65280 & c1.ToArgb()) >> 8;
int c1b = (255 & c1.ToArgb());

int c2r = (16711680 & c2.ToArgb()) >> 16;
int c2g = (65280 & c2.ToArgb()) >> 8;
int c2b = (255 & c2.ToArgb());

int c3r = (16711680 & c3.ToArgb()) >> 16;
int c3g = (65280 & c3.ToArgb()) >> 8;
int c3b = (255 & c3.ToArgb());

int c4r = (16711680 & c4.ToArgb()) >> 16;
int c4g = (65280 & c4.ToArgb()) >> 8;
int c4b = (255 & c4.ToArgb());

int cr = (c1r + c2r + c3r + c4r) / 4;
int cg = (c1g + c2g + c3g + c4g) / 4;
int cb = (c1b + c2b + c3b + c4b) / 4;

bmp.SetPixel(Xcount, Ycount, Color.FromArgb((cr == 255 && cg == 0 && cb == 255) ? 0 : 255, cr, cg, cb));
}
}
return bmp;
}
}

這里有一段我寫的對bmp類型圖片的繪制,你可以看下

E. C#winform 中上傳圖片保存到資料庫中

就是2中方法:
1:上傳圖片的相對路徑到資料庫中相應欄位里,讀取顯示時,將控制項(假設用的是Image控制項)的ImageUrl屬性指向該相對路徑即可。

2:將圖片以二進制流的方式整體上傳到資料庫里,讀取顯示時,以二進制流的方式整體讀出。這種方法稍微麻煩一點,但保存的是圖片整體到資料庫里。

F. C#WinForm中,用於將圖片以二進制存入sql資料庫中,並將圖片從資料庫中取出,顯示在PictureBox控制項中。

插入: //單擊圖片選擇添加的圖片 private void pic_Click(object sender, EventArgs e)
{ dlg.Filter = "JPG|*.jpg|BMP|*.bmp|PNG|*.png";
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pic.Image = Image.FromFile(dlg.FileName);
txtFilePath = dlg.FileName;
}
} public byte[] picData; public string txtFilePath = ""; 添加確定按鈕代碼: f (txtFilePath != "")
{
try
{
FileStream fs = new FileStream(txtFilePath, FileMode.Open, FileAccess.Read);
int len = Convert.ToInt32(fs.Length);
b = new byte[len];
fs.Read(b, 0, len);
fs.Close();
}
catch
{
b = null;
}
} SqlConnection conn = new SqlConnection(strConn);
conn.Open(); SqllCommand cmdInsert = new SqlCommand();
cmdInsert.Connection = conn;
cmdInsert.CommandText =插入語句; cmdInsert.Parameters.Add("@照片", SqlDbType.Image); if (txtFilePath == "")
{
cmdInsert.Parameters["@照片"].Value = DBNull.Value;
}
else
{
cmdInsert.Parameters["@照片"].Value = b;
}
cmdInsert.ExecuteNonQuery();
conn.Close();獲取: public byte[] picData; SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from 聯系人 where 編號=" + ID.ToString();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet(); sda.Fill(ds); if (ds.Tables[0].Rows.Count == 1)
{
if (ds.Tables[0].Rows[0]["照片"] == DBNull.Value)
{ //pic為picturebox控制項
pic.Image = PhoneBoook.Properties.Resources.DeskShade;//為空的話給個默認圖片
}
else
{
byte[] b = (byte[])(ds.Tables[0].Rows[0]["照片"]);
pic.Image = Image.FromStream(new MemoryStream(b));
picData = b;
}
}

G. C#讀取資料庫IMAGE欄位的內容。

讀取... 讀取長二進制為圖片..
string sql = "select photo from studentinfo where studentid = " + this.Tag.ToString();
OleDbCommand cmd = new OleDbCommand(sql, connection1);
if (Convert.DBNull != cmd.ExecuteScalar())
pictureBox1.Image = Image.FromStream(new MemoryStream((Byte[])cmd.ExecuteScalar()));

放大就不知道了

保存:
string filename= textBox1.Text;//"c:\\IMG_0117.jpg";
BinaryReader reader=null;
FileStream myfilestream = new FileStream(filename,FileMode.Open);

try
{
reader=new BinaryReader(myfilestream);
byte[] image = reader.ReadBytes((int)myfilestream.Length);
using (SqlConnection conn = new SqlConnection("server=test05;database=esdb2;uid=datatran;pwd=qyrl"))
{
using (SqlCommand command = conn.CreateCommand())
{
command.CommandText =@"INSERT INTO photo(photo) VALUES (@photo)";
command.Parameters.Add("@photo", image);
conn.Open();
command.ExecuteNonQuery();
conn.Close();
MessageBox.Show("文件保存成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
}
catch(IOException ee)
{
MessageBox.Show(ee.Message.ToString());
}
finally
{
if(reader!=null)
reader.Close();
}

熱點內容
除法萬能速演算法 發布:2025-02-28 15:45:23 瀏覽:851
編程語言種類 發布:2025-02-28 15:23:03 瀏覽:934
java圖譜 發布:2025-02-28 15:20:45 瀏覽:853
蘋果平板系統初始密碼是多少 發布:2025-02-28 15:18:04 瀏覽:91
ftp最後20分鍾 發布:2025-02-28 15:17:57 瀏覽:119
易語言腳本多窗口運行 發布:2025-02-28 15:17:53 瀏覽:54
惠普伺服器怎麼進raid5 發布:2025-02-28 15:10:05 瀏覽:568
讀資料庫 發布:2025-02-28 14:59:00 瀏覽:22
手機壓縮包能刪除嗎 發布:2025-02-28 14:44:47 瀏覽:222
雲伺服器網址是什麼 發布:2025-02-28 14:43:57 瀏覽:189