How to convert BLOB to Image












1















I have a problem in displaying the picture which is stored in my MySQL database.
I don't know if I have stored it successfully but using this function which converts image to a blob file, here is the function:



private byte imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}


When I check my database, it says BLOB with blue highlight. Now, I would like to display the image in my picturebox. I have also a function to convert byte array to image..



private Image byteArrayToImage(byte byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
ms.Position = 0;
Image returnImage = Image.FromStream(ms);
return returnImage;
}


When I run the application, it says:




ArgumentException was unheld, Parameter is not valid




I have tried using this syntax:



pictureBox1.Image = byteArrayToImage(dr["img"] as byte);


Or I'm thinking if I should convert BLOB to Byte Array first? then use the function to convert the byte array into Image?

when I click the name, it should display the information, unfortunately, I'm receiving the argument exception..



int i = dataGridView1.SelectedCells[0].RowIndex;
string firstname = dataGridView1.Rows[i].Cells[0].Value.ToString();
string lastname = dataGridView1.Rows[i].Cells[1].Value.ToString();

Connection connect = new Connection();
MySqlConnection mySqlConnect = new MySqlConnection(connect.connString());
mySqlConnect.Open();

string s = "SELECT * FROM tbl_contacts WHERE username = '" + label1.Text + "' and (fname = '" + firstname + "' and lname = '" + lastname + "')";

MySqlCommand mySQL = new MySqlCommand(s, mySqlConnect);
mySQL.ExecuteNonQuery();
MySqlDataReader dr = mySQL.ExecuteReader();

if (dr.HasRows)
{
dr.Read();
txtFname.Text = dr["fname"].ToString();
txtLname.Text = dr["lname"].ToString();
txtBday.Text = dr["birthday"].ToString();
txtEmail.Text = dr["email"].ToString();
txtMobile.Text = dr["mobile"].ToString();
txtAddress.Text = dr["address"].ToString();
txtNotes.Text = dr["notes"].ToString();
pictureBox1.Image = byteArrayToImage(dr["img"] as byte);
}









share|improve this question

























  • Where is the ArguementException being thrown, in the first or second bit of code? Also, how are you retrieving the data from the database?

    – Nashibukasan
    Jan 15 '13 at 7:30






  • 2





    If you run this in the debugger, you should be given an option to inspect the ArgumentException. This should a) give you the name of the argument that's not valid, and b) a stack trace that shows shere it is being thrown. Without knowing one or both of these pieces of information, we could be guessing for days.

    – Damien_The_Unbeliever
    Jan 15 '13 at 7:52











  • Can you tell what dr["img"].GetType() returns?

    – Mehmet Ataş
    Jan 15 '13 at 7:55











  • @Mehmet Ataş it says system.byte

    – Lhynx
    Jan 15 '13 at 8:04











  • Possible duplicate of Easy way to convert a Bitmap and Png Image to text and vice versa

    – caesay
    Jan 1 at 10:07
















1















I have a problem in displaying the picture which is stored in my MySQL database.
I don't know if I have stored it successfully but using this function which converts image to a blob file, here is the function:



private byte imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}


When I check my database, it says BLOB with blue highlight. Now, I would like to display the image in my picturebox. I have also a function to convert byte array to image..



private Image byteArrayToImage(byte byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
ms.Position = 0;
Image returnImage = Image.FromStream(ms);
return returnImage;
}


When I run the application, it says:




ArgumentException was unheld, Parameter is not valid




I have tried using this syntax:



pictureBox1.Image = byteArrayToImage(dr["img"] as byte);


Or I'm thinking if I should convert BLOB to Byte Array first? then use the function to convert the byte array into Image?

when I click the name, it should display the information, unfortunately, I'm receiving the argument exception..



int i = dataGridView1.SelectedCells[0].RowIndex;
string firstname = dataGridView1.Rows[i].Cells[0].Value.ToString();
string lastname = dataGridView1.Rows[i].Cells[1].Value.ToString();

Connection connect = new Connection();
MySqlConnection mySqlConnect = new MySqlConnection(connect.connString());
mySqlConnect.Open();

string s = "SELECT * FROM tbl_contacts WHERE username = '" + label1.Text + "' and (fname = '" + firstname + "' and lname = '" + lastname + "')";

