locked
storing images in Database RRS feed

  • Question

  • User-651360610 posted


    Hi,

    I've written an admin section for my site so that people can upload pictures. I have managed to get this working and also to pull the data from the DB and display the images correctly.

    Only thing is that they are not the same quality as the originals. They are all slightly blurry.

    My site allows people to enter the file location into a textbox. I then use team_file.PostedFile to get the filename, length, and inputstream. which is passed to be below.

    Here's my function that writes to the database.

    private static bool insert_post_attachment_impl(
            Stream content,
            int content_length,
            string file, string content_type,
            bool UpdateExisting, int existing_id, string hyperlink, int team_id, bool isdeleted, string pictureType)
        {
            string effective_file = file;
            int effective_content_length = content_length;
            string effective_content_type = content_type;
            Stream effective_content = null;
            int bp_id = 0;
            bool bOk = false;
            try
            {
                effective_content = content;
                effective_file = file;
                effective_content_length = content_length;
                effective_content_type = content_type;
    
                MemoryStream jpgStream = new MemoryStream();
                if (content_type == "image/bmp")
                {
                    using (System.Drawing.Image img = System.Drawing.Image.FromStream(effective_content))
                    {
                        img.Save(jpgStream, ImageFormat.Jpeg);
                        effective_content = jpgStream;
                        effective_content_type = "image/pjpeg";
                        effective_content_length = Convert.ToInt32(effective_content.Length);
                        effective_content.Position = 0;
                        effective_file = file.Replace("bmp", "JPG");
                    }
                }
                try
                {
                    byte[] data = new byte[effective_content_length];
                    int bytes_read = 0;
    
                    while (bytes_read < effective_content_length)
                    {
                        int bytes_read_this_iteration = effective_content.Read(data, bytes_read, effective_content_length - bytes_read);
                        if (bytes_read_this_iteration == 0)
                        {
                            throw new Exception("Unexpectedly reached the end of the stream before all data was read.");
                        }
                        bytes_read += bytes_read_this_iteration;
                    }
                    List<SqlParameter> pCol = new List<SqlParameter>();
                    bool bOK = false;
                    SqlParameter p1 = new SqlParameter("@image", data);
                    pCol.Add(p1);
                    SqlParameter p3 = new SqlParameter("@team_id", team_id);
                    pCol.Add(p3);
                    switch (pictureType)
                    {
                        case "Team":
                            bOK = btnet.DbUtil.execute_SP("update_Team_Picture", pCol);
                            break;
                        case "AwayKit":
                            bOK = btnet.DbUtil.execute_SP("update_AwayKit_Picture", pCol);
                            break;
                        case "Kit":
                            bOK = btnet.DbUtil.execute_SP("update_Kit_Picture", pCol);
                            break;
                    }
                }
                catch
                {
                }
    
                return bOk;
            }
            finally
            {
                // If this procedure "owns" the content (instead of our caller owning it), dispose it.
                if (effective_content != null && effective_content != content)
                {
                    effective_content.Dispose();
                }
            }
        }


    Anyone with any ideas would be of great help!


    private static bool insert_post_attachment_impl(
            Stream content,
            int content_length,
            string file, string content_type,
            bool UpdateExisting, int existing_id, string hyperlink, int team_id, bool isdeleted, string pictureType)
        {
            string effective_file = file;
            int effective_content_length = content_length;
            string effective_content_type = content_type;
            Stream effective_content = null;
            int bp_id = 0;
            bool bOk = false;
            try
            {
                effective_content = content;
                effective_file = file;
                effective_content_length = content_length;
                effective_content_type = content_type;
                MemoryStream jpgStream = new MemoryStream();
                if (content_type == "image/bmp")
                {
                    using (System.Drawing.Image img = System.Drawing.Image.FromStream(effective_content))
                    {
                        img.Save(jpgStream, ImageFormat.Jpeg);
                        effective_content = jpgStream;
                        effective_content_type = "image/pjpeg";
                        effective_content_length = Convert.ToInt32(effective_content.Length);
                        effective_content.Position = 0;
                        effective_file = file.Replace("bmp", "JPG");
                    }
                }
                try
                {
                    byte[] data = new byte[effective_content_length];
                    int bytes_read = 0;
                    while (bytes_read < effective_content_length)
                    {
                        int bytes_read_this_iteration = effective_content.Read(data, bytes_read, effective_content_length - bytes_read);
                        if (bytes_read_this_iteration == 0)
                        {
                            throw new Exception("Unexpectedly reached the end of the stream before all data was read.");
                        }
                        bytes_read += bytes_read_this_iteration;
                    }
                    List<SqlParameter> pCol = new List<SqlParameter>();
                    bool bOK = false;
                    SqlParameter p1 = new SqlParameter("@image", data);
                    pCol.Add(p1);
                    SqlParameter p3 = new SqlParameter("@team_id", team_id);
                    pCol.Add(p3);
                    switch (pictureType)
                    {
                        case "Team":
                            bOK = btnet.DbUtil.execute_SP("update_Team_Picture", pCol);
                            break;
                        case "AwayKit":
                            bOK = btnet.DbUtil.execute_SP("update_AwayKit_Picture", pCol);
                            break;
                        case "Kit":
                            bOK = btnet.DbUtil.execute_SP("update_Kit_Picture", pCol);
                            break;
                    }
                }
                catch
                {
                }
                return bOk;
            }
            finally
            {
                // If this procedure "owns" the content (instead of our caller owning it), dispose it.
                if (effective_content != null && effective_content != content)
                {
                    effective_content.Dispose();
                }
            }
        }



    Thursday, November 5, 2009 6:01 AM

Answers

  • User-2137527425 posted

    Hi,

    Have you tried to save in another format..? I think that jpg reduces the quality of the image..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 5, 2009 6:44 AM

All replies

  • User-2137527425 posted

    Hi,

    Have you tried to save in another format..? I think that jpg reduces the quality of the image..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 5, 2009 6:44 AM
  • User-651360610 posted

    excellent call. Changed to PNG and all is well. Thanks

    Thursday, November 5, 2009 8:49 AM