Home > Back-end >  Resize image to store in database - ASP.NET
Resize image to store in database - ASP.NET

Time:10-06

I followed the code from this guide in order to resize an image before storing it in a database: https://www.aspsnippets.com/questions/876401/Resize-image-and-save-into-Database-with-Binary-format-using-C-and-VBNet-in-ASPNet/

Here is the code:

protected void Save(object sender, EventArgs e)
{
    if (fuImage.HasFile)
    {
        Byte[] bytes;
        string contentType = fuImage.PostedFile.ContentType;
        string fileName = Path.GetFileName(fuImage.FileName);
        string filePath = fuImage.PostedFile.FileName;
        System.Drawing.Image image = System.Drawing.Image.FromFile(filePath);
        // Resize image
        using (System.Drawing.Image thumbnail = image.GetThumbnailImage(130, 170, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                thumbnail.Save(memoryStream, ImageFormat.Png);
                bytes = new Byte[memoryStream.Length];
                memoryStream.Position = 0;
                memoryStream.Read(bytes, 0, (int)bytes.Length);
            }
        }
        // Insert uploaded image to Database
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            string query = "INSERT INTO tblFiles1 VALUES (@Name, @ContentType, @Data)";
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@Name", fileName);
                cmd.Parameters.AddWithValue("@ContentType", contentType);
                cmd.Parameters.AddWithValue("@Data", bytes);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
    // Display image after upload to Database
    Image1.Visible = true;
    byte[] byteData = (byte[])GetData("SELECT Data FROM tblFiles1").Rows[0]["Data"];
    string base64String = Convert.ToBase64String(byteData, 0, byteData.Length);
    Image1.ImageUrl = "data:image/png;base64,"   base64String;
}
 
 
public bool ThumbnailCallback()
{
    return false;
}

The problem I am getting however, is once I select an image using the FileUploader and try to run the save method, I get a System.IO.FileNotFoundException at the line:

 System.Drawing.Image image = System.Drawing.Image.FromFile(filePath);

Thanks in advance!

CodePudding user response:

The FileName property is the file name property from the client (https://docs.microsoft.com/en-us/dotnet/api/system.web.httppostedfilebase.filename?view=netframework-4.8#System_Web_HttpPostedFileBase_FileName) so you can't load the file using that property. I can upload an image from c:\yfeyhcdrt\image.png, but this folder will probably not exist on your server. You should load it using the InputStream property instead using https://docs.microsoft.com/en-us/dotnet/api/system.drawing.image.fromstream?view=windowsdesktop-5.0

  • Related