1

I'm using C# 4.5 Framework and MySql

MySqlDataReader reader = Command.ExecuteReader();

if (reader.Read())
{

   byte[] ReturnImage = reader["Photo"] as byte[];

   MemoryStream ms = new MemoryStream(ReturnImage);

   Image Photo = Image.FromStream(ms);  //Error is in this statement!! 
}

When this stmt is executed the following error displays "Parameter is not valid"

I couldn't find the answer from the web.. Somebody Pls help..

26
  • 1
    what is reader and are you sure it is a byte array? Commented Jul 7, 2014 at 10:15
  • @Sayse if it wasn't a byte[], I would expect the first line to return a null, and therefore the second line to throw an ArgumentNullException Commented Jul 7, 2014 at 10:17
  • 4
    @Rejith Here's the thing: you can say "it is a genuine image", but: Image disagrees with you. I'm inclined to believe Image more. So: how did you store it? Commented Jul 7, 2014 at 10:21
  • 1
    @Rejith but again, how did you store it; what steps did you go through when you stored the data? Precisely what the blob contents are, and how you stored it is very important here... LongBlob is entirely appropriate and fine, but that is only half the story. A LongBlob that contains something that isn't quite the raw image doesn't help. Commented Jul 7, 2014 at 10:29
  • 1
    @Rejith that is a guid, not a jpg... Commented Jul 7, 2014 at 11:09

1 Answer 1

1

The most likely cause here is that the contents of the longblob are not the raw image bytes. Rather than go around in circles, the first thing to do is to: compare them. For example, you say (comments) that the data came from a jpg file, via OpenFileDialog. In that case, compare them. Check that you have successfully stored and retrieved the image.

Let's suppose that the file in question is c:\Some\Photo.jpg - stored etc per whatever process. In that case, you should be able to check the contents are the same. Until the following reports success, all bets are off:

byte[] original = File.ReadAllBytes(@"c:\Some\Photo.jpg");
byte[] ReturnImage = reader["Photo"] as byte[];

if(Convert.ToBase64String(original) == Convert.ToBase64String(ReturnImage)) {
    Console.WriteLine("Success; the contents match");
} else {
    Console.WriteLine("Failure; the contents are different");
}

If this reports "Failure; the contents are different", then the error is most likely in one of:

  • the code where you prepare the image to be stored (populating parameters etc)
  • the stored procedure that does the storage
  • the code that fetches the image back from the database

If this reports "Success; the contents match": then and only then is it meaningful to look at the code that attempts to load the Image. In this scenario, and assuming that c:\Some\Photo.jpg loads in most other image loading tools ("paint", etc) - then it is possible that Image doesn't recognise the subformat. But my guess is that it is going to say "Failure; the contents are different".

Note that Convert.ToBase64String here is used solely as a lazy way to check binary equivalence. You wouldn't use it like this in production code, but it is fine for this purpose.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.