.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Phoito resize and store in database

Posted By:Brijesh Kumar       Posted Date: March 15, 2011    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

This article refer for the create photo resize in differenet size and after store in the SQL Database.

1)  Indicate these directory above of Program:-

using System.Data.SqlClient;

using System.Drawing.Imaging;

using System.Drawing.Drawing2D;

using System.Drawing;

2)  Drag a Button named "Upload" and a File Upload control from toolbox and a gridview for the show the image.

3)  Write this code to button click:-

 string strImageName = txtName.Text.ToString();

 if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")


    string strExtension = System.IO.Path.GetExtension(FileUpload1.FileName);

    if ((strExtension.ToUpper() == ".JPG") | (strExtension.ToUpper() == ".GIF"))


     // Resize Image Before Uploading to DataBase

      System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);

      int imageHeight = imageToBeResized.Height;

      int imageWidth = imageToBeResized.Width;

      int maxHeight = 240;

      int maxWidth = 320;

      imageHeight = (imageHeight * maxWidth) / imageWidth;

      imageWidth = maxWidth;


              if (imageHeight > maxHeight)


                    imageWidth = (imageWidth * maxHeight) / imageHeight;

                    imageHeight = maxHeight;



                Bitmap bitmap = new Bitmap(imageToBeResized, imageWidth, imageHeight);

                System.IO.MemoryStream stream = new MemoryStream();

                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);

                stream.Position = 0;

                byte[] image = new byte[stream.Length + 1];

                stream.Read(image, 0, image.Length);




                // Create SQL Connection

                SqlConnection con = new SqlConnection();

                con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;


                // Create SQL Command


                SqlCommand cmd = new SqlCommand();

                cmd.CommandText = "INSERT INTO Images(ImageName,Image) VALUES (@ImageName,@Image)";

                cmd.CommandType = CommandType.Text;

                cmd.Connection = con;


                SqlParameter ImageName = new SqlParameter("@ImageName", SqlDbType.VarChar, 50);

                ImageName.Value = strImageName.ToString();



                SqlParameter UploadedImage = new SqlParameter("@Image", SqlDbType.Image, image.Length);

                UploadedImage.Value = image;



                int result = cmd.ExecuteNonQuery();


                if (result > 0)

                    lblMessage.Text = "File Uploaded";





Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend