在c#.net中如何显示存在数据表中的照片?
摄像头拍的照片存在数据库中的一个字段里,在程序中如何将其显示在页面上?多谢!!!!!! 问题点数:20、回复次数:5Top
1 楼LaoDai_Net(『老代』)回复于 2005-06-03 11:38:49 得分 0
webform 可以看这里
http://dotnet.aspx.cc/ShowDetail.aspx?id=ECD9AE16-8FF0-4A1C-9B9F-5E8B641CB1B1
winform没有搞过,,帮upTop
2 楼bumm(......)回复于 2005-06-03 11:43:16 得分 5
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace 保存图片
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Data.SqlClient.SqlCommand sqlCommand1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Windows.Forms.Button button2;
/// <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 Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.BackColor = System.Drawing.Color.White;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(104, 320);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pictureBox1
//
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(456, 296);
this.pictureBox1.TabIndex = 2;
this.pictureBox1.TabStop = false;
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "server=localhost;database=exe;uid=sa;password=";
//
// button2
//
this.button2.BackColor = System.Drawing.Color.White;
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Location = new System.Drawing.Point(248, 320);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(440, 357);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.pictureBox1,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
string pathName;
if (this.openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
pathName = this.openFileDialog1.FileName;
System.Drawing.Image img = System.Drawing.Image.FromFile(pathName);
this.pictureBox1.Image = img;
//将图像读入到字节数组
System.IO.FileStream fs = new System.IO.FileStream(pathName,System.IO.FileMode.Open,System.IO.FileAccess.Read);
byte[] buffByte = new byte[fs.Length];
fs.Read(buffByte,0,(int)fs.Length);
fs.Close();
fs = null;
//建立Command命令
string comm = @"Insert into image(image) values(@img)";
this.sqlCommand1 = new System.Data.SqlClient.SqlCommand ();
this.sqlCommand1.CommandType = System.Data.CommandType.Text ;
this.sqlCommand1.CommandText = comm;
this.sqlCommand1.Connection = this.sqlConnection1 ;
//创建Parameter
this.sqlCommand1.Parameters.Add("@img",System.Data.SqlDbType.Image);
this.sqlCommand1.Parameters[0].Value = buffByte;
// this.sqlCommand1.Parameters.Add("@name",System.Data.SqlDbType.VarChar);
// this.sqlCommand1.Parameters[1].Value =pathName.Substring(pathName.LastIndexOf("\\")+1);
try
{
this.sqlConnection1.Open();
this.sqlCommand1.ExecuteNonQuery();
this.sqlConnection1.Close();
}
catch(System.Exception ee)
{
MessageBox.Show(ee.Message );
}
buffByte = null;
// this.FillListBox();
}
}
private void button2_Click(object sender, System.EventArgs e)
{
try
{
byte[] buffByte = null;
string comm = @"select image from image";
this.sqlCommand1 = new System.Data.SqlClient.SqlCommand ();
// this.sqlCommand1.CommandType = System.Data.CommandType.Text ;
this.sqlCommand1.CommandText = comm;
this.sqlCommand1.Connection = this.sqlConnection1 ;
this.sqlConnection1.Open();
System.Data.SqlClient.SqlDataReader rd = this.sqlCommand1.ExecuteReader();
while (rd.Read())
{
buffByte = ((byte[])rd[0]);
}
rd.Close();
this.sqlConnection1.Close();
//将图像的字节数组放入内存流
System.IO.MemoryStream ms = new System.IO.MemoryStream(buffByte);
//通过流对象建立Bitmap
System.Drawing.Bitmap bmp = new Bitmap(ms);
this.pictureBox1.Image = bmp;
}
catch(Exception error)
{
MessageBox.Show(error.Message);
}
}
}
}
Top
3 楼richardingding()回复于 2005-06-03 11:55:28 得分 0
建立一个放图片的文件夹,在数据库里面保存路径,要显示图片的时候,直接在数据库你提出路径,这样就可以显示图片了Top
4 楼xumingxsh(admin11)回复于 2005-06-03 13:06:40 得分 10
private System.Windows.Forms.PictureBox pbFTFB;
private byte[] m_TL;
private void GetPicture(object Data_)
{
try
{
byte[] myByte = null;
myByte = (byte[])Data_;
if ( myByte == null)
{
return;
}
MemoryStream ms = new MemoryStream(myByte);
Bitmap bmap = new Bitmap(ms);
pbFTFB.Image = (Image)bmap;
ms.Close();
}
catch
{
MessageBox.Show("显示附图附表时出现错误" ,"错误");
}
}Top
5 楼syliuxybj()回复于 2005-06-03 13:31:15 得分 5
如果图像是存贮的路径,那就直接读取路径,然后Bitmap bmap = new Bitmap(filepath);picturebox.Image = bmap就可以了。
如果数据库里存储的是大二进制流binary(SQL Server) BCLOB(Oracle)的话,那就先把图片的二进制流读取到一个数组byte[]里,然后再在picturebox里显示就行了。
读取图片还是比较容易的 要存就没有那么简单了 HoHo~~~~~~~~~~~Top




