Use C # To Read and Write images from and to the database using winform

Source: Internet
Author: User
Tags strong password

(1)

Write:

Stream MS;
Byte [] picbyte;
Openfiledialog1.filter = systemconst. image_file_filter;
If (this. openfiledialog1.showdialog () = dialogresult. OK)
{
If (MS = openfiledialog1.openfile ())! = NULL)
{
Picbyte = new byte [Ms. Length];
Ms. Position = 0;
Ms. Read (picbyte, 0, convert. toint32 (Ms. Length ));
Ms. Dispose ();
}
}
After picbyte [] is obtained, the database can be inserted. Image Type of the corresponding database
Read:
If (Dt. Rows [0] ["photo"]! = Dbnull. value)
{
Byte [] mybyte = (byte []) dt. Rows [0] ["photo"];
Memorystream MS = new memorystream (mybyte );
This. pb_employee.image = image. fromstream (MS, true );
}

 

(2)

Storage:
Sqlconnection con = new sqlconnection ("Server = darkover; uid = <username>; Pwd = <strong password>; database = northwind ");
Sqldataadapter da = new sqldataadapter ("select * From myimages", con );
Sqlcommandbuilder mycb = new sqlcommandbuilder (DA );
Dataset DS = new dataset ("myimages ");
Da. missingschemaaction = missingschemaaction. addwithkey;
Filestream FS = new filestream (@ "C: \ winnt \ gone fishing. BMP", filemode. openorcreate, fileaccess. Read );
Byte [] mydata = new byte [fs. Length];
FS. Read (mydata, 0, system. Convert. toint32 (FS. Length ));
FS. Close ();
Da. Fill (DS, "myimages ");
Datarow myrow;
Myrow = Ds. Tables ["myimages"]. newrow ();
Myrow ["Description"] = "This wocould be description text ";
Myrow ["imgfield"] = mydata;
DS. Tables ["myimages"]. Rows. Add (myrow );
Da. Update (DS, "myimages ");
Con. Close ();
Take:
Sqlconnection con = new sqlconnection ("Server = darkover; uid = <username>; Pwd = <strong password>; database = northwind ");
Sqldataadapter da = new sqldataadapter ("select * From myimages", con );
Sqlcommandbuilder mycb = new sqlcommandbuilder (DA );
Dataset DS = new dataset ("myimages ");
Byte [] mydata = new byte [0];
Da. Fill (DS, "myimages ");
Datarow myrow;
Myrow = Ds. Tables ["myimages"]. Rows [0];
Mydata = (byte []) myrow ["imgfield"];
Int arraysize = new int ();
Arraysize = mydata. getupperbound (0 );
Filestream FS = new filestream (@ "C: \ winnt \ gone fishing2.bmp", filemode. openorcreate, fileaccess. Write );
FS. Write (mydata, 0, arraysize );
FS. Close ();

 

(3)

Add Image
Oledbconnection mycnn = new oledbconnection ("provider = Microsoft. Jet. oledb.4.0; Data Source = F: \ Dazhu. mdb ");
Mycnn. open ();
Oledbcommand mycmd = new oledbcommand ("Update info set picture = @ A", mycnn );
Filestream mystream = new filestream ("F: \ 1.jpg", filemode. Open, fileaccess. Read );
Long Len = mystream. length;
Mycmd. Parameters. Add ("@ A", oledbtype. Binary, (INT) Len, "picture ");
Mycmd. Parameters ["@ A"]. Direction = system. Data. parameterdirection. input;
Byte [] box = new byte [Len];
Mystream. Read (box, 0, (INT) Len );
Mycmd. Parameters ["@ A"]. value = box;
// Update
Mycmd. executenonquery ();
MessageBox. Show ("OK ");
Mystream. Close ();
Mycnn. Close ();

Read Image
Oledbconnection mycnn = new oledbconnection ("provider = Microsoft. Jet. oledb.4.0; Data Source = F: \ Dazhu. mdb ");
Mycnn. open ();
MessageBox. Show ("OK. mycnn. Open ");
Oledbcommand mycmd = new oledbcommand ("select * from Info", mycnn );
Oledbdatareader myrd = mycmd. executereader ();
If (myrd. Read ())
{
Byte [] box = (byte []) myrd ["picture"];
Stream stream1 = new memorystream (box );
This. picturebox2.image = system. Drawing. image. fromstream (stream1 );
Stream1.close ();
}
Mycnn. Close ();

 