MySqlCommand mySQL = new MySqlCommand(s, mySqlConnect);
mySQL.ExecuteNonQuery();
MySqlDataReader dr = mySQL.ExecuteReader();

if (dr.HasRows)
{
dr.Read();
txtFname.Text = dr["fname"].ToString();
txtLname.Text = dr["lname"].ToString();
txtBday.Text = dr["birthday"].ToString();
txtEmail.Text = dr["email"].ToString();
txtMobile.Text = dr["mobile"].ToString();
txtAddress.Text = dr["address"].ToString();
txtNotes.Text = dr["notes"].ToString();
pictureBox1.Image = byteArrayToImage(dr["img"] as byte);
}









share|improve this question

























  • Where is the ArguementException being thrown, in the first or second bit of code? Also, how are you retrieving the data from the database?

    – Nashibukasan
    Jan 15 '13 at 7:30






  • 2





    If you run this in the debugger, you should be given an option to inspect the ArgumentException. This should a) give you the name of the argument that's not valid, and b) a stack trace that shows shere it is being thrown. Without knowing one or both of these pieces of information, we could be guessing for days.

    – Damien_The_Unbeliever
    Jan 15 '13 at 7:52











  • Can you tell what dr["img"].GetType() returns?

    – Mehmet Ataş
    Jan 15 '13 at 7:55











  • @Mehmet Ataş it says system.byte

    – Lhynx
    Jan 15 '13 at 8:04











  • Possible duplicate of Easy way to convert a Bitmap and Png Image to text and vice versa

    – caesay
    Jan 1 at 10:07














1












1








1


1






I have a problem in displaying the picture which is stored in my MySQL database.
I don't know if I have stored it successfully but using this function which converts image to a blob file, here is the function:



private byte imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}


When I check my database, it says BLOB with blue highlight. Now, I would like to display the image in my picturebox. I have also a function to convert byte array to image..



private Image byteArrayToImage(byte byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
ms.Position = 0;
Image returnImage = Image.FromStream(ms);
return returnImage;
}


When I run the application, it says:




ArgumentException was unheld, Parameter is not valid




I have tried using this syntax:



pictureBox1.Image = byteArrayToImage(dr["img"] as byte);


Or I'm thinking if I should convert BLOB to Byte Array first? then use the function to convert the byte array into Image?

when I click the name, it should display the information, unfortunately, I'm receiving the argument exception..



int i = dataGridView1.SelectedCells[0].RowIndex;
string firstname = dataGridView1.Rows[i].Cells[0].Value.ToString();
string lastname = dataGridView1.Rows[i].Cells[1].Value.ToString();

Connection connect = new Connection();
MySqlConnection mySqlConnect = new MySqlConnection(connect.connString());
mySqlConnect.Open();

string s = "SELECT * FROM tbl_contacts WHERE username = '" + label1.Text + "' and (fname = '" + firstname + "' and lname = '" + lastname + "')";

MySqlCommand mySQL = new MySqlCommand(s, mySqlConnect);
mySQL.ExecuteNonQuery();
MySqlDataReader dr = mySQL.ExecuteReader();

if (dr.HasRows)
{
dr.Read();
txtFname.Text = dr["fname"].ToString();
txtLname.Text = dr["lname"].ToString();
txtBday.Text = dr["birthday"].ToString();
txtEmail.Text = dr["email"].ToString();
txtMobile.Text = dr["mobile"].ToString();
txtAddress.Text = dr["address"].ToString();
txtNotes.Text = dr["notes"].ToString();
pictureBox1.Image = byteArrayToImage(dr["img"] as byte);
}









share|improve this question
















I have a problem in displaying the picture which is stored in my MySQL database.
I don't know if I have stored it successfully but using this function which converts image to a blob file, here is the function:



private byte imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}


When I check my database, it says BLOB with blue highlight. Now, I would like to display the image in my picturebox. I have also a function to convert byte array to image..



private Image byteArrayToImage(byte byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
ms.Position = 0;
Image returnImage = Image.FromStream(ms);
return returnImage;
}


When I run the application, it says:




ArgumentException was unheld, Parameter is not valid




I have tried using this syntax:



pictureBox1.Image = byteArrayToImage(dr["img"] as byte);


