图片保存到数据及从数据库读出(winform,c# 和vb.net)

C#
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;

namespace regedit
{
///

1<summary>   
2/// Picture 的摘要描述。   
3/// </summary>

public class Picture : System.Windows.Forms.Form
{
private System.Windows.Forms.OpenFileDialog openFileDlg;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnBrowse;
private System.Windows.Forms.TextBox txtfile;
private System.Windows.Forms.Button btnInsert;
private System.Windows.Forms.TextBox txtname;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.PictureBox pictureBox1;

///

1<summary>   
2/// 設計工具所需的變數。   
3/// </summary>

private System.ComponentModel.Container components = null;
SqlConnection conn=new SqlConnection("Data Source=localhost; Integrated Security=SSPI;Initial Catalog=mis");
public Picture()
{
//
// Windows Form 設計工具支援的必要項
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼
//
}

///

1<summary>   
2/// 清除任何使用中的資源。   
3/// </summary>

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form 設計工具產生的程式碼
///

1<summary>   
2/// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改   
3/// 這個方法的內容。   
4/// </summary>

private void InitializeComponent()
{
this.openFileDlg = new System.Windows.Forms.OpenFileDialog();
this.label1 = new System.Windows.Forms.Label();
this.txtfile = new System.Windows.Forms.TextBox();
this.btnBrowse = new System.Windows.Forms.Button();
this.btnInsert = new System.Windows.Forms.Button();
this.txtname = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 23);
this.label1.TabIndex = 0;
this.label1.Text = "ImageName";
//
// txtfile
//
this.txtfile.Location = new System.Drawing.Point(88, 16);
this.txtfile.Name = "txtfile";
this.txtfile.Size = new System.Drawing.Size(384, 22);
this.txtfile.TabIndex = 1;
this.txtfile.Text = "";
//
// btnBrowse
//
this.btnBrowse.Location = new System.Drawing.Point(24, 48);
this.btnBrowse.Name = "btnBrowse";
this.btnBrowse.TabIndex = 2;
this.btnBrowse.Text = "Browse";
this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
//
// btnInsert
//
this.btnInsert.Location = new System.Drawing.Point(112, 48);
this.btnInsert.Name = "btnInsert";
this.btnInsert.TabIndex = 3;
this.btnInsert.Text = "Insert";
this.btnInsert.Click += new System.EventHandler(this.btnInsert_Click);
//
// txtname
//
this.txtname.Location = new System.Drawing.Point(112, 88);
this.txtname.Name = "txtname";
this.txtname.TabIndex = 4;
this.txtname.Text = "txtname";
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 88);
this.button1.Name = "button1";
this.button1.TabIndex = 5;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(24, 128);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(320, 160);
this.pictureBox1.TabIndex = 6;
this.pictureBox1.TabStop = false;
//
// Picture
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
this.ClientSize = new System.Drawing.Size(488, 317);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.txtname);
this.Controls.Add(this.btnInsert);
this.Controls.Add(this.btnBrowse);
this.Controls.Add(this.txtfile);
this.Controls.Add(this.label1);
this.Name = "Picture";
this.Text = "操作圖片";
this.Load += new System.EventHandler(this.Picture_Load);
this.ResumeLayout(false);

}
#endregion

private void btnBrowse_Click(object sender, System.EventArgs e)
{
if (openFileDlg.ShowDialog()==DialogResult.OK )
{
txtfile.Text=openFileDlg.FileName;
btnInsert.Enabled = true;
}
}

private void btnInsert_Click(object sender, System.EventArgs e)
{
FileStream fs=File.OpenRead(txtfile.Text);
byte[] content=new byte[fs.Length];
fs.Read(content, 0,content.Length);
fs.Close();
conn.Open();
string sql ="insert into Photos(name,Photo) values(@name, @Photos)";
SqlCommand comm=new SqlCommand(sql,conn);
comm.Parameters.Add("@Photos", SqlDbType.Image).Value=content;
comm.Parameters.Add("@name", SqlDbType.NVarChar).Value=txtname.Text;
if(comm.ExecuteNonQuery()==1)
{
MessageBox.Show("Successfully insert image into database!");
}
else
{
MessageBox.Show("Failed to insert image into database");
}
conn.Close();
}

private void Picture_Load(object sender, System.EventArgs e)
{

}

private void button1_Click(object sender, System.EventArgs e)
{
ShowImage(txtname.Text);
}

private void ShowImage(string s)
{
string str = "SELECT photo FROM Photos WHERE name='" + s +"'";
SqlCommand cmd = new SqlCommand(str, conn);
conn.Open();
byte[] b= (byte[])cmd.ExecuteScalar();
if (b.Length > 0)
{
MemoryStream stream = new MemoryStream(b, true);
stream.Write(b, 0, b.Length);
DrawToScale(new Bitmap(stream));
stream.Close();
}
conn.Close();
}

