title: C# Resize jpg image, convert to byte and save into database using varbinary
body: <p>I'm trying to resize my jpg image uploaded by using FileUpload control and convert it into byte before saving it to database (SQL Server 2008) as (varbinary(MAX)). What I have done and code show below was that I manage to convert it into byte and save into database as varbinary(MAX). I wish to know how to I resize the image before doing all those function. Do help me out. Thanks!</p>
  
  <p>Read the file</p>
  
  <pre><code>string filePath = FileUpload1.PostedFile.FileName;
  string filename = Path.GetFileName(filePath);
  string ext = Path.GetExtension(filename);
  </code></pre>
  
  <p>Set the contenttype based on File Extension</p>
  
  <pre><code>string contenttype = String.Empty;            
  switch (ext)
  {
     case ".jpg":
     contenttype = "image/jpg";
     break;
  }
  </code></pre>
  
  <p>Convert into byte and save into database using varbinary</p>
  
  <pre><code>if (contenttype != String.Empty)
  {
              Stream fs = FileUpload1.PostedFile.InputStream;
  
              BinaryReader br = new BinaryReader(fs);
  
              Byte[] bytes = br.ReadBytes((Int32)fs.Length);
  
              //insert the file into database
              string strQuery = "insert into MemberReport(username, typeofcrime, location, crdatetime, citizenreport, image1, image2, image3, image4, image5)" +
                 " values ('" + username + "','" + typeofcrime + "','" + location.Trim() + "','" + datetime + "','" + detail.Trim() + "', @Data, @Data2, @Data3, @Data4, @Data5)";
              SqlCommand cmd = new SqlCommand(strQuery);
              cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
              cmd.Parameters.Add("@Data2", SqlDbType.Binary).Value = bytes2;
              cmd.Parameters.Add("@Data3", SqlDbType.Binary).Value = bytes3;
              cmd.Parameters.Add("@Data4", SqlDbType.Binary).Value = bytes4;
              cmd.Parameters.Add("@Data5", SqlDbType.Binary).Value = bytes5;
              InsertUpdateData(cmd);
  
              lblMessage.ForeColor = System.Drawing.Color.Green;
              lblMessage.Text = "Report Sent!";
  
          }
          else
          {
              lblMessage.ForeColor = System.Drawing.Color.Red;
              lblMessage.Text = "File format not recognised." +
                " Upload Image formats";
          }
  </code></pre>
  
  <p>InsertUpdateData method</p>
  
  <pre><code>    private Boolean InsertUpdateData(SqlCommand cmd)
      {
          SqlConnection con = new SqlConnection("Data Source=localhost; Initial Catalog=project; Integrated Security=True");
          cmd.CommandType = CommandType.Text;
          cmd.Connection = con;
          try
          {
              con.Open();
              cmd.ExecuteNonQuery();
              return true;
          }
          catch (Exception ex)
          {
              Response.Write(ex.Message);
              return false;
          }
          finally
          {
              con.Close();
              con.Dispose();
          }
  </code></pre>