C # reading L pictures and storing pictures from SQL Server database

Source: Internet
Author: User

This example focuses on how to store pictures in a database. Put the picture into the database, first to create a table in the database, the stored image of the field type is set to the image type, with the FileStream class, BinaryReader to read the picture in the form of bytes, assigned to a byte array, The data is then saved to the database using the ExecuteNonQuery () method of the Ado.sqlcommand object. The main code is as follows:

private void Button1_Click (object sender, EventArgs e)

{

Openfiledialog1.filter = "*jpg|*. jpg|*. gif|*. gif|*. bmp|*. BMP ";

if (Openfiledialog1.showdialog () ==dialogresult.ok)

{

String FullPath =openfiledialog1.filename;//file path

FileStream fs = new FileStream (FullPath, FileMode.Open);

Byte[] Imagebytes =new byte[fs. Length];

BinaryReader br = new BinaryReader (FS);

Imagebytes = Br. Readbytes (Convert.ToInt32 (fs. Length));

Open Database

SqlConnection con = new SqlConnection ("server= (local); uid=sa;pwd=;d atabase=db_05");

Con. Open ();

SqlCommand com = new SqlCommand ("INSERT into tb_08 values (@ImageList)", con);

Com. Parameters.Add ("ImageList", sqldbtype.image);

Com. parameters["ImageList"]. Value = imagebytes;

Com. ExecuteNonQuery ();

Con. Close ();

}

}

This example mainly describes how to read the pictures from the database. The main implementation of this example is to use SqlDataReader to read the value of the image field from the database, assign to a byte[] byte array, and then use the MemoryStream class and bitmap to read the picture. The main code is as follows:

private void Button1_Click (object sender, EventArgs e)

{

byte[] imagebytes = null;

Open Database

SqlConnection con = new SqlConnection ("server= (local); uid=sa;pwd=;d atabase=db_05");

Con. Open ();

SqlCommand com = new SqlCommand ("Select Top 1* from tb_09", con);

SqlDataReader dr = com. ExecuteReader ();

while (Dr. Read ())

{

Imagebytes = (byte[]) Dr. GetValue (1);

}

Dr. Close ();

Com. Clone ();

Con. Close ();

MemoryStream ms = new MemoryStream (imagebytes);

Bitmap bmpt = new Bitmap (MS);

pictureBox1.Image = bmpt;

}

This example mainly describes how to allow only the specified picture format to be entered. Open the picture file with the OpenFileDialog control, as long as you specify the Filter property of the OpenFileDialog control as a specific picture format. For example, to open a picture of a. bmp file, the main code is as follows:

This.openFileDialog1.Filter = "BMP file (*.bmp) |*.bmp";

When you enter a picture with the PictureBox control, to unify the image size, simply set the control's SizeMode property value to StretchImage, and the StretchImage value indicates that the size of the image will be resized to the size of the control. In this way, the size of the picture is uniform.

C # reading L pictures and storing pictures from SQL Server database

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.