Get the App
SLTechnology News&Howtos  ›  Database  › 

C # reads l pictures and saves pictures from SQL server database

Shulou Source: shulou.com Published: 2022-06-01 14:30:11 09月21日 Update

1. The basic method of obtaining binary values from pictures: Image.Save method (String,   ImageFormat)

This writes the saved Image to the format specified in the specified file.

Namespace: System.Drawing

Assemblies: System.Drawing (located in System.Drawing.dll)

Syntax:

Public void Save (string filename, ImageFormat format)

Parameters.

Filename

Type: System.String

A string containing the name Image to save this file.

Format

Type: System.Drawing.Imaging.ImageFormat

The Image used for this ImageFormat. (including .JGP, png, etc.)

Abnormal

ArgumentNullException: filename or format is null.

ExternalException: save the image in the wrong image format. -or-the image has been saved to the same file from creating it.

2. Bitmap.Save method

Named space: System.Drawing

Component: System.Drawing (in System.Drawing.dll)

Multi-list:

1. Save (Stream,   ImageCodecInfo,   EncoderParameters): save this image to the specified data stream using the specified editor and image editor parameters. (inherited from Image.)

2. Save (Stream,   ImageFormat) saves the image to the specified data stream in the specified format. (inherited from Image.)

3. Save (String) this will save the Image to the specified file or stream. (inherited from Image.)

4. Save (String,   ImageCodecInfo,   EncoderParameters) this will save the Image to the specified file to the specified editor and image editor parameters. (inherited from Image.)

5. Save (String,   ImageFormat) this will save the Image to the format specified in the specified file. (inherited from Image.)

3. Examples of saving pictures in a database and restoring programs from a database:

/ / Save pictures

Private void button1_Click (object sender, EventArgs e)

{

# region / / read pictures from a file

FileStream fs = new FileStream (@ "D:\ I\ 2017 data\ SPC\ SPC\ 30.jpg", FileMode.Open, FileAccess.Read)

Byte [] imagebytes = new byte [fs.Length]

BinaryReader br = new BinaryReader (fs)

Imagebytes = br.ReadBytes (Convert.ToInt32 (fs.Length))

SqlConnection mycn = new SqlConnection ("server=192.168.1.14;database=fengyp;uid=sa;pwd=")

Mycn.Open ()

SqlCommand com = new SqlCommand ("insert into dbo. Experimental picture access values (10 recording imageList)", mycn)

Com.Parameters.Add ("ImageList", SqlDbType.Image); com.Parameters ["ImageList"]. Value = imagebytes; com.ExecuteNonQuery (); mycn.Close (); # endregion # region / / read pictures from picturebox / / convert Image to stream data and save as byte [] MemoryStream mstream = new MemoryStream () PictureBox1.Image.Save (mstream, System.Drawing.Imaging.ImageFormat.Bmp); byte [] bytes = new Byte [mstream.Length]; mstream.Position = 0; mstream.Read (bytes, 0, bytes.Length); mstream.Close (); SqlConnection mycn = new SqlConnection ("server=192.168.1.14;database=fengyp;uid=sa;pwd="); mycn.Open ()

SqlCommand com = new SqlCommand ("insert into dbo. Experimental picture access values (11 referenced ImageList)", mycn)

Com.Parameters.Add ("ImageList", SqlDbType.Image); com.Parameters ["ImageList"]. Value = bytes; com.ExecuteNonQuery (); mycn.Close (); # endregion} / / read pictures from the database and restore private void button2_Click (object sender, EventArgs e) {byte [] imagebytes = null / / Open the database SqlConnection con = new SqlConnection ("server=192.168.1.14;database=fengyp;uid=sa;pwd="); con.Open (); SqlCommand com = new SqlCommand ("select picture from dbo. Test picture access where serial number = 10 ", con); SqlDataReader dr = com.ExecuteReader (); while (dr.Read ()) {imagebytes = (byte []) dr.GetValue (0);} dr.Close (); com.Clone (); con.Close (); MemoryStream ms = new MemoryStream (imagebytes); Bitmap bmpt = new Bitmap (ms) PictureBox2.Image = bmpt;}

Tags: Picture data format database image file image method access experiment middle finger program binary parameter name character string serial number example space Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Apple Docker Shulou Technology