0

I would like to resize an image before uploading it to a database. How would I do that? Code:

        private void SelectBtn_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFile = new OpenFileDialog();

        if (openFile.ShowDialog() == DialogResult.OK)
        {
            imagePath = openFile.FileName;
        }
        FileStream fs1 = new FileStream(imagePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        image = new byte[fs1.Length];
        fs1.Read(image, 0, Convert.ToInt32(fs1.Length));
        fs1.Close();
        MemoryStream stream = new MemoryStream(image);
        avatarPb.Image = Image.FromStream(stream);
        UpdateBtn.Enabled = true;
    }
    private void UpdateBtn_Click(object sender, EventArgs e)
    {
        using (SqlConnection connection = new SqlConnection(con))
        {
            connection.Open();
            using (SqlCommand cmd = new SqlCommand("UPDATE UserDetails SET Avatar =@avatar WHERE Email =@email", connection))
            {
                cmd.Parameters.AddWithValue("@email", Properties.Settings.Default.Email);
                SqlParameter prm = new SqlParameter("@avatar", SqlDbType.VarBinary, image.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, image);
                cmd.Parameters.Add(prm);
                cmd.ExecuteNonQuery();
            }
            connection.Close();
        }
    }

I want resize images before they are uploaded to the database. Thanks a bunch!

richardj97
  • 79
  • 1
  • 10
  • If you are just saving something larger than 128x128, consider renaming and saving the image to some folder and saving just that filename in the db. Its faster then encoding/decoding the image and bloats the db much less. You can do things like encode the PK and name in the name. – Ňɏssa Pøngjǣrdenlarp Jul 03 '16 at 20:34
  • In This [question](http://stackoverflow.com/questions/1582499/resize-image-gdi-graphics-net/1587486#1587486),I think you'll have some answers, – Malitha Shan Jul 03 '16 at 20:38
  • If you search your question title *C# Resizing an image* you will see the linked post as first result of both [Bing](http://www.bing.com/search?q=C%23+Resizing+an+image) and [Google](https://www.google.com/#q=C%23+Resizing+an+image). Also when asking a question, after typing the title don't neglect the offer: *Questions that may already have your answer*. – Reza Aghaei Jul 03 '16 at 20:53

0 Answers0