0

hi thanks a lot i have one code below in this code ill upload one image . convert it into byte code and store it in database .. and retrive it in gridview .. the thing is before converting it into byte code i want to resize it could u plz tell me what code i should insert here ... thanks a lot .. ...

protected void btnUpload_Click(object sender, EventArgs e)
{
 string strID= txtid.Text.ToString();
 string strImageName = txtName.Text.ToString();
 if (FileUpload1.PostedFile != null && 
     FileUpload1.PostedFile.FileName != "")
  {


   byte[] imageSize = new byte
                 [FileUpload1.PostedFile.ContentLength];

  HttpPostedFile uploadedImage = FileUpload1.PostedFile;
  uploadedImage.InputStream.Read
     (imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);

 // Create SQL Connection 
  SqlConnection con = new SqlConnection("user id=sa;password=Zoomin@123;database=salary_db;server=192.168.1.100");


 // Create SQL Command 

 SqlCommand cmd = new SqlCommand();
 cmd.CommandText = "INSERT INTO image1(ID,ImageName,Image)" +
                   " VALUES (@ID,@ImageName,@Image)";
 cmd.CommandType = CommandType.Text;
 cmd.Connection = con;


 SqlParameter ID = new SqlParameter
                   ("@ID", SqlDbType.VarChar, 50);
 ID.Value = strID.ToString();
 cmd.Parameters.Add(ID);

 SqlParameter ImageName = new SqlParameter
                     ("@ImageName", SqlDbType.VarChar, 50);
 ImageName.Value = strImageName.ToString();
 cmd.Parameters.Add(ImageName);

 SqlParameter UploadedImage = new SqlParameter
               ("@Image", SqlDbType.Image, imageSize.Length);
 UploadedImage.Value = imageSize;
 cmd.Parameters.Add(UploadedImage);
 con.Open();
 int result = cmd.ExecuteNonQuery();
 con.Close();
 if (result > 0)
 lblMessage.Text = "File Uploaded";

 GridView1.DataBind();

 }}
Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
ajaz ahmed
  • 3
  • 1
  • 2
  • 6
    Unrelated to the question, but still important: do NOT use the SA account to connect to your database, and do NOT hard code your SQL connection string inline in your code. Use the appropriate connectionStrings section in your web.config. – Wim Dec 19 '09 at 10:24
  • 1
    Somewhat related to the question: consider storing the images as files and storing the filepath in the database instead. Storing them will not only balloon the size of the database, but you'll also really hinder performance, especially if the images are large. – Daniel T. Dec 19 '09 at 10:32

2 Answers2

3

You could use the following function:

public void ResizeImage(double scaleFactor, Stream fromStream, Stream toStream)
{
    using (var image = Image.FromStream(fromStream))
    {
        var newWidth = (int)(image.Width * scaleFactor);
        var newHeight = (int)(image.Height * scaleFactor);
        using (var thumbnailBitmap = new Bitmap(newWidth, newHeight))
        using (var thumbnailGraph = Graphics.FromImage(thumbnailBitmap))
        {
            thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
            thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
            thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
            var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
            thumbnailGraph.DrawImage(image, imageRectangle);
            thumbnailBitmap.Save(toStream, image.RawFormat);
        }
    }
}

The name of the parameters should be pretty self-explanatory.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

Have a look at

Resize an Image C#

Community
  • 1
  • 1
Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284