private void DrawToScale(Image bmp)
{
pictureBox1.Image = new Bitmap(bmp);
}

}
}

VB.NET
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient

Public Class photofrm
Inherits System.Windows.Forms.Form
Dim cn As SqlClient.SqlConnection

#Region " Windows Form 設計工具產生的程式碼 "

Public Sub New()
MyBase.New()

'此為 Windows Form 設計工具所需的呼叫。
InitializeComponent()

'在 InitializeComponent() 呼叫之後加入所有的初始設定

End Sub

'Form 覆寫 Dispose 以清除元件清單。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'為 Windows Form 設計工具的必要項
Private components As System.ComponentModel.IContainer

'注意: 以下為 Windows Form 設計工具所需的程序
'您可以使用 Windows Form 設計工具進行修改。
'請勿使用程式碼編輯器來修改這些程序。
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Public WithEvents SqlCommand1 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents Button5 As System.Windows.Forms.Button

  1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()   
  2Me.Label1 = New System.Windows.Forms.Label   
  3Me.Label2 = New System.Windows.Forms.Label   
  4Me.TextBox1 = New System.Windows.Forms.TextBox   
  5Me.PictureBox1 = New System.Windows.Forms.PictureBox   
  6Me.Button1 = New System.Windows.Forms.Button   
  7Me.Button2 = New System.Windows.Forms.Button   
  8Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog   
  9Me.SqlCommand1 = New System.Data.SqlClient.SqlCommand   
 10Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection   
 11Me.Button3 = New System.Windows.Forms.Button   
 12Me.Button4 = New System.Windows.Forms.Button   
 13Me.Button5 = New System.Windows.Forms.Button   
 14Me.SuspendLayout()   
 15'   
 16'Label1   
 17'   
 18Me.Label1.Location = New System.Drawing.Point(38, 15)   
 19Me.Label1.Name = "Label1"   
 20Me.Label1.Size = New System.Drawing.Size(120, 21)   
 21Me.Label1.TabIndex = 0   
 22Me.Label1.Text = "Name"   
 23'   
 24'Label2   
 25'   
 26Me.Label2.Location = New System.Drawing.Point(38, 60)   
 27Me.Label2.Name = "Label2"   
 28Me.Label2.Size = New System.Drawing.Size(120, 21)   
 29Me.Label2.TabIndex = 1   
 30Me.Label2.Text = "Photo"   
 31'   
 32'TextBox1   
 33'   
 34Me.TextBox1.Location = New System.Drawing.Point(202, 15)   
 35Me.TextBox1.Name = "TextBox1"   
 36Me.TextBox1.Size = New System.Drawing.Size(120, 21)   
 37Me.TextBox1.TabIndex = 2   
 38Me.TextBox1.Text = ""   
 39'   
 40'PictureBox1   
 41'   
 42Me.PictureBox1.Location = New System.Drawing.Point(202, 52)   
 43Me.PictureBox1.Name = "PictureBox1"   
 44Me.PictureBox1.Size = New System.Drawing.Size(528, 187)   
 45Me.PictureBox1.TabIndex = 3   
 46Me.PictureBox1.TabStop = False   
 47'   
 48'Button1   
 49'   
 50Me.Button1.Location = New System.Drawing.Point(173, 284)   
 51Me.Button1.Name = "Button1"   
 52Me.Button1.Size = New System.Drawing.Size(90, 21)   
 53Me.Button1.TabIndex = 4   
 54Me.Button1.Text = "Brose"   
 55'   
 56'Button2   
 57'   
 58Me.Button2.Location = New System.Drawing.Point(298, 284)   
 59Me.Button2.Name = "Button2"   
 60Me.Button2.Size = New System.Drawing.Size(90, 21)   
 61Me.Button2.TabIndex = 5   
 62Me.Button2.Text = "Add"   
 63'   
 64'SqlCommand1   
 65'   
 66Me.SqlCommand1.CommandText = "dbo.[sp_InsertPhoto]"   
 67Me.SqlCommand1.CommandType = System.Data.CommandType.StoredProcedure   
 68Me.SqlCommand1.Connection = Me.SqlConnection1   
 69Me.SqlCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))   
 70Me.SqlCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50))   
 71Me.SqlCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@image", System.Data.SqlDbType.VarBinary, 2147483647))   
 72'   
 73'SqlConnection1   
 74'   
 75Me.SqlConnection1.ConnectionString = "workstation id=HELLEN;packet size=4096;user id=sa;data source=HELLEN;persist secu" &amp; _   
 76"rity info=False;initial catalog=MIS"   
 77'   
 78'Button3   
 79'   
 80Me.Button3.Location = New System.Drawing.Point(403, 284)   
 81Me.Button3.Name = "Button3"   
 82Me.Button3.Size = New System.Drawing.Size(90, 21)   
 83Me.Button3.TabIndex = 6   
 84Me.Button3.Text = "Show"   
 85'   
 86'Button4   
 87'   
 88Me.Button4.Location = New System.Drawing.Point(614, 284)   
 89Me.Button4.Name = "Button4"   
 90Me.Button4.Size = New System.Drawing.Size(90, 21)   
 91Me.Button4.TabIndex = 7   
 92Me.Button4.Text = "Close"   
 93'   
 94'Button5   
 95'   
 96Me.Button5.Location = New System.Drawing.Point(509, 284)   
 97Me.Button5.Name = "Button5"   
 98Me.Button5.Size = New System.Drawing.Size(90, 21)   
 99Me.Button5.TabIndex = 8   
