Using C # in SQL Server2000 Access Image for Window

xiaoxiao2021-03-06  71

Using C # in SQL Server2000 Access Image for Window

2004-05-21 Yufengly 9CBS

Database Table:

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

code:

The black body part is the main code

Using system.drawing; using system.componentmodel; using system.windows.form; using system.data;

A summary description of Namespace WindowsApplication1 {///

/// Form1. /// 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; /// // / / The designer variable. /// private system.componentmodel.container components = null;

Public Form1 () {// // Windows Form Designer Support for // InitializeComponent ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = null) {Components.dispose ();}} Base.Dispose (DISPOSION);} #Region Windows Form The code generated by the designer /// /// designer supports the required method - do not use the code editor to modify the // / this method.

/// 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.suspendla Yout (); // // ListBox1 // this.listbox1.dock = system.windows.forms.dockstyle.LOCT; this.listbox1.ithemheight = 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.form; this.pal1.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 = "Select image to deposit database"; 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 = "Read"; 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 = "Save"; //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 = "narrow"; //this.Menuitem2.click = new system.EventHandler (this.Menuitem2_click); // // Menuitem3 // this.Menuitem3. Index = 2; this.Menuitem3.text = "Zoom"; ///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.nam E = "form1"; this.text = "form1"; this.load = new system.eventhandler (this.form1_load); this.Panel1.ResumeLayout (false); this.Panel2.ResumeLayout (false); this.ResumeLayout (false);} #ENDREGION

///

/// The primary entry point of the application. /// [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.valueme MBER = "ID"; this.listbox1.refresh (); this.listbox1.selected IndexChanged = new system.eventhandler (this.listbox1_selected);}

// SQL server2000 saved image display Picture buffByte = null in the private void listBox1_SelectedIndexChanged (object sender, System.EventArgs e) {byte []; 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.sqlclcent.sqlDataReader rd = this.sqlcommand1.executeReader (); while (rd.read ()) {buffbyte = ((byte []) RD [0]);} rd .Close (); this.sqlConnection1.close (); // put the image array array into memory current system.io.MemoryStream ms = new system.io.MemoryStream (BUFFBYTE); // Establish BitMap System through the stream .Drawing.Bitmap bmp = new Bitmap (ms); this.pictureBox1.Image = bmp;} // save the image to the image field SQL server2000 private void button2_Click_1 (object sender, System.EventArgs e) {string pathName; if (this.openfiledialog1.showdia log () == System.Windows.Forms.DialogResult.OK) {pathName = this.openFileDialog1.FileName; System.Drawing.Image img = System.Drawing.Image.FromFile (pathName); this.pictureBox1.Image = img; // Read the image into byte arrays System.IO.FileStream fs = new system.io.filestream (pathname, system.io.filemode.open, system.io.filecess.read; byte [] buffbyte = new Byte [fs.length]; fs.read (buffbyte, 0, (int) fs.length); fs.close (); fs = null; // Established Command 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; // create 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.executenonury (); this.sqlConnection1.close ();} catch System.Exception EE) {MessageBox.show (Ee.Message);} buffbyte = null; this.FillListBox ();}}

// private void meneuitem1_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 | Bitmap (* .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.Eventa rgs 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; //}}}}}

转载请注明原文地址:https://www.9cbs.com/read-120157.html

New Post(0)