如何在c# ASP.NET中为不同的数据库列添加不同分辨率的相同图像

本文关键字:数据库 添加 分辨率 图像 NET ASP | 更新日期: 2023-09-27 18:10:53

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

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

图片上传代码:
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

如何在c# ASP.NET中为不同的数据库列添加不同分辨率的相同图像

第一行

试试下面的代码。

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