Upload picture, resize and save in SQL database - THE ANSWER!

Last post 11-14-2009 12:20 PM by Ritushido. 4 replies.

Sort Posts:

  • Upload picture, resize and save in SQL database - THE ANSWER!

    10-10-2009, 12:38 PM

    Hey guys and gals,

    I have been looking for a solution for this all over the web and it took me a couple of hours to figure it out. Finally I did it. So, here's my code to upload a picture file using the fileupload control, resize the file and then store it to a SQL Server database.

            If FleUpload.HasFile Then
                Dim fileName As String = Server.HtmlEncode(FleUpload.FileName)
                Dim extension As String = System.IO.Path.GetExtension(fileName)
                If (extension.ToUpper = ".JPG") Or (extension.ToUpper = ".GIF") Then
    
                    '**** Resize image section ****
                    Dim image_file As System.Drawing.Image = System.Drawing.Image.FromStream(FleUpload.PostedFile.InputStream)
                    Dim image_height As Integer = image_file.Height
                    Dim image_width As Integer = image_file.Width
                    Dim max_height As Integer = 120
                    Dim max_width As Integer = 160
    
    
                    image_height = (image_height * max_width) / image_width
                    image_width = max_width
    
                    If image_height > max_height Then
                        image_width = (image_width * max_height) / image_height
                        image_height = max_height
                    Else
                    End If
    
    
                    Dim bitmap_file As New Bitmap(image_file, image_width, image_height)
                    Dim stream As New System.IO.MemoryStream
    
                    bitmap_file.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
                    stream.Position = 0
    
                    Dim data(stream.Length) As Byte
                    stream.Read(data, 0, stream.Length)
                    '**** End resize image section ****
    
    
                    Dim myConn As New SqlConnection(ConfigurationManager.ConnectionStrings("cs").ConnectionString)
                    Dim mycmd As New SqlCommand("se_equipmentimages_insert", myConn)
                    mycmd.CommandType = CommandType.StoredProcedure
    
                    mycmd.Parameters.AddWithValue("@equipment_id", id)
                    mycmd.Parameters.AddWithValue("@image_file", data)
    
                    Try
                        myConn.Open()
                        mycmd.ExecuteNonQuery()
                    Catch ex As Exception
                    Finally
                        myConn.Close()
                    End Try
    
                Else
                    lblError.Text = "Please only upload .jpg or .gif files"
                    lblError.Visible = True
                End If
            Else
                lblError.Text = "No file selected"
                lblError.Visible = True
            End If
    


     

    I hope this helps someone!

  • Re: Upload picture, resize and save in SQL database - THE ANSWER!

    10-14-2009, 6:05 AM

    Hi,

    Thank you so much for sharing your resolution. It will definitely benefit other community members.

    Jian Kang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Upload picture, resize and save in SQL database - THE ANSWER!

    10-15-2009, 5:41 PM
    • Member
      4 point Member
    • gcodfish
    • Member since 09-19-2009, 3:24 AM
    • Posts 4

    Any chance you could up the C# Code?

  • Re: Upload picture, resize and save in SQL database - THE ANSWER!

    11-04-2009, 7:36 AM
    • Member
      4 point Member
    • gcodfish
    • Member since 09-19-2009, 3:24 AM
    • Posts 4

    Hi Christian


    I have had to convert you code to c# and am having some problems with the     stream.read(data, 0 stream.Length)  portion of code


    this is what it looks like

     System.Drawing.Image newImage = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
                    int image_height = newImage.Height;
                    int image_width = newImage.Width;
                    int max_height = 100;
                    int max_width = 100;
                    if (image_height > max_height)
                    {
                        image_width = (image_width * max_height) / image_height;
                        image_height = max_height;
                    }
                    else { }
                    Bitmap bitmap_file = new Bitmap(newImage, image_width, image_height);
                    System.IO.MemoryStream stream = new System.IO.MemoryStream();
                    stream.Position = 0;
                    bitmap_file.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    
                    byte[] data = new Byte[stream.Length+ 1];
                    stream.Read(data, 0, stream.Length);




    I am getting the error cannot convert long to int for the last overload on the last line of the above code.

    Could you help at all?


    Coddy

  • Re: Upload picture, resize and save in SQL database - THE ANSWER!

    11-14-2009, 12:20 PM
    • Member
      10 point Member
    • Ritushido
    • Member since 06-13-2009, 12:28 PM
    • Spain
    • Posts 62

    I would also appreciate the c# version as this is exactly what I need right now! Thanks.


    EDIT: I found an online converter so not sure if this code would work, but could you please tell me where I am supposed to start the if statement? (Page Load etc.??)

    {
        if (FleUpload.HasFile) {
            string fileName = Server.HtmlEncode(FleUpload.FileName);
            string extension = System.IO.Path.GetExtension(fileName);
            if ((extension.ToUpper == ".JPG") | (extension.ToUpper == ".GIF")) {
                
                //**** Resize image section ****
                System.Drawing.Image image_file = System.Drawing.Image.FromStream(FleUpload.PostedFile.InputStream);
                int image_height = image_file.Height;
                int image_width = image_file.Width;
                int max_height = 120;
                int max_width = 160;
                
                
                image_height = (image_height * max_width) / image_width;
                image_width = max_width;
                
                if (image_height > max_height) {
                    image_width = (image_width * max_height) / image_height;
                    image_height = max_height;
                }
                else {
                }
                
                
                Bitmap bitmap_file = new Bitmap(image_file, image_width, image_height);
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                
                bitmap_file.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                stream.Position = 0;
                
                byte[] data = new byte[stream.Length + 1];
                stream.Read(data, 0, stream.Length);
                //**** End resize image section ****
                
                
                SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings("cs").ConnectionString);
                SqlCommand mycmd = new SqlCommand("se_equipmentimages_insert", myConn);
                mycmd.CommandType = CommandType.StoredProcedure;
                
                mycmd.Parameters.AddWithValue("@equipment_id", id);
                mycmd.Parameters.AddWithValue("@image_file", data);
                
                try {
                    myConn.Open();
                    mycmd.ExecuteNonQuery();
                }
                catch (Exception ex) {
                }
                finally {
                    myConn.Close();
                    
                }
            }
            else {
                lblError.Text = "Please only upload .jpg or .gif files";
                lblError.Visible = true;
            }
        }
        else {
            lblError.Text = "No file selected";
            lblError.Visible = true;
        }
    }
    


    Regards,
    James King
Page 1 of 1 (5 items)