-1

I try converting image data from the database which is already in byte[] back to the image and I'm getting "invalid parameter error" using Image.FileStream. Please, can anyone help me out with this?

I've tried working around the code using various methods and the last one is below in my code.

byte[] data = validaccount.FingerPrint;

try
{
    using (MemoryStream strm = new MemoryStream())
    {
        strm.Write(data, 0, data.Length);
        strm.Position = 0;
        System.Drawing.Image img = System.Drawing.Image.FromStream(strm);
        BitmapImage bi = new BitmapImage();

        bi.BeginInit();

        MemoryStream ms = new MemoryStream();
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

        ms.Seek(0, SeekOrigin.Begin);
        bi.StreamSource = ms;
        bi.EndInit();
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

The code is supposed to convert the byte[] into an image.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

2

According to the docs, Image.FromStream(stream) will throw an argument exception if "The stream does not have a valid image format". Have you verified the data is actually correct? If so, what type of image is it?

You're misusing your streams. You only need 1 memory stream and it has a constructor that takes a byte array (no need to write the bytes yourself). Be sure to wrap it in a using block (like you did for your first stream).

You may not want to use BitmapImage - that's for xaml/wpf apps. You probably want System.Drawing.Bitmap which inherits/extends System.Drawing.Image. Additionally, Bitmap has a constructor which takes a stream - no need to use FromStream.

Finally, Image (and hence Bitmap since Bitmap inherits Image) implements IDisposable, so you should also wrap it in a using block.

P.S. This is a duplicate question.

Jeff Shepler
  • 2,007
  • 2
  • 22
  • 22
  • The data is being read from a database, the database contains all byte from a saved template stored in a folder using .fpt. – Quadry Mustaqeem Aug 06 '19 at 03:02
  • I am currently working on a WPF application actually. – Quadry Mustaqeem Aug 06 '19 at 03:02
  • How do you mean I am misusing the MemoryStream? – Quadry Mustaqeem Aug 06 '19 at 03:03
  • If .fpt is referring to a foxpro database, then the bytes from the file is more than just the image (and hence not readable via FromStream) and you would have to write code to parse out the bytes that are just the image itself. If .fpt is some kind of proprietary image format, it would not be a supported (standard) image format and you would need to find/write code to manually convert it to a bitmap. – Jeff Shepler Aug 06 '19 at 11:23
  • By "misusing your streams", I was referring to a few things: 1) the second stream isn't part of the using block and so it will not get disposed properly, 2) you could have reused the first stream, and 3) is along the same lines as a comment above going from byte[] -> bitmap -> byte[] i.e. why not use the first stream as bi.StreamSource instead of going through the image class. – Jeff Shepler Aug 06 '19 at 11:32
  • Also "misuse" doesn't mean "doesn't work". Technically what you wrote would probably work if the data was a valid/supported image type, but it wouldn't be considered the "proper" way. Sorry if I offended you, that was not my intent. – Jeff Shepler Aug 06 '19 at 11:50
  • This [SO answer](https://stackoverflow.com/a/24315437/374837) might or might not be helpful. This would let you bypass the built-in image parsing code that's throwing your exception and will give you some kind of bitmap to view, though it probably won't be what you want. – Jeff Shepler Aug 06 '19 at 12:16
0

Though its not memory stream, this method has worked for me and if you browse SO for your question, some times MS does not work.

using System.Drawing;

var converterdImage = (Bitmap)((new ImageConverter()).ConvertFrom(byteArray));

Byte Array to Image Conversion

Gabriel Llorico
  • 1,783
  • 1
  • 13
  • 23