【问题标题】:How to add same image in different resolution to different database columns in c# ASP.NET如何将不同分辨率的相同图像添加到 c# ASP.NET 中的不同数据库列
【发布时间】:2015-11-27 18:33:49
【问题描述】:

我有一个项目,页面上有一个分辨率为 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 的第一行出现异常

【问题讨论】:

  • 仅在 DB 列中存储文件名。并将单个图像存储到两个目录(如 photo-upolad-thumb 和 photo-upload-large)。
  • 没有both images。我想将相同的图像但不同的分辨率存储到不同的列。
  • 对不起,将单个图像存储到不同分辨率的不同目录中。
  • 是的。但要使用 c# 来执行此操作?????
  • 只是示例,主图像尺寸为 1024x1024。所以将主图像直接保存到 photo-upload-large 文件夹并使用 C# 调整图像大小功能将主图像调整为 400x300 并将其保存到 photo-upload-thumb 文件夹。我可以提供用于调整图像大小的 c# resize image 代码吗?

标签: c# asp.net image image-uploading httppostedfile


【解决方案1】:

试试下面的代码。

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

【讨论】:

  • System.IO.FileNotFoundException 在注释行 //Store copy image 之后。找不到文件
  • 我有编辑代码请检查或检查上传的主图像路径。
  • 参数@ITM_PATH@ITM_LARGE 的值是什么。在您的代码中,它只是文件名,而不是路径
  • 是的,两个参数值都只有文件名。当显示图像到那个时候结合路径和文件名的任何地方。
猜你喜欢
  • 1970-01-01
  • 2019-07-01
  • 2011-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-05
  • 1970-01-01
相关资源
最近更新 更多