4

My code currently looks like this:

if (fe == "CR2")
{
    Image img = null;
    byte[] ba = File.ReadAllBytes(open.FileName);
    using (Image raw = Image.FromStream(new MemoryStream(ba)))
    {
        img = raw;
    }
    Bitmap bm = new Bitmap(img);
    pictureBox1.Image = bm;
    statusl.Text = fe;
}

When I open a RAW image the program stops and Visual Studio says:

Parameter is not valid: Image raw = Image.FromStream(new MemoryStream(ba))

Please help! How can I get a RAW file to show in a PictureBox ?

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62

3 Answers3

6

Create the bitmap like this:

Bitmap bmp = (Bitmap) Image.FromFile(open.FileName);

or without using bitmap:

 this.pictureBox1.Image = Image.FromFile(open.FileName);

Example WPF:

BitmapDecoder bmpDec = BitmapDecoder.Create(new Uri(origFile),
BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
BitmapEncoder bmpEnc = new BmpBitmapEncoder();
bmpEnc.Frames.Add(bmpDec.Frames[0]);
Stream ms = new MemoryStream();
bmpEnc.Save(ms);
Image srcImage = Bitmap.FromStream(ms);
Martijn van Put
  • 3,293
  • 18
  • 17
  • I got another exception "Out of memory." What did i do wrong?`if (fe == "CR2") { Bitmap bmp = (Bitmap)Image.FromFile(open.FileName); pictureBox1.Image = bmp; statusl.Text = fe; }` –  Jul 30 '13 at 20:54
  • What size is the image? – Geeky Guy Jul 30 '13 at 20:54
  • OutOfMemory can be caused by: The file does not have a valid image format. -- GDI+ does not support the pixel format of the file. – Martijn van Put Jul 30 '13 at 20:54
  • I tried with .arw (Sony raw image) and .cr2 (Cannon raw image) and they are photos made with the camera. –  Jul 30 '13 at 20:58
  • Image doesn't understand .cr2 or arw files. Convert them to .JPG or PNG / BMP etc. With WPF it is possible with BitmapDecoder and requires codec with WIC support to be installed – Martijn van Put Jul 30 '13 at 21:00
  • What is WPF? How do i convert raw images to .bmp in c#? –  Jul 30 '13 at 21:09
  • Normally Windows Forms is used to represent UI but WPF another way to create an UI app. It is commonly used to build more advanced UI apps and advanced drawing. http://www.wpftutorial.net/WPFIntroduction.html – Martijn van Put Jul 30 '13 at 21:11
  • So how do WPF help me? –  Jul 30 '13 at 21:16
  • Yes, it may resolve your problem but only when you have the Raw codes. See my anwser for code for a sample which may work. – Martijn van Put Jul 30 '13 at 21:18
  • I see but where do i get the raw code and what do i do with them? "BitmapDecoder" could not be found... –  Jul 30 '13 at 21:28
  • BitmapDecoder does only exists in WPF, so you must create a new WPF project. http://www.fastpictureviewer.com/codecs/ – Martijn van Put Jul 30 '13 at 21:31
  • Ok i will try it out but how do i use the raw codes after i have them? –  Jul 30 '13 at 21:35
  • I cannot help you further, i don't have experience with WIC codes and using BitmapDecorder. – Martijn van Put Jul 30 '13 at 21:37
3

You're actually disposing an Image by specifying using (Image raw = Image.FromStream(new MemoryStream(ba))) later assigning the Disposed instance of image to picturebox which leads to this exception. To make to work you've to either don't dispose or clone the image.

Bitmap raw = Image.FromStream(new MemoryStream(ba) as Bitmap;
pictureBox1.Image = raw;

Or simply Clone

using (Image raw = Image.FromStream(new MemoryStream(ba)))
{
    img = raw.Clone() as Bitmap;
}

Both of the above should work

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
1

you try this code :

private static void SaveImageToRawFile(string strDeviceName, Byte[] Image, int nImageSize)
    {
        string strFileName = strDeviceName;
        strFileName += ".raw";

        FileStream vFileStream = new FileStream(strFileName, FileMode.Create);
        BinaryWriter vBinaryWriter = new BinaryWriter(vFileStream);
        for (int vIndex = 0; vIndex < nImageSize; vIndex++)
        {
            vBinaryWriter.Write((byte)Image[vIndex]);
        }
        vBinaryWriter.Close();
        vFileStream.Close();
    }

    private static void LoadRawFile(string strDeviceName, out Byte[] Buffer)
    {
        FileStream vFileStream = new FileStream(strDeviceName, FileMode.Open);
        BinaryReader vBinaryReader = new BinaryReader(vFileStream);

        Buffer = new Byte[vFileStream.Length];

        Buffer = vBinaryReader.ReadBytes(Convert.ToInt32(vFileStream.Length));

        vBinaryReader.Close();
        vFileStream.Close();
    }
MHAnbar
  • 11
  • 1