0

I have byte-translated images in the database.And I have tags for these images. The user needs to search the image by entering the desired tags in the Listbox. PictureBox must be created up to the number of pictures.

Fail error message:

An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll
Additional information: Parameter is not valid.

Here are my codes;

 PictureBox[] img = new PictureBox[9999];
            for (int j = 0; j < listBox1.Items.Count; j++)
            {
                con.Open();
                MySqlCommand cmdImgCount = new MySqlCommand("select count(scan.image) from deu_scanner.scan where scan.id_Image IN (select kw_img.FK_idImg from deu_scanner.kw_img where kw_img.FK_idKeyword IN (select keyword.idkeyword from deu_scanner.keyword where keyword.keywordName='" + listBox1.Items[j] + "'));", con);
                imgCount = Convert.ToInt32(cmdImgCount.ExecuteScalar().ToString());
                con.Close();
                ArrayList ar = new ArrayList();

                for (int i = 0; i < imgCount; i++)
                {
                    con.Open();
                    MySqlCommand cmd = new MySqlCommand("select scan.image from deu_scanner.scan where scan.id_Image IN (select kw_img.FK_idImg from deu_scanner.kw_img where kw_img.FK_idKeyword IN (select keyword.idkeyword from deu_scanner.keyword where keyword.keywordName='" + listBox1.Items[j] + "'))", con);
                    MySqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        byte[] imagedata = (byte[])dr["image"];
                        MemoryStream memorystream = new MemoryStream(imagedata, 0, imagedata.Length);
                        memorystream.Write(imagedata, 0, imagedata.Length);
                        memorystream.Position = 0;
                        Image sourceImg = Image.FromStream(memorystream, true);
                        clonedImg = new Bitmap(sourceImg.Width, sourceImg.Height);
                        var copy = Graphics.FromImage(clonedImg);
                        copy.DrawImage(sourceImg, 0, 0);
                        ar.Add(clonedImg);
                    }

                    con.Close();
                }
                for (int k = 0; k < imgCount; k++)
                {
                    img[k] = new PictureBox();
                    img[k].Name = "image-" + k.ToString();
                    img[k].Image = (Image)ar[k];
                    img[k].Visible = true;
                    img[k].SizeMode = PictureBoxSizeMode.StretchImage;
                    img[k].SetBounds(12 + k * 150, 180, 120, 120);
                    this.Controls.Add(img[k]);
                    img[k].BringToFront();
                }
}

Update (from comments): The database is filled with this code

SaveFileDialog save = new SaveFileDialog(); 
save.Filter = "JPEG(.JPG)|.jpg"; 
FileStream fs = new FileStream(save.FileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs); 
byte[] data = br.ReadBytes(Convert.ToInt32(fs.Length)); 
br.Close(); 
fs.Close();
thst
  • 4,592
  • 1
  • 26
  • 40
DilaraD
  • 19
  • 4
  • 1
    where does it fail? – thst Apr 17 '17 at 07:18
  • Here; Image sourceImg = Image.FromStream(memorystream, true); @thst – DilaraD Apr 17 '17 at 07:24
  • Possible duplicate of [Save and retrieve image (binary) from SQL Server using Entity Framework 6](http://stackoverflow.com/questions/25400555/save-and-retrieve-image-binary-from-sql-server-using-entity-framework-6) – Ken Y-N Apr 17 '17 at 07:32
  • 1
    How did you create the bytestream in the database? what is the column type? – thst Apr 17 '17 at 08:44
  • İmage column type is a LONGBLOB @thst – DilaraD Apr 17 '17 at 08:52
  • Thank you, I will evaluate @KenY-N – DilaraD Apr 17 '17 at 08:52
  • I used this conversion code, but I got the same error again. public Image byteArrayToImage(byte[] byteArrayIn) { MemoryStream ms = new MemoryStream(byteArrayIn); Image returnImage = Image.FromStream(ms); return returnImage; } @KenY-N – DilaraD Apr 17 '17 at 08:58
  • How did you get the image bytes? Please post the write data code. – thst Apr 17 '17 at 09:02
  • SaveFileDialog save = new SaveFileDialog(); save.Filter = "JPEG(*.JPG)|*.jpg"; FileStream fs = new FileStream(save.FileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); byte[] data = br.ReadBytes(Convert.ToInt32(fs.Length)); br.Close(); fs.Close(); I turn it like this @thst – DilaraD Apr 17 '17 at 10:38
  • Did you find a solution? – thst Apr 21 '17 at 09:09

1 Answers1

0

The Image-object can read jpeg directly from a stream. Yet, if it throws an ArgumentException the dotnet documents say, that the image is broken in format.

Check the contents of the DB: You should test what you read from the database with an independent JPG viewer: Load the data from the database and write it to a file, then test that file to be a valid JPG. Since you are using mysql, you can use mysql workbench tools or HeidiSQL to save the blob data directly from the table, without any possibly problematic code of yours.

Having done that, make sure the JPG is readable. If not, check the writing code.

Possible problem in your DB uploader: The FileStream.length property gives the length of the stream, not the filesize. So, the contents of the db may be incomplete.

I will not repeat SO, so here is the way to properly copy the stream to a byte array for the database.

You should end with sth like this:

FileStream fs = new FileStream(save.FileName, FileMode.Open, FileAccess.Read);
MemoryStream memorystream = new MemoryStream();
fs.CopyTo(memorystream);
byte[] data = memorystream.ToArray();
// close streams

data contains now all bytes from the selected file.

I left off all security measures, like file existance etc. this is left to the OP :-)

Community
  • 1
  • 1
thst
  • 4,592
  • 1
  • 26
  • 40