分享
 
 
 

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

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

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

{

/// <summary>

/// Picture 的摘要描述。

/// </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;

/// <summary>

/// 設計工具所需的變數。

/// </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 呼叫之後加入任何建構函式程式碼

//

}

/// <summary>

/// 清除任何使用中的資源。

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

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

/// <summary>

/// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改

/// 這個方法的內容。

/// </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

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.Label1 = New System.Windows.Forms.Label

Me.Label2 = New System.Windows.Forms.Label

Me.TextBox1 = New System.Windows.Forms.TextBox

Me.PictureBox1 = New System.Windows.Forms.PictureBox

Me.Button1 = New System.Windows.Forms.Button

Me.Button2 = New System.Windows.Forms.Button

Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog

Me.SqlCommand1 = New System.Data.SqlClient.SqlCommand

Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection

Me.Button3 = New System.Windows.Forms.Button

Me.Button4 = New System.Windows.Forms.Button

Me.Button5 = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'Label1

'

Me.Label1.Location = New System.Drawing.Point(38, 15)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(120, 21)

Me.Label1.TabIndex = 0

Me.Label1.Text = "Name"

'

'Label2

'

Me.Label2.Location = New System.Drawing.Point(38, 60)

Me.Label2.Name = "Label2"

Me.Label2.Size = New System.Drawing.Size(120, 21)

Me.Label2.TabIndex = 1

Me.Label2.Text = "Photo"

'

'TextBox1

'

Me.TextBox1.Location = New System.Drawing.Point(202, 15)

Me.TextBox1.Name = "TextBox1"

Me.TextBox1.Size = New System.Drawing.Size(120, 21)

Me.TextBox1.TabIndex = 2

Me.TextBox1.Text = ""

'

'PictureBox1

'

Me.PictureBox1.Location = New System.Drawing.Point(202, 52)

Me.PictureBox1.Name = "PictureBox1"

Me.PictureBox1.Size = New System.Drawing.Size(528, 187)

Me.PictureBox1.TabIndex = 3

Me.PictureBox1.TabStop = False

'

'Button1

'

Me.Button1.Location = New System.Drawing.Point(173, 284)

Me.Button1.Name = "Button1"

Me.Button1.Size = New System.Drawing.Size(90, 21)

Me.Button1.TabIndex = 4

Me.Button1.Text = "Brose"

'

'Button2

'

Me.Button2.Location = New System.Drawing.Point(298, 284)

Me.Button2.Name = "Button2"

Me.Button2.Size = New System.Drawing.Size(90, 21)

Me.Button2.TabIndex = 5

Me.Button2.Text = "Add"

'

'SqlCommand1

'

Me.SqlCommand1.CommandText = "dbo.[sp_InsertPhoto]"

Me.SqlCommand1.CommandType = System.Data.CommandType.StoredProcedure

Me.SqlCommand1.Connection = Me.SqlConnection1

Me.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))

Me.SqlCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50))

Me.SqlCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@image", System.Data.SqlDbType.VarBinary, 2147483647))

'

'SqlConnection1

'

Me.SqlConnection1.ConnectionString = "workstation id=HELLEN;packet size=4096;user id=sa;data source=HELLEN;persist secu" & _

"rity info=False;initial catalog=MIS"

'

'Button3

'

Me.Button3.Location = New System.Drawing.Point(403, 284)

Me.Button3.Name = "Button3"

Me.Button3.Size = New System.Drawing.Size(90, 21)

Me.Button3.TabIndex = 6

Me.Button3.Text = "Show"

'

'Button4

'

Me.Button4.Location = New System.Drawing.Point(614, 284)

Me.Button4.Name = "Button4"

Me.Button4.Size = New System.Drawing.Size(90, 21)

Me.Button4.TabIndex = 7

Me.Button4.Text = "Close"

'

'Button5

'

Me.Button5.Location = New System.Drawing.Point(509, 284)

Me.Button5.Name = "Button5"

Me.Button5.Size = New System.Drawing.Size(90, 21)

Me.Button5.TabIndex = 8

Me.Button5.Text = "Delete"

'

'photofrm

'

Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)

Me.ClientSize = New System.Drawing.Size(796, 381)

Me.Controls.Add(Me.Button5)

Me.Controls.Add(Me.Button4)

Me.Controls.Add(Me.Button3)

Me.Controls.Add(Me.Button2)

Me.Controls.Add(Me.Button1)

Me.Controls.Add(Me.PictureBox1)

Me.Controls.Add(Me.TextBox1)

Me.Controls.Add(Me.Label2)

Me.Controls.Add(Me.Label1)

Me.Name = "photofrm"

Me.Text = "photofrm"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub photofrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'dim s as Decimal

'If s = True Then

' MsgBox(Convert.ToString(Convert.ToUInt16(s)))

'End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Display Picture File

OpenFileDialog1.InitialDirectory = "d:\pic"

OpenFileDialog1.DefaultExt = "gif"

OpenFileDialog1.Filter = "Bmp Files(*.bmp)|*.bmp|Gif Files(*.gif)|*.gif|Jpg Files(*.jpg)|*.jpg"

OpenFileDialog1.ShowDialog()

PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

' To Insert Image

Dim st As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)

Dim s As String = TextBox1.Text

Dim mbr As BinaryReader = New BinaryReader(st)

Dim buffer(st.Length) As Byte

mbr.Read(buffer, 0, CInt(st.Length))

st.Close()

InsertImage(buffer, s)

End Sub

'Function For Inserting in the Procdeure in the Database

Public Function InsertImage(ByRef buffer, ByVal str)

cn = New SqlClient.SqlConnection(SqlConnection1.ConnectionString)

cn.Open()

Dim cmd As New SqlClient.SqlCommand("sp_InsertPhoto", cn)

cmd.CommandType = CommandType.StoredProcedure

cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text

cmd.Parameters.Add("@image", SqlDbType.Image).Value = buffer

cmd.ExecuteNonQuery()

MsgBox("Image inserted")

cn.Close()

End Function

Private Sub ShowImage(ByVal s As String)

cn = New SqlClient.SqlConnection(SqlConnection1.ConnectionString)

cn.Open()

Dim str As String = "SELECT photo FROM Photos WHERE name='" & s & "'"

Dim cmd As New SqlClient.SqlCommand(str, cn)

TextBox1.Text = s

Dim b() As Byte

b = cmd.ExecuteScalar()

If (b.Length > 0) Then

Dim stream As New MemoryStream(b, True)

stream.Write(b, 0, b.Length)

DrawToScale(New Bitmap(stream))

stream.Close()

End If

cn.Close()

End Sub

Private Sub DrawToScale(ByVal bmp As Image)

PictureBox1.Image = New Bitmap(bmp)

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim i As String = InputBox("Please Input Name:")

ShowImage(i)

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

Me.Dispose()

End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

cn = New SqlClient.SqlConnection(SqlConnection1.ConnectionString)

cn.Open()

Dim s As String = InputBox("Please Input Name:")

Dim cmd As New SqlClient.SqlCommand("delete from photos where name='" & s & "'", cn)

cmd.ExecuteNonQuery()

MsgBox("Image deleted")

cn.Close()

End Sub

Private Sub photofrm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

End Sub

End Class

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
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- 王朝網路 版權所有