Or I'm thinking if I should convert BLOB to Byte Array first? then use the function to convert the byte array into Image?

when I click the name, it should display the information, unfortunately, I'm receiving the argument exception..



int i = dataGridView1.SelectedCells[0].RowIndex;
string firstname = dataGridView1.Rows[i].Cells[0].Value.ToString();
string lastname = dataGridView1.Rows[i].Cells[1].Value.ToString();

Connection connect = new Connection();
MySqlConnection mySqlConnect = new MySqlConnection(connect.connString());
mySqlConnect.Open();

string s = "SELECT * FROM tbl_contacts WHERE username = '" + label1.Text + "' and (fname = '" + firstname + "' and lname = '" + lastname + "')";

MySqlCommand mySQL = new MySqlCommand(s, mySqlConnect);
mySQL.ExecuteNonQuery();
MySqlDataReader dr = mySQL.ExecuteReader();

if (dr.HasRows)
{
dr.Read();
txtFname.Text = dr["fname"].ToString();
txtLname.Text = dr["lname"].ToString();
txtBday.Text = dr["birthday"].ToString();
txtEmail.Text = dr["email"].ToString();
txtMobile.Text = dr["mobile"].ToString();
txtAddress.Text = dr["address"].ToString();
txtNotes.Text = dr["notes"].ToString();
pictureBox1.Image = byteArrayToImage(dr["img"] as byte);
}






c# image bytearray blob picturebox






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 15 '13 at 8:02









Default

8,41575390




8,41575390










asked Jan 15 '13 at 7:21









LhynxLhynx

492212




492212













  • Where is the ArguementException being thrown, in the first or second bit of code? Also, how are you retrieving the data from the database?

    – Nashibukasan
    Jan 15 '13 at 7:30






  • 2





    If you run this in the debugger, you should be given an option to inspect the ArgumentException. This should a) give you the name of the argument that's not valid, and b) a stack trace that shows shere it is being thrown. Without knowing one or both of these pieces of information, we could be guessing for days.

    – Damien_The_Unbeliever
    Jan 15 '13 at 7:52











  • Can you tell what dr["img"].GetType() returns?

    – Mehmet Ataş
    Jan 15 '13 at 7:55











  • @Mehmet Ataş it says system.byte

    – Lhynx
    Jan 15 '13 at 8:04











  • Possible duplicate of Easy way to convert a Bitmap and Png Image to text and vice versa

    – caesay
    Jan 1 at 10:07



















  • Where is the ArguementException being thrown, in the first or second bit of code? Also, how are you retrieving the data from the database?

    – Nashibukasan
    Jan 15 '13 at 7:30






  • 2





    If you run this in the debugger, you should be given an option to inspect the ArgumentException. This should a) give you the name of the argument that's not valid, and b) a stack trace that shows shere it is being thrown. Without knowing one or both of these pieces of information, we could be guessing for days.

    – Damien_The_Unbeliever
    Jan 15 '13 at 7:52











  • Can you tell what dr["img"].GetType() returns?

    – Mehmet Ataş
    Jan 15 '13 at 7:55











  • @Mehmet Ataş it says system.byte

    – Lhynx
    Jan 15 '13 at 8:04











  • Possible duplicate of Easy way to convert a Bitmap and Png Image to text and vice versa

    – caesay
    Jan 1 at 10:07

















Where is the ArguementException being thrown, in the first or second bit of code? Also, how are you retrieving the data from the database?

– Nashibukasan
Jan 15 '13 at 7:30





Where is the ArguementException being thrown, in the first or second bit of code? Also, how are you retrieving the data from the database?

– Nashibukasan
Jan 15 '13 at 7:30




2




2





If you run this in the debugger, you should be given an option to inspect the ArgumentException. This should a) give you the name of the argument that's not valid, and b) a stack trace that shows shere it is being thrown. Without knowing one or both of these pieces of information, we could be guessing for days.

– Damien_The_Unbeliever
Jan 15 '13 at 7:52





If you run this in the debugger, you should be given an option to inspect the ArgumentException. This should a) give you the name of the argument that's not valid, and b) a stack trace that shows shere it is being thrown. Without knowing one or both of these pieces of information, we could be guessing for days.

