c資料庫讀取圖片
比較復雜,看我的筆記。
根本方法是定義圖片VARIANT變數,其類型為VT_ARRAY|VT_UI1。此時VARIANT的值為SAFEARRAY.將圖片載入SAFEARRAY中。便可以存入圖片了。可以存儲任何格式 的圖片;
void CDataBase16Dlg::OnBnClickedAddNew()
{
CString filepath;
CFileDialog filedialog(TRUE);
if(filedialog.DoModal()==IDOK)
{
filepath=filedialog.GetPathName();
}
CFile file;
file.Open(filepath,CFile::modeRead);
SAFEARRAY *psa; //SAFEARRAY是variant變數的參數之一。
SAFEARRAYBOUND bound[1]; //SAFEARRAYBOUND為SAFEARRAY結構成員之一。
bound[0].lLbound=0; //代表數組起始索引,一般為 0;
bound[0].cElements=file.GetLength(); //代表元素個數,即圖片的大小。以位元組為單位
psa=SafeArrayCreate(VT_UI1,1,bound); //創建SAFEARRAY,中間參數代表創建的是一個一維數組;
BYTE *filebuffer=new BYTE[file.GetLength()+1];
file.Read(filebuffer,file.GetLength()); //將圖片文件讀入內存。
for(long index=0;index<file.GetLength();index++)
SafeArrayPutElement(psa,&index,&filebuffer[index]); //將圖片的每一位元組放入SAFEAWWAY中。
VARIANT vt;
vt.vt = VT_ARRAY|VT_UI1;
vt.parray=psa;
m_recordset->GetFields()->GetItem(_variant_t("肖像"))->AppendChunk(vt);//添加圖片。
m_recordset->Update();
}
至於讀取,你沒問我也就不答了,比較長。
B. 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();
}
C. C#做c/s開發,是怎麼讀取遠程主機的圖像文件的
圖片都存在伺服器上, cs程序端也是通過URL形式訪問圖片,如System.Net.WebClient類可以讀取遠程圖片, 本質上和BS結構一樣,只不過BS結構是瀏覽器幫你實現讀取遠程圖片這一步。
當然,伺服器端得架設一個web站點提供服務。
D. c#怎樣從資料庫讀取圖片並保存到指定文件
SqlDataAdapter da = new SqlDataAdapter("select * from newpicture", conn);
DataSet ds = new DataSet();
da.Fill(ds, "pic");
string picdotname;
string picfilename;
int piclength;
int i;
//添加新列
DataColumn newcolumn = ds.Tables["pic"].Columns.Add("pic_url", typeof(string));
for (i = 0; i < Convert.ToInt16(ds.Tables["pic"].Rows.Count); i++)
{
picdotname = ds.Tables["pic"].Rows[i]["pic_dot"].ToString();
piclength = Convert.ToInt32(ds.Tables["pic"].Rows[i]["pic_length"]);
picfilename = Server.MapPath("temp/") + "temp" + i.ToString() + "." + picdotname;
FileStream fs = new FileStream(picfilename, FileMode.Create, FileAccess.Write);
byte[] piccontent = new byte[piclength];
piccontent = (byte[])ds.Tables["pic"].Rows[i]["pic_content"];
fs.Write(piccontent, 0, piclength);
fs.Close();
ds.Tables["pic"].Rows[i]["pic_url"] = "temp/temp" + i.ToString() + "." + picdotname;//相對路徑,改成你自己的文件夾
}
數據源 = ds.Tables["pic"];//數據綁定
大體是這樣吧,裡面表名列名很多細節你按你的表修改吧!
用哪個控制項都行,只要圖片路徑正確就能顯示的
E. VS.C#如何向數據資料庫中存入和讀取圖片的
首先,在資料庫中要建立相應的欄位能保存Bytes,例如在SQL Server中用Image類型來定義欄位。我所用到的資料庫大致結構如下:
欄位名
類型
備注
FileID
Int
自增欄位
FileName
Varchar(256)
FullName
Varchar(1024)
FileData
Image
然後就是寫入資料庫,代碼如下:
FileInfo fi = new FileInfo( txtFileName.Text );// Replace with your file name
if ( fi.Exists)
{
byte[] bData = null;
int nNewFileID = 0;
// Read file data into buffer
using ( FileStream fs = fi.OpenRead() )
{
bData = new byte[fi.Length];
int nReadLength = fs.Read( bData,0, (int)(fi.Length) );
}
// Add file info into DB
string strQuery = "INSERT INTO FileInfo "
+ " ( FileName, FullName, FileData ) "
+ " VALUES "
+ " ( @FileName, @FullName, @FileData ) "
+ " SELECT @@IDENTITY AS 'Identity'";
SqlCommand sqlComm = new SqlCommand( strQuery, sqlConn );
sqlComm.Parameters.Add( "@FileName", fi.Name );
sqlComm.Parameters.Add( "@FullName", fi.FullName );
sqlComm.Parameters.Add( "@FileData", bData );
// Get new file ID
SqlDataReader sqlReader = sqlComm.ExecuteReader();
if( sqlReader.Read() )
{
nNewFileID = int.Parse(sqlReader.GetValue(0).ToString());
}
sqlReader.Close();
sqlComm.Dispose();
if( nNewFileID > 0 )
{
// Add new item in list view
ListViewItem itmNew = lsvFileInfo.Items.Add( fi.Name );
itmNew.Tag = nNewFileID;
}
}
而讀出的代碼如下:
// Get new file name
string strFullName = dlgFBSave.SelectedPath;
if( strFullName[strFullName.Length - 1] != '\\' )
strFullName += @"\";
strFullName += lsvFileInfo.SelectedItems[0].Text;
string strQuery = "SELECT FileData FROM FileInfo "
+ " WHERE FileID = " + lsvFileInfo.SelectedItems[0].Tag.ToString();
SqlDataAdapter sqlDAdapter = new SqlDataAdapter(strQuery,sqlConn);
DataSet sqlRecordSet = new DataSet();
byte[] bData = null;
//Get file data from DB
try
{
sqlDAdapter.Fill( sqlRecordSet, "FileInfo" );
foreach( DataRow dr in sqlRecordSet.Tables["FileInfo"].Rows)
{
if( dr["FileData"] != DBNull.Value )
bData = ( byte[] )dr["FileData"];
}
}
catch(SqlException sqlErr)
{
MessageBox.Show( sqlErr.Message );
}
catch
{
MessageBox.Show( "Failed to read data from DB!" );
}
sqlRecordSet.Dispose();
sqlDAdapter.Dispose();
if( bData != null )
{
// Save file
FileInfo fi = new FileInfo( strFullName );
if( !fi.Exists )
{
//Create the file.
using (FileStream fs = fi.Create())
{
fs.Write( bData, 0, bData.Length);
}
}
else
{
//Create the file.
using (FileStream fs = fi.OpenWrite())
{
fs.Write( bData, 0, bData.Length);
}
}
}
不過需要提的一點,如果把大量的文件存入資料庫的話,會造成資料庫的臃腫,而且訪問量也會增大。所以現在比較流行的做法,是把文件上傳到伺服器上,而在資料庫上只保存文件的相對路徑即可。那麼訪問的時候,先通過資料庫得到文件的相對路徑,然後再訪問伺服器上的文件
F. c#從資料庫中提取圖片
很簡單
如果沒別的要求的話
你在第一個image裡面綁定一個數據集或則直接查詢出第一條數據賦值給它
然後再查詢TOP6分別賦值給下面的6個IMAGE 就可以了
數據查詢不用我寫了吧
把查詢的結果放在DATATABLE裡面也沒問題吧
賦值就更簡單了 IAMGE1.imageurl = dt.rows[0]["圖片的URL"].ToString();
還有什麼問題的話 給我留言
G. asp.net在資料庫讀取圖片(C#)急
<ItemTemplate>
<a href='<%# DataBinder.Eval(Container.DataItem,"zsdatu")%>'><asp:Image ID="image1" ImageUrl='<%# Eval("zsdatu") %>' runat ="server" /></a>
</ItemTemplate>
分頁嘛,我給你點資料,自己去改一下,我以前就是根據這個做的,
本文收藏在我博客裏了
http://hi..com/lancy/blog/item/12be3b29b6cec3fe99250ab9.html
DataGrid控制項內部也使用了PagedDataSource類,PagedDataSource 類封裝 DataGrid 控制項的屬性,這些屬性使 DataGrid 可以執行分頁。
PagedDataSource 類的部分公共屬性:
AllowCustomPaging 獲取或設置指示是否啟用自定義分頁的值。
AllowPaging 獲取或設置指示是否啟用分頁的值。
Count 獲取要從數據源使用的項數。
CurrentPageIndex 獲取或設置當前頁的索引。
DataSource 獲取或設置數據源。
DataSourceCount 獲取數據源中的項數。
FirstIndexInPage 獲取頁中的第一個索引。
IsCustomPagingEnabled 獲取一個值,該值指示是否啟用自定義分頁。
IsFirstPage 獲取一個值,該值指示當前頁是否是首頁。
IsLastPage 獲取一個值,該值指示當前頁是否是最後一頁。
IsPagingEnabled 獲取一個值,該值指示是否啟用分頁。
IsReadOnly 獲取一個值,該值指示數據源是否是只讀的。
IsSynchronized 獲取一個值,該值指示是否同步對數據源的訪問(線程安全)。
PageCount 獲取顯示數據源中的所有項所需要的總頁數。
PageSize 獲取或設置要在單頁上顯示的項數。
VirtualCount 獲取或設置在使用自定義分頁時數據源中的實際項數。
這些屬性是否和DataGrid的屬性很相似?沒錯,DataGrid控制項就是使用PagedDataSource類來實現數據分頁顯示的 。下面舉個使用PagedDataSource類實現DataList和Repeater控制項的分頁顯示的例子:
public void Page_Load(Object src,EventArgs e)
{
OleDbConnection objConn=new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\test.mdb");
OleDbDataAdapter objCommand=new OleDbDataAdapter("select * from Users",objConn);
DataSet ds=new DataSet();
objCommand.Fill(ds);
//對PagedDataSource 對象的相關屬性賦值
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds.Tables[0].DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 5;
int CurPage;
//當前頁面從Page查詢參數獲取
if (Request.QueryString["Page"] != null)
CurPage=Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage=1;
objPds.CurrentPageIndex = CurPage-1;
lblCurrentPage.Text = "Page: " + CurPage.ToString();
if (!objPds.IsFirstPage)
lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage-1);
if (!objPds.IsLastPage)
lnkNext.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" + Convert.ToString(CurPage+1);
//把PagedDataSource 對象賦給Repeater控制項
Repeater1.DataSource=objPds;
Repeater1.DataBind();
H. 資料庫以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
I. vc mfc中怎麼通過按鈕 「上傳圖片」把本機上的圖片讀入SQL資料庫
圖片就是文件嘛,把文件數據全部讀入到內存然後插入到
sql資料庫
中就可以了,但不建議這樣做,因為圖片數據比較大,存入資料庫會很慢,檢索也會很慢,建議只存入圖片的路徑,比如你要存入的圖片為c:\\test.bmp,那麼你存入數據的信息就為c:\\test.bmp,當有地方要使用該圖片時,只需取出c:\\test.bmp這個路徑就可以操作該圖片了。
J. 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類型圖片的繪制,你可以看下