Crofty

Hi, i am trying to use the code below to show a image from a file but on the line "Image image = Image.FromStream(mStream); " it gives me a parameter is invalid error and i do not know how to solve, any ideas

private Image getImage(byte[] inputImage)
{
byte[] header = new byte[] { 68, 68, 83, 32, 124, 0, 0, 0, 15, 16, 0, 0, 207, 0, 0, 0, 220, 0, 0, 0, 128, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 0, 0, 0, 0, 255, 8, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
MemoryStream mStream = new MemoryStream();
BinaryWriter bw = new BinaryWriter(mStream);
bw.BaseStream.Position = 0;
bw.Write(header);
bw.Write(inputImage);
Image image = Image.FromStream(mStream);
bw.Close();
return image;
}


Re: Visual C# General Need Help! "Paramter is invalid"

nahguam

You do know there's an Image.FromFile method don't you



Re: Visual C# General Need Help! "Paramter is invalid"

Crofty

Yes i know there is a fromfile but im extracting the image from a file so i would have to create a file everytime i would want to view it, so i thought be easier to put it into memory.




Re: Visual C# General Need Help! "Paramter is invalid"

nahguam

Ok. Is the image embedded in an rtf document or such like



Re: Visual C# General Need Help! "Paramter is invalid"

Crofty

No, its just a file that had loads of images but dosent have the header's for it.




Re: Visual C# General Need Help! "Paramter is invalid"

nahguam

I've replicated your problem by unlocking the bits of a bitmap and marshalling them into a byte[].

The object browser in VS says "The stream does not have a valid image format-or-stream is null."

Are you sure the header is correct





Re: Visual C# General Need Help! "Paramter is invalid"

Mark Dawson

Hi,

if you do not have the headers for the image then you can use the Bitmap.LockBits to populate the pixel data, however you will need to know the size and format of the image data you are trying to populate to initialize the bitmap object.

Mark.






Re: Visual C# General Need Help! "Paramter is invalid"

Crofty

hmm i know the size and image type, how do i use that Bitmap.LockBits code.




Re: Visual C# General Need Help! "Paramter is invalid"

Mark Dawson

Hi,

see this thread: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1126715&SiteID=1 I give an example of how to create an image using the LockBits method.

Mark.






Re: Visual C# General Need Help! "Paramter is invalid"


Re: Visual C# General Need Help! "Paramter is invalid"

nahguam

This should do it:

private Bitmap GetImage(byte[] inputImage, Size size, PixelFormat pixelFormat)< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

{

    Bitmap bmp = new Bitmap(size.Width, size.Height);

    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);

    BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, pixelFormat);

         

    IntPtr ptr = bmpData.Scan0;

 

    int bytes = bmp.Width * bmp.Height * 3;

 

    System.Runtime.InteropServices.Marshal.Copy(inputImage, 0, ptr, bytes);

 

    bmp.UnlockBits(bmpData);

    return bmp;

}