-1

我有一个项目,页面上有一个分辨率为 400*300 的图像。当我将图像悬停时,我有<a>并且它以全屏形式打开。

现在,当我上传图像时,我将其保存到数据库列ITM_PATH。还有另一列ITM_LARGE。现在我想这样做,如果我上传图像,简单图像将存储在ITM_PATH相同的图像中,但列中的分辨率为 2400*1594 ITM_LARGE。我已经搜索过这个,但我没有解决方案。

上传图片代码:

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();
            }
        }

我通过从数据库列中获取其 url 来检索中继器中的图像。

编辑

我搜索了相同的内容,并找到bitmap了解决方案,所以我添加了该代码,但file not found异常即将到来。任何其他想法或编辑?

异常出现在第一行bitmap

4

1 回答 1

1

试试下面的代码。

受保护的无效btnSubmit_Click(对象发送者,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();
        }
    }
于 2015-09-02T10:38:57.467 回答