100Me.Button5.Text = "Delete"   
101'   
102'photofrm   
103'   
104Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)   
105Me.ClientSize = New System.Drawing.Size(796, 381)   
106Me.Controls.Add(Me.Button5)   
107Me.Controls.Add(Me.Button4)   
108Me.Controls.Add(Me.Button3)   
109Me.Controls.Add(Me.Button2)   
110Me.Controls.Add(Me.Button1)   
111Me.Controls.Add(Me.PictureBox1)   
112Me.Controls.Add(Me.TextBox1)   
113Me.Controls.Add(Me.Label2)   
114Me.Controls.Add(Me.Label1)   
115Me.Name = "photofrm"   
116Me.Text = "photofrm"   
117Me.ResumeLayout(False) 
118
119End Sub 
120
121#End Region 
122
123Private Sub photofrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load   
124'dim s as Decimal   
125'If s = True Then   
126' MsgBox(Convert.ToString(Convert.ToUInt16(s)))   
127'End If 
128
129End Sub 
130
131Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
132
133'Display Picture File   
134OpenFileDialog1.InitialDirectory = "d:\pic"   
135OpenFileDialog1.DefaultExt = "gif"   
136OpenFileDialog1.Filter = "Bmp Files(*.bmp)|*.bmp|Gif Files(*.gif)|*.gif|Jpg Files(*.jpg)|*.jpg"   
137OpenFileDialog1.ShowDialog()   
138PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)   
139End Sub 
140
141Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click   
142' To Insert Image   
143Dim st As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)   
144Dim s As String = TextBox1.Text   
145Dim mbr As BinaryReader = New BinaryReader(st)   
146Dim buffer(st.Length) As Byte   
147mbr.Read(buffer, 0, CInt(st.Length))   
148st.Close()   
149InsertImage(buffer, s) 
150
151End Sub   
152'Function For Inserting in the Procdeure in the Database   
153Public Function InsertImage(ByRef buffer, ByVal str)   
154cn = New SqlClient.SqlConnection(SqlConnection1.ConnectionString)   
155cn.Open()   
156Dim cmd As New SqlClient.SqlCommand("sp_InsertPhoto", cn)   
157cmd.CommandType = CommandType.StoredProcedure   
158cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text   
159cmd.Parameters.Add("@image", SqlDbType.Image).Value = buffer   
160cmd.ExecuteNonQuery()   
161MsgBox("Image inserted")   
162cn.Close()   
163End Function   
164Private Sub ShowImage(ByVal s As String)   
165cn = New SqlClient.SqlConnection(SqlConnection1.ConnectionString)   
166cn.Open()   
167Dim str As String = "SELECT photo FROM Photos WHERE name='" &amp; s &amp; "'"   
168Dim cmd As New SqlClient.SqlCommand(str, cn)   
169TextBox1.Text = s   
170Dim b() As Byte   
171b = cmd.ExecuteScalar()   
172If (b.Length &gt; 0) Then   
173Dim stream As New MemoryStream(b, True)   
174stream.Write(b, 0, b.Length)   
175DrawToScale(New Bitmap(stream))   
176stream.Close()   
177End If   
178cn.Close()   
179End Sub   
180Private Sub DrawToScale(ByVal bmp As Image)   
181PictureBox1.Image = New Bitmap(bmp)   
182End Sub 
183
184Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 
185
186Dim i As String = InputBox("Please Input Name:")   
187ShowImage(i)   
188End Sub 
189
190Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click   
191Me.Dispose() 
192
193End Sub 
194
195Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click   
196cn = New SqlClient.SqlConnection(SqlConnection1.ConnectionString)   
197cn.Open()   
198Dim s As String = InputBox("Please Input Name:")   
199Dim cmd As New SqlClient.SqlCommand("delete from photos where name='" &amp; s &amp; "'", cn)   
200cmd.ExecuteNonQuery()   
201MsgBox("Image deleted")   
202cn.Close()   
203End Sub 
204
205Private Sub photofrm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing 
206
207End Sub   
208End Class</system.diagnostics.debuggerstepthrough()>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus