Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report