-1

I have a project in which, there is a Images on page with 400*300 resolution. When I hover Image, I have <a> and it opens as a full screen.

Now, when I upload an image, i save it to database column ITM_PATH. And there is another column ITM_LARGE. Now I want to do that, if I upload an image, the simple image will be stored at ITM_PATH and the same image but with 2400*1594 resolution in ITM_LARGE column. I have searched this but I got no solution.

Code for Upload Image:

protected void btnSubmit_Click(object sender, EventArgs e)
        {

            HttpFileCollection fileCollection = Request.Files;
            string fileName="";
            string largeFile = "";
            for (int i = 0; i < fileCollection.Count; i++)
            {
                HttpPostedFile uploadfile = fileCollection[i];
                fileName = Path.GetFileName(uploadfile.FileName);
                if (uploadfile.ContentLength > 0)
                {
                    uploadfile.SaveAs(Server.MapPath("~/Photo-Upload/") + fileName);
                    lblMessage.Text += fileName + "Saved Successfully<br>";
                    fileName = "Photo-Upload/" + fileName;
                }
            }
            using (Bitmap bitmap = (Bitmap)System.Drawing.Image.FromFile(fileName))
            {
                using (Bitmap newbitmap = new Bitmap(bitmap))
                {
                    newbitmap.SetResolution(2400, 1594);
                    newbitmap.Save(fileName + "Large", ImageFormat.Jpeg);
                    largeFile = newbitmap.ToString(); ;
                }
            }


            int _Itm_Id = GetMaxNo();
            if (_Itm_Id > 0)
            {
                ConnectDataBase();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "SP_GENERAL";
                cmd.Parameters.AddWithValue("@SP_STATUS", "INSERT_ITM");
                cmd.Parameters.AddWithValue("@ITM_ID", _Itm_Id);
                cmd.Parameters.AddWithValue("@ITM_CAT_ID", ddlCategory.SelectedValue);
                cmd.Parameters.AddWithValue("@ITM_NAME", txtItemName.Text);
                cmd.Parameters.AddWithValue("@ITM_PATH", fileName);
                cmd.Parameters.AddWithValue("@ITM_LARGE", largeFile);
                //cmd.Parameters.AddWithValue("@ITM_PRICE", Convert.ToDecimal(txtPrice.Text));
                int retval = cmd.ExecuteNonQuery();
                if (retval > 0)
                    lblMessage.Text = "Record Successfully Inserted!!!";
                else
                    lblMessage.Text = "Server Error!!! Please Try Again...";
                ClearAll();
            }
        }

i am retrieving image in repeater by getting its url from database column.

EDIT

I searched for the same, and found bitmap as a solution, so i added that code, but file not found exception is coming. Any other idea or edits??

Exception is coming at first line of bitmap

halfer
  • 19,824
  • 17
  • 99
  • 186
Dinav Ahire
  • 581
  • 1
  • 10
  • 30

1 Answers1

1

Try Below Code.

protected void btnSubmit_Click(object sender, EventArgs e) {

        HttpFileCollection fileCollection = Request.Files;
        string fileName = "";
        for (int i = 0; i < fileCollection.Count; i++) {
            HttpPostedFile uploadfile = fileCollection[i];
            fileName = Path.GetFileName(uploadfile.FileName);
            if (uploadfile.ContentLength > 0) {
                uploadfile.SaveAs(Server.MapPath("~/Photo-Upload-Large/") + fileName);
                lblMessage.Text += fileName + "Saved Successfully<br>";

                //Store Crope Image
                System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath("~/Photo-Upload-Large/") + fileName);
                int newwidthimg = 400;
                float AspectRatio = (float)image.Size.Width / (float)image.Size.Height;
                int newHeight = Convert.ToInt32(newwidthimg / AspectRatio);
                Bitmap thumbnailBitmap = new Bitmap(newwidthimg, newHeight);
                Graphics thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
                thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
                thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
                thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
                var imageRectangle = new Rectangle(0, 0, newwidthimg, newHeight);
                thumbnailGraph.DrawImage(image, imageRectangle);
                thumbnailBitmap.Save(Server.MapPath("~/Photo-Upload-Thumb/"), ImageFormat.Jpeg);
                thumbnailGraph.Dispose();
                thumbnailBitmap.Dispose();
                image.Dispose();     
            }
        }

        int _Itm_Id = GetMaxNo();
        if (_Itm_Id > 0) {
            ConnectDataBase();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "SP_GENERAL";
            cmd.Parameters.AddWithValue("@SP_STATUS", "INSERT_ITM");
            cmd.Parameters.AddWithValue("@ITM_ID", _Itm_Id);
            cmd.Parameters.AddWithValue("@ITM_CAT_ID", ddlCategory.SelectedValue);
            cmd.Parameters.AddWithValue("@ITM_NAME", txtItemName.Text);
            cmd.Parameters.AddWithValue("@ITM_PATH", fileName);
            cmd.Parameters.AddWithValue("@ITM_LARGE", fileName);
            //cmd.Parameters.AddWithValue("@ITM_PRICE", Convert.ToDecimal(txtPrice.Text));
            int retval = cmd.ExecuteNonQuery();
            if (retval > 0)
                lblMessage.Text = "Record Successfully Inserted!!!";
            else
                lblMessage.Text = "Server Error!!! Please Try Again...";
            ClearAll();
        }
    }
Kaushik Maheta
  • 1,741
  • 1
  • 18
  • 27