Click here to Skip to main content
15,900,653 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to load an image to PictureBox from MySql db that stores in column with datatype of BLOB, and it works fine when I'm loading the image (.png) file but then when I tried to load .ico file from the DB I got an error saying "Parameter is not valid"

The error occurred at line

byte[] buffers = (byte[])readImgIco.GetValue(0);
            using (var memStream = new MemoryStream(buffers)) {
                memStream.Position = 0;
                img.Image = Image.FromStream(memStream, false);
            }


This is lines of code to insert the Icon

Icon getIcon = Icon.ExtractAssociatedIcon(open.FileName);
Image bitMapIcon = getIcon.ToBitmap();
command.Parameters["@CUST_FILE_PATH"].Value = getName;
command.Parameters["@CUST_FILE"].Value = bitMapIcon;
command.Parameters["@CUST_USERNAME"].Value = label5.Text;
command.Parameters[@"UPLOAD_DATE"].Value = varDate;
command.Parameters[@"CUST_PASSWORD"].Value = label3.Text;
if (command.ExecuteNonQuery() == 1) {
    //
}


What I have tried:

var img = ((Guna2PictureBox)panelF.Controls["ImgG" + i]);

                    String retrieveImg = "SELECT CUST_FILE FROM file_info WHERE CUST_USERNAME = @username AND CUST_PASSWORD = @password";
                    command = new MySqlCommand(retrieveImg, con);
                    command.Parameters.AddWithValue("@username", label5.Text);
                    command.Parameters.AddWithValue("@password", label3.Text);
                    MySqlDataReader readImgIco = command.ExecuteReader();

                    while(readImgIco.Read()) {
                        byte[] buffers = (byte[])readImgIco.GetValue(0);
                        using (var memStream = new MemoryStream(buffers)) {
                            memStream.Position = 0;
                            img.Image = Image.FromStream(memStream, false);
                        }
                    }
                    readImgIco.Close();
Posted
Updated 13-Oct-22 1:34am
v2

In addition to what Richard has - rightly - said, there are other problems with your code.

Start by checking your DB and how you entered the icon data to it: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^]

Then fix your whole password system ... Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

And remember: if you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.

Quote:
See my updates question where I shows a line of code I uses to send the Icon
C#
Image bitMapIcon = getIcon.ToBitmap();
...
command.Parameters["@CUST_FILE"].Value = bitMapIcon;
That won't work, and is likely your problem. What that sends to SQL is not the image data, it's the name of the bitmap class as a string: "System.Drawing.Bitmap" See the link above!
 
Share this answer
 
v2
Comments
Dan Sep2022 13-Oct-22 7:35am    
See my updated question where I shows a line of code I uses to send the Icon
OriginalGriff 13-Oct-22 7:55am    
Answer updated.
Dan Sep2022 14-Oct-22 3:00am    
Thank you! everything works now, but I got this issue where when the Icon image loaded the background become black why?
OriginalGriff 14-Oct-22 3:40am    
Without access to your code and image? I have no idea! :laugh:
Dan Sep2022 13-Oct-22 7:38am    
Thanks for providing improvement suggestion for my password storing security I'll consider about it.
The Image Class (System.Drawing) | Microsoft Learn[^] works with Bitmaps and Metafile types. Icons are quite different and require the Icon Class (System.Drawing) | Microsoft Learn[^].
 
Share this answer
 

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