当前位置:首页 » 操作系统 » c数据库读取图片

c数据库读取图片

发布时间: 2022-08-31 17:45:16

A. vc怎么把图片读入sql数据库

比较复杂,看我的笔记。
根本方法是定义图片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类型图片的绘制,你可以看下

热点内容
apache加密 发布:2025-05-14 14:49:13 浏览:967
安卓什么软件苹果不能用 发布:2025-05-14 14:49:03 浏览:769
jsoupjava 发布:2025-05-14 14:38:00 浏览:885
影豹选哪个配置最好 发布:2025-05-14 14:28:50 浏览:255
定期预算法的 发布:2025-05-14 14:24:08 浏览:894
interbase数据库 发布:2025-05-14 13:49:50 浏览:691
微商海报源码 发布:2025-05-14 13:49:42 浏览:347
分布式缓存部署步骤 发布:2025-05-14 13:24:51 浏览:611
php获取上一月 发布:2025-05-14 13:22:52 浏览:90
购买云服务器并搭建自己网站 发布:2025-05-14 13:20:31 浏览:689