Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
1.67/5 (2 votes)
See more:
good day guys and girls, i insert images into mysql database using the following blocks of code

C#
protected void ad3upload_Click(object sender, EventArgs e)
        {
            if (FileUpload6.HasFile)
            {
                string fileName = FileUpload6.FileName;
                string path = fileName;
                TextBox13.Text = fileName;
                FileUpload6.SaveAs(Server.MapPath(path));
                pass3img.ImageUrl = path;

                //this gets admin1passport
                Stream fs3 = FileUpload6.PostedFile.InputStream;
                BinaryReader br3 = new BinaryReader(fs3);
                Byte[] bytes3 = br3.ReadBytes((Int32)fs3.Length);

                try
                {
                    MySqlConnection conn = new MySqlConnection(connection);
                    MySqlCommand ins = new MySqlCommand("update tables.admintable set Passport = @img where Name = '" + newname.Text + "'", conn);
                    MySqlDataReader read;
                    conn.Open();
                    ins.Parameters.Add("@img", MySqlDbType.Binary).Value = bytes3;
                    read = ins.ExecuteReader();
                    conn.Close();
                    Label84.ForeColor = System.Drawing.Color.Green;
                    Label84.Text = "Successful Upload";
                }
                catch (MySqlException)
                {
                    Label84.ForeColor = System.Drawing.Color.Red;
                    Label84.Text = "Failed Upload, Please Try Agin";
                }
                catch (Exception)
                {
                    Label84.ForeColor = System.Drawing.Color.Red;
                    Label84.Text = "Failed Upload, Please Try Agin";
                }
            }
        }
it succesfully uploads buy when i recall the image from the database through these set of codes
protected void adminListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                MySqlConnection conn = new MySqlConnection(connection);
                MySqlCommand select = new MySqlCommand("select Passport from tables.admintable where Name = '" + Label33.Text + "'", conn);
                MySqlDataReader read;
                conn.Open();
                read = select.ExecuteReader();
                while (read.Read())
                {
                    byte[] passportImage1 = (byte[])(read["Passport"]);
                    if (adminListBox.SelectedIndex.Equals(0))
                    {
                        if (passportImage1 == null)
                        {
                            adminImage.ImageUrl = null;
                        }
                        else
                        {
                            MemoryStream ms = new MemoryStream(passportImage1); // this holds the image gotten from the database
                            string base64string = Convert.ToBase64String(passportImage1, 0, passportImage1.Length);
                            adminImage.ImageUrl = "data:image/png;base64," + base64string;
                        }
                    }
                }
                read.Close();
                conn.Close();
            catch (MySqlException)
            {
                Label84.ForeColor = System.Drawing.Color.Red;
                Label84.Text = "Failed Upload, Please Try Agin";
            }
            catch (Exception)
            {
                Label84.ForeColor = System.Drawing.Color.Red;
                Label84.Text = "Failed Upload, Please Try Agin";
            }

            }
        }

this gets the image quite alright but it looks like its a cracked image on display, like its corrupted.
i want it to show clearly. please what am i missing?
Posted
Updated 26-Feb-15 21:58pm
v2
Comments
Afzaal Ahmad Zeeshan 27-Feb-15 4:19am    
By uploading and using good quality images only.
Sinisa Hajnal 28-Feb-15 3:16am    
Is your image (the one you're uploading) of good quality? Do you compress it? How are you showing it? Maybe it is just a display problem (you know, it is stretched instead of zoomed or something like that...
EasyHero 28-Feb-15 3:28am    
The images re compressed inside an image box of size 150x150. Irrespective of d image size. Could that be the problem?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900