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>
/// Abstract description of 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>
/// The number of changes required by the design tool.
/// </Summary>
Private system. componentmodel. Container components = NULL;
Sqlconnection conn = new sqlconnection ("Data Source = localhost; Integrated Security = sspi; initial catalog = mis ");
Public picture ()
{
//
// Required features supported by the Windows Form Design Tool
//
Initializecomponent ();
//
// Todo: After the initializecomponent call, add Any constructor Handler
//
}
/// <Summary>
/// Clear any resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region windows Form Design Tool
/// <Summary>
/// The method required for the design tool support-do not use the program programming tool to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. openfiledlg = new system. Windows. Forms. openfiledialog ();
This. label1 = new system. Windows. Forms. Label ();
This.txt file = new system. Windows. Forms. Textbox ();
This. btnbrowse = new system. Windows. Forms. Button ();
This. btninsert = new system. Windows. Forms. Button ();
This.txt name = 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.txt file. Location = new system. Drawing. Point (88, 16 );
This.txt file. Name = "txtfile ";
This.txt file. size = new system. Drawing. Size (384, 22 );
This.txt file. tabindex = 1;
This.txt file. 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.txt name. Location = new system. Drawing. Point (112, 88 );
This.txt name. Name = "txtname ";
This.txt name. tabindex = 4;
This.txt name. 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. button#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.txt name );
This. Controls. Add (this. btninsert );
This. Controls. Add (this. btnbrowse );
This.controls.add(this.txt file );
This. Controls. Add (this. label1 );
This. Name = "picture ";
This. Text = "operate shards ";
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 button#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 "program generated by Windows form design tool"
Public sub new ()
Mybase. New ()
'The call is required by the Windows form design tool.
Initializecomponent ()
'Add all the initial settings after the initializecomponent () call.
End sub
'Form override dispose to clear the component list.
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
'The necessity for Windows form design tools
Private components as system. componentmodel. icontainer
'Note: The following programs are required for the Windows form design tool.
'You can use the Windows form design tool for modification.
'Do not use the program Program program compiler to modify these programs.
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. openfiledig1
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 button#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