– Damien_The_Unbeliever
Jan 15 '13 at 7:52













Can you tell what dr["img"].GetType() returns?

– Mehmet Ataş
Jan 15 '13 at 7:55





Can you tell what dr["img"].GetType() returns?

– Mehmet Ataş
Jan 15 '13 at 7:55













@Mehmet Ataş it says system.byte

– Lhynx
Jan 15 '13 at 8:04





@Mehmet Ataş it says system.byte

– Lhynx
Jan 15 '13 at 8:04













Possible duplicate of Easy way to convert a Bitmap and Png Image to text and vice versa

– caesay
Jan 1 at 10:07





Possible duplicate of Easy way to convert a Bitmap and Png Image to text and vice versa

– caesay
Jan 1 at 10:07












2 Answers
2






active

oldest

votes


















0














Try something like that :



byteArrayToImage(dr.GetSqlBytes(dr.GetOrdinal("img")).Buffer);





share|improve this answer































    0














    ArgumentException was unheld, Parameter is not valid is due to corrupt image. Your image did not save correctly. Save byte you have received in some file in the disk and try to rename it as .jpg or .png and try to open.



    Corruption of binary data happens due to various reasons, Length of binary data wasn't saved or trimmed. If your database is configured to store 5kb someone wrote code to trim data to 5kb instead of raising exception or appending buffer wasn't coded correctly resulting in missing last few bytes.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14332862%2fhow-to-convert-blob-to-image%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Try something like that :



      byteArrayToImage(dr.GetSqlBytes(dr.GetOrdinal("img")).Buffer);





      share|improve this answer




























        0














        Try something like that :



        byteArrayToImage(dr.GetSqlBytes(dr.GetOrdinal("img")).Buffer);





        share|improve this answer


























          0












          0








          0







          Try something like that :



          byteArrayToImage(dr.GetSqlBytes(dr.GetOrdinal("img")).Buffer);





          share|improve this answer













          Try something like that :



          byteArrayToImage(dr.GetSqlBytes(dr.GetOrdinal("img")).Buffer);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 15 '13 at 7:56









          Nicolas RepiquetNicolas Repiquet

          7,07812342




          7,07812342

























              0














              ArgumentException was unheld, Parameter is not valid is due to corrupt image. Your image did not save correctly. Save byte you have received in some file in the disk and try to rename it as .jpg or .png and try to open.



              Corruption of binary data happens due to various reasons, Length of binary data wasn't saved or trimmed. If your database is configured to store 5kb someone wrote code to trim data to 5kb instead of raising exception or appending buffer wasn't coded correctly resulting in missing last few bytes.






              share|improve this answer




























                0














                ArgumentException was unheld, Parameter is not valid is due to corrupt image. Your image did not save correctly. Save byte you have received in some file in the disk and try to rename it as .jpg or .png and try to open.



                Corruption of binary data happens due to various reasons, Length of binary data wasn't saved or trimmed. If your database is configured to store 5kb someone wrote code to trim data to 5kb instead of raising exception or appending buffer wasn't coded correctly resulting in missing last few bytes.






                share|improve this answer


























                  0












                  0








                  0







                  ArgumentException was unheld, Parameter is not valid is due to corrupt image. Your image did not save correctly. Save byte you have received in some file in the disk and try to rename it as .jpg or .png and try to open.



                  Corruption of binary data happens due to various reasons, Length of binary data wasn't saved or trimmed. If your database is configured to store 5kb someone wrote code to trim data to 5kb instead of raising exception or appending buffer wasn't coded correctly resulting in missing last few bytes.






                  share|improve this answer













                  ArgumentException was unheld, Parameter is not valid is due to corrupt image. Your image did not save correctly. Save byte you have received in some file in the disk and try to rename it as .jpg or .png and try to open.



                  Corruption of binary data happens due to various reasons, Length of binary data wasn't saved or trimmed. If your database is configured to store 5kb someone wrote code to trim data to 5kb instead of raising exception or appending buffer wasn't coded correctly resulting in missing last few bytes.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 16 '17 at 7:37









                  Akash KavaAkash Kava

                  30.8k17103144




                  30.8k17103144






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14332862%2fhow-to-convert-blob-to-image%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                      How to fix TextFormField cause rebuild widget in Flutter