| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1613 人关注过本帖
标题:请问怎样把图像文件从数据库中取出显示在相应的控件中?
只看楼主 加入收藏
cchflydragon
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-6-20
收藏
 问题点数:0 回复次数:2 
请问怎样把图像文件从数据库中取出显示在相应的控件中?

我现在想把pdf文档放入数据库,在从数据库中取出显示。谁有示例代码帮忙给以份。我的邮箱cchflydragon@163.com
下面是我的代码帮我看看错在什么地方

//将图片文件存入数据库
private void button1_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("D:\\12.jpg",FileMode.Open);
BinaryReader br = new BinaryReader(fs);
int len = Convert.ToInt32(br.BaseStream.Length);
byte[] buffer = new Byte[len];
OleDbParameter[] parms = { new OleDbParameter("@info", OleDbType.LongVarBinary) };
parms[0].Value = buffer;
SqlHelper helper = new SqlHelper();
int result = helper.Execute("insert into data(info) values (@info)", parms);

}

//从数据库取出图片文件并显示在picturebox1控件中

private void button2_Click(object sender, EventArgs e)
{
SqlHelper helper = new SqlHelper();
DataSet ds = helper.GetDs("select info from data ");
byte[] buffer = (byte[])ds.Tables[0].Rows[0][0];
MessageBox.Show(Convert.ToString(buffer.Length));
MemoryStream ms = new MemoryStream(buffer,false);
this.pictureBox1.Image = Image.FromStream(ms);

}

最后提示参数无效

搜索更多相关主题的帖子: 数据库 控件 图像 文件 
2007-08-16 11:55
hxhfg
Rank: 1
等 级:新手上路
帖 子:284
专家分:0
注 册:2006-4-18
收藏
得分:0 

我昨夜研究才得,我发给你.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.IO;
namespace 插入图片完整版
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
string fullname;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
OleDbConnection con;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;
private System.Windows.Forms.TextBox picName;
OleDbDataAdapter da;
DataSet ds;
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.picName = new System.Windows.Forms.TextBox();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
this.dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.White;
this.pictureBox1.Location = new System.Drawing.Point(40, 80);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(416, 352);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(528, 15);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 23);
this.button1.TabIndex = 1;
this.button1.Text = "浏览图片";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(616, 15);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 23);
this.button2.TabIndex = 2;
this.button2.Text = "保 存";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// picName
//
this.picName.Location = new System.Drawing.Point(256, 15);
this.picName.Name = "picName";
this.picName.Size = new System.Drawing.Size(248, 21);
this.picName.TabIndex = 3;
this.picName.Text = "";
//
// dataGrid1
//
this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.ActiveCaptionText;
this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Control;
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(504, 88);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.ReadOnly = true;
this.dataGrid1.Size = new System.Drawing.Size(176, 344);
this.dataGrid1.TabIndex = 4;
this.dataGrid1.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
this.dataGridTableStyle1});
this.dataGrid1.Click += new System.EventHandler(this.dataGrid1_Click);
//
// dataGridTableStyle1
//
this.dataGridTableStyle1.DataGrid = this.dataGrid1;
this.dataGridTableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
this.dataGridTextBoxColumn1});
this.dataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGridTableStyle1.MappingName = "image1";
//
// dataGridTextBoxColumn1
//
this.dataGridTextBoxColumn1.Format = "";
this.dataGridTextBoxColumn1.FormatInfo = null;
this.dataGridTextBoxColumn1.HeaderText = "图片编号:";
this.dataGridTextBoxColumn1.MappingName = "id";
this.dataGridTextBoxColumn1.Width = 75;
//
// label1
//
this.label1.Location = new System.Drawing.Point(48, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(296, 23);
this.label1.TabIndex = 5;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(696, 454);
this.Controls.Add(this.label1);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.picName);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.pictureBox1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
try
{
this.openFileDialog1.Filter = "图片文件(*.jpg;*.gif;*.bmp)|*.jpg;*.gif;*.bmp";
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
fullname=this.openFileDialog1.FileName;
this.pictureBox1.Image=Bitmap.FromFile(fullname);
this.picName.Text=fullname;
int i=this.openFileDialog1.FileName.LastIndexOf("\\")+1;
this.fullname=fullname.Substring(i);
}
}
catch(Exception er)
{
MessageBox.Show(er.Message);
}
}

private void button2_Click(object sender, System.EventArgs e)
{
try
{
FileStream fs=File.OpenRead(fullname);
byte [] content=new byte[fs.Length];
fs.Read(content,0,content.Length);
fs.Close();

OleDbCommand com=new OleDbCommand("insert into image1(pic,picname)values(@pic,'"+this.fullname.Trim().ToString()+"')",con);
com.Parameters.Add("@pic",OleDbType.Binary);
com.Parameters["@pic"].Value=content;
com.ExecuteNonQuery();
// con.Close();
MessageBox.Show("图片成功保存!");
this.load_data();
}
catch(Exception er)
{
MessageBox.Show(er.Message);
}
}

private void Form1_Load(object sender, System.EventArgs e)
{
this.load_data();
}
void load_data()
{
con=new OleDbConnection(@"provider=microsoft.jet.oledb.4.0;data source="+AppDomain.CurrentDomain.BaseDirectory+"image.mdb");
con.Open();
da=new OleDbDataAdapter("select * from image1",con);
ds=new DataSet();
da.Fill(ds);
this.dataGrid1.DataSource=ds.Tables[0].DefaultView;
}
private int Cur()
{
return this.dataGrid1.CurrentRowIndex;
}

private void dataGrid1_Click(object sender, System.EventArgs e)
{
try
{
//int cur=this.dataGrid1.CurrentRowIndex;
int cur=this.Cur();
this.label1.Text=this.ds.Tables[0].Rows[cur]["picname"].ToString();
MemoryStream ms=new MemoryStream((byte[])this.ds.Tables[0].Rows[cur]["pic"]);
Image image=Image.FromStream(ms,true);
this.pictureBox1.Image=image;
}
catch(Exception er)
{
MessageBox.Show(er.Message);
}
}
}
}


2007-08-16 12:25
jacklee
Rank: 7Rank: 7Rank: 7
来 自:XAplus
等 级:贵宾
威 望:32
帖 子:1769
专家分:104
注 册:2006-11-3
收藏
得分:0 
GOOD!

XAplus!
讨论群:51090447
删吧删吧,把我的号给删了!
2007-08-16 21:41
快速回复:请问怎样把图像文件从数据库中取出显示在相应的控件中?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043540 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved