分享
 
 
 

利用C#在SQL Server2000存取图像 For Window

王朝mssql·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

数据库表:

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;

// }

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有