(4)

// Select and display the image
Private void menuitem4_click (Object sender, system. eventargs E)
{
Dlgaddpic. filter = "JPEG image file (*. JPG ;*. JPEG) | *. JPG ;*. JPEG | Windows bitmap file (*. BMP) | *. BMP | GIF Computer Service (*. GIF) | *. GIF | all files (*. *) | *. *";
If (dlgaddpic. showdialog () = dialogresult. OK)
{
If (picsmall. image! = NULL)
{
Picsmall. image. Dispose ();
Picbig. image. Dispose ();
Picsmall. Image = NULL;
Picbig. Image = NULL;
}
Picsmall. Image = image. fromfile (dlgaddpic. filename );
Picbig. Image = image. fromfile (dlgaddpic. filename );
Txtpicname. Text = dlgaddpic. filename. substring (dlgaddpic. filename. lastindexof (@ "\", dlgaddpic. filename. length, dlgaddpic. filename. Length) + 1 );
}
}
// Save and add it to the database
Private void menuitem15_click (Object sender, system. eventargs E)
{
If (cbopictype. Text = "")
{
MessageBox. Show ("Please confirm the image category! ");
Return;
}
Memorystream mstream = new memorystream ();
Try
{
If (txtpicname. Text. Length <5) return;
Switch (txtpicname. Text. substring (txtpicname. Text. Length-3). toupper ())
{
Case "jpg ":
Case "Peg ":
Picbig. image. Save (mstream, imageformat. JPEG );
Picbig. image. Save ()
Break;
Case "GIF ":
Picbig. image. Save (mstream, imageformat. GIF );
Break;
Case "BMP ":
Picbig. image. Save (mstream, imageformat. BMP );
Break;
}
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
Byte [] picdata = new byte [mstream. Length];
Mstream. Position = 0;
Mstream. Read (picdata, 0, convert. toint32 (mstream. Length ));
Mstream. Close ();
Mstream = NULL;
String strinsertsql = "insert into" + cbopictype. text + "(name, PIC) values ('" + txtpicname. text + picdata. length. tostring () + "', @ pic )";
Cmd. commandtext = strinsertsql;
Oledbparameter parampic = new oledbparameter ("@ pic", oledbtype. varbinary );
Parampic. sourcecolumn = "pic ";
Parampic. value = picdata;

Cmd. Parameters. Clear ();
Cmd. Parameters. Add (parampic );
Try
{
Cmd. executenonquery ();
MessageBox. Show ("added successfully! ");
String strsql = "select top 1 ID from" + cbopictype. Text + "order by id desc ";
Cmd. commandtext = strsql;
Cbopicid. Items. Add (CMD. executescalar (). tostring ());
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}

 

(5)

// Number of uploaded photos
Try
{
If (path! = "")
{
Fileinfo Fi = new fileinfo (PATH );
Int imgdatalen = convert. toint32 (Fi. Length );
Byte [] imadata = new byte [imgdatalen];
Stream imgdatastream = Fi. openread ();
Imgdatastream. Read (imadata, 0, imgdatalen );
EMP. ZP = imadata;
}
}
Catch (exception ee)
{
MessageBox. Show (EE. Message. tostring ());
}
Click here for picturebox:
Private void picturebox#click (Object sender, system. eventargs E)
{
Try
{
Bitmap myimage;
Openfiledialog open = new openfiledialog ();
Open. showdialog ();
Path = open. filename;
If (Path = "")
{
Return;
}
Picturebox1.sizemode = pictureboxsizemode. stretchimage;
Myimage = new Bitmap (PATH );
Picturebox1.clientsize = new size (picturebox1.width, picturebox1.height );
Picturebox1.image = (image) myimage;
}
Catch (exception ee)
{
MessageBox. Show (EE. Message. tostring ());
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.