Asp. NET implementation of the picture in binary form into the database _ practical skills

Source: Internet
Author: User

In this paper, an example is given to describe the method of asp.net implementation of the image into the database in the form of binary. In the past we are directly in the database in the image file name, has not tried to store the entire picture into the database, after a data query and testing, sorted out the following function code:

1. Create the SQL statement for the table that holds the picture:

Use [niunantest] 
go 
/****** object: Table [dbo].[ Picdata]  Script Date: 03/30/2010 14:51:58 ******/ 
SET ansi_nulls on 
go 
SET quoted_identifier 
in Go CREATE TABLE [dbo]. [Picdata] ( 
  [id] [int] IDENTITY (1,1) not NULL, 
  [content] [image] null, 
  [createdate] [datetime] NOT NULL CONSTRAINT [D F_picdata_createdate] DEFAULT (getdate ()), 
 CONSTRAINT [pk_picdata] PRIMARY KEY CLUSTERED  
( 
  [id] ASC 
)  With (Pad_index = off, Statistics_norecompute = off, Ignore_dup_key = off, Allow_row_locks = on, allow_page_locks = ON) on [PRIMARY] 
) On [PRIMARY] textimage_on [PRIMARY] 

2. The following is a code fragment that saves the picture to the database:

int len = fu. Postedfile.contentlength; Picture size 
byte[] pic = new Byte[len];//Create a byte array sized to the size of the picture, the database stores this thing 
fu. PostedFile.InputStream.Read (pic, 0, Len); Save files in upload control in binary read to pic byte array 
//  Insert Picture into database    
SqlConnection connection = new 
SqlConnection (@ "server=.\ sqlexpress;database=niunantest;uid=sa;pwd=123456 "); 
Try 
{ 
  connection. Open (); 
  SqlCommand cmd = new SqlCommand ("Insert  into  picdata  " 
  + "([content])  values  (@pic)", connection); 
  Cmd. Parameters.Add ("@pic", pic); 
  Cmd. ExecuteNonQuery (); 
  Label1.Text = "Picture inserted into database success!" "; 
 
  Image1.imageurl = "getpic.ashx?t=" + DateTime.Now.Ticks; Displays the picture that just inserted the database 
} 
finally 
{ 
  connection. Close (); 
}  
 

3. The following is a code fragment that takes a picture out of the database:

MemoryStream stream = new MemoryStream (); 
SqlConnection connection = new 
SqlConnection (@ "server=.\sqlexpress;database=niunantest;uid=sa;pwd=123456"); 
Try 
{ 
  connection. Open (); 
  SqlCommand command = new 
  SqlCommand ("select top 1 [content]  from Picdata ORDER by id DESC", connection); 
   
    byte[] image = (byte[]) command. ExecuteScalar (); 
  Stream. Write (image, 0, image. Length); 
  Bitmap Bitmap = new Bitmap (stream); 
  Context. Response.ContentType = "Image/jpeg"; 
  Bitmap. Save (context. Response.outputstream, System.Drawing.Imaging.ImageFormat.Jpeg); 
Finally 
{ 
  connection. Close (); 
  Stream. Close (); 
}


   

The principle of the program is in fact, through the flow of the image into a byte array and then stored in the database, then read the byte array from the database, and then through the byte array to create the stream, and then through the flow of the image output, found that you are stored in the database is a GIF image, and then take out the image can be converted into JPG, Because when we take out the image, we set his contenttype to be image/jpeg.

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.