数据库表:
CREATE TABLE [TABLE1] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[img] [image] NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
程序代码:
黑体字部分为主要代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Data.SqlClient.SqlCommand sqlCommand1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
/// <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.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.listBox1 = new System.Windows.Forms.ListBox();
this.splitter1 = new System.Windows.Forms.Splitter();
this.panel1 = new System.Windows.Forms.Panel();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.panel2 = new System.Windows.Forms.Panel();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Dock = System.Windows.Forms.DockStyle.Left;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(0, 0);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(176, 412);
this.listBox1.TabIndex = 4;
//
// splitter1
//
this.splitter1.Location = new System.Drawing.Point(176, 0);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(3, 421);
this.splitter1.TabIndex = 5;
this.splitter1.TabStop = false;
//
// panel1
//
this.panel1.Controls.Add(this.button2);
this.panel1.Controls.Add(this.button1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel1.Location = new System.Drawing.Point(179, 349);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(469, 72);
this.panel1.TabIndex = 6;
//
// button2
//
this.button2.Location = new System.Drawing.Point(152, 16);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(208, 40);
this.button2.TabIndex = 4;
this.button2.Text = "选择图片存入数据库";
this.button2.Click += new System.EventHandler(this.button2_Click_1);
//
// button1
//
this.button1.Location = new System.Drawing.Point(216, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 32);
this.button1.TabIndex = 5;
this.button1.Text = "读取";
this.button1.Visible = false;
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2,
this.menuItem3});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "另存";
//this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// panel2
//
this.panel2.AutoScroll = true;
this.panel2.Controls.Add(this.pictureBox1);
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel2.Location = new System.Drawing.Point(179, 0);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(469, 349);
this.panel2.TabIndex = 7;
//
// pictureBox1
//
this.pictureBox1.ContextMenu = this.contextMenu1;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(264, 192);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "缩小";
//this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.Text = "放大";
//this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(648, 421);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Controls.Add(this.splitter1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
string connString="";
connString = @"Persist Security Info=False;User ID=sa;Initial Catalog=work;Data Source=(local)";
this.sqlConnection1.ConnectionString = connString;
this.FillListBox();
if (this.listBox1.Items.Count > 0) this.listBox1.SetSelected(0,true);
}
private void FillListBox()
{
this.listBox1.SelectedIndexChanged -= new System.EventHandler(this.listBox1_SelectedIndexChanged);
string comm = "select id,name from table1";
System.Data.DataTable dt = new DataTable("imgTable");
this.sqlCommand1.CommandType = System.Data.CommandType.Text ;
this.sqlCommand1.CommandText = comm;
this.sqlCommand1.Connection = this.sqlConnection1 ;
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(this.sqlCommand1);
this.sqlConnection1.Open();
da.Fill(dt);
this.sqlConnection1.Close();
this.listBox1.DataSource = dt;
this.listBox1.DisplayMember = "name";
this.listBox1.ValueMember = "id";
this.listBox1.Refresh();
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
}
//将SQL server2000中保存的图像显示在Picture中
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
byte[] buffByte = null;
string comm = @"select img from table1 where id = " + this.listBox1.SelectedValue ;
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;
}
//将图像保存到SQL server2000的Image字段中
private void button2_Click_1(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 table1(img,name) values(@img,@name)";
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 menuItem1_Click(object sender, System.EventArgs e)
// {
// this.saveFileDialog1.FileName = ((System.Data.DataRowView)this.listBox1.SelectedItem)[1].ToString();
// this.saveFileDialog1.Filter = "JPG (*.jpg)|*.jpg|GIF (*.gif)|*.gif|位图(*.bmp)|*.bmp";
// if (this.saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
// this.pictureBox1.Image.Save(this.saveFileDialog1.FileName);
// }
//
// private void menuItem2_Click(object sender, System.EventArgs e)
// {
// System.Drawing.Image img = this.pictureBox1.Image;
// System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
//
// System.Drawing.Image img1 = img.GetThumbnailImage(System.Convert.ToInt32(img.Width*0.5),System.Convert.ToInt32(img.Height*0.5),myCallback,IntPtr.Zero);
// this.pictureBox1.Image = img1;
// }
// public bool ThumbnailCallback()
// {
// return false;
// }
//
// private void menuItem3_Click(object sender, System.EventArgs e)
// {
// System.Drawing.Image img = this.pictureBox1.Image;
// System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
//
// System.Drawing.Image img1 = img.GetThumbnailImage(System.Convert.ToInt32(img.Width*1.5),System.Convert.ToInt32(img.Height*1.5),myCallback,IntPtr.Zero);
// this.pictureBox1.Image = img1;
// }
}
}