Image.FromStream

Alright. I'm trying to load a Bitmap (or a TIFF File, but one step at a time here people) into a MemoryFile to be used and quickly discarded (the program can interact faster with it that way, correct?)
So then!!
here is my current progress, Amazingly enough it compiles and runs (somewhat).
/********************
CreateMMFile takes a BinaryReader object defined in the "HandleFileType" Method.
********************/
      public static void HandleFileType(string Filename)
      {
            Image ocrImage = Image.FromFile(Filename);
            byte[] bitmap32 = new byte[3]
                  {66,208,3};
            byte[] tif14 = new byte[3]
                  {73,0,8};
            byte[] imgBytes = new byte[3]
                  {0,0,0};

            if(Filename.Length != 0)
            {
                  using (BinaryReader brFile = new BinaryReader(
                                 new FileStream(Filename,FileMode.Open,FileAccess.Read,
                                 FileShare.Read)))
                  {

                        imgBytes[0] = (byte)brFile.BaseStream.ReadByte();
                        imgBytes[1] = (byte)brFile.BaseStream.ReadByte();
                        imgBytes[2] = (byte)brFile.BaseStream.ReadByte();

                        if(imgBytes[0] == bitmap32[0])
                        {
                              CreateMMFile(brFile);
                        }
                        if(imgBytes[0] == tif14[0])
                        {
// Image is a TIFF File
                        }
                  }
            }
      }
      public static void CreateMMFile(BinaryReader bmpFile)
      {
            byte[] buffer = new byte[bmpFile.BaseStream.Length];
            byte[] imgBinary = new byte[bmpFile.BaseStream.Length];
            IntPtr MMFile = IntPtr.Zero;

            MemoryStream msStream = new MemoryStream(buffer,0,buffer.Length,true,true);

            for(int i = 0;i < bmpFile.BaseStream.Length - 1; i++)
            {
                  imgBinary[i] = (byte)bmpFile.BaseStream.ReadByte();
            }
            using (BinaryWriter bwFile = new BinaryWriter(msStream))
            {
                  bwFile.Write(imgBinary);
            }
      }

My Question is .. Is this doing what I think it is doing, and how can I get the Data from Memory and load it as an Image Object?
LVL 2
derekthorntonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_TAD_Commented:


take a look at this code...


                  Stream inputFile = new FileStream(@"C:\temp\myPic.jpg",FileMode.Open);
            byte[] buff = new byte[inputFile.Length];
                  inputFile.Read(buff,0,buff.Length);

                  Stream memStream = new MemoryStream(buff);

                  Image im = Image.FromStream(memStream);
0
_TAD_Commented:


In a nutshell....

Open a new stream (of type filestream)
read all of the contents of the file into a byte array
Open a new Stream (of type MemoryStream) using the array of bytes from the file stream
Create an image from a stream
0
derekthorntonAuthor Commented:
Doesn't work. I get an Invalid Parameter Exception.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

_TAD_Commented:


You did copy and paste the code directly, right?

I have a form with a picture box and a button on it (that's it), and this code works perfectly:


            private void button1_Click(object sender, System.EventArgs e)
            {
                  Stream inputFile = new FileStream(@"C:\temp\frog.jpg",FileMode.Open);
            byte[] buff = new byte[inputFile.Length];
                  inputFile.Read(buff,0,buff.Length);

                  Stream memStream = new MemoryStream(buff);

                  Image im = Image.FromStream(memStream);

                  pictureBox1.Image = im;
            }
0
derekthorntonAuthor Commented:
That can't work. It can't have a Designated path name. It has to take the filename from another process that examined the image to begin with.
0
_TAD_Commented:


In fact.... just so that there is no confusion, send back a thumbnail image (resized to the size of the textbox);



            private void button1_Click(object sender, System.EventArgs e)
            {
                  Stream inputFile = new FileStream(@"C:\temp\frog.jpg",FileMode.Open);
            byte[] buff = new byte[inputFile.Length];
                  inputFile.Read(buff,0,buff.Length);

                  Stream memStream = new MemoryStream(buff);

                  Image im = Image.FromStream(memStream);

                  pictureBox1.Image = im.GetThumbnailImage(pictureBox1.Width,pictureBox1.Height,null,IntPtr.Zero);
            }

0
derekthorntonAuthor Commented:
It keeps telling me the file is being used by another process, how can I free up the File from the previous BinaryReader stream?
0
_TAD_Commented:

okay.... replace the string with the variable name with the fully qualified path name (no @ symbol)
          private string MyFilePath = @"C:\temp\frog.jpg";
          private string MyFilePath = "C:\\temp\\frog.jpg";   // same thing as above, no @ symbol



          private void button1_Click(object sender, System.EventArgs e)
          {
               Stream inputFile = new FileStream(MyFilePath, FileMode.Open);
            byte[] buff = new byte[inputFile.Length];
               inputFile.Read(buff,0,buff.Length);

               Stream memStream = new MemoryStream(buff);

               Image im = Image.FromStream(memStream);

               pictureBox1.Image = im.GetThumbnailImage(pictureBox1.Width,pictureBox1.Height,null,IntPtr.Zero);
          }

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
_TAD_Commented:


You have to close the binaryreader object.
0
derekthorntonAuthor Commented:
But the file is still in use by Binary Reader. I have to have the file open to do what I need to do, but I have to close it to have it be read by the FileStream.
0
derekthorntonAuthor Commented:
I DO close the BinaryReader object and it does nothing. It STILL tells me it's open. Do I have to close a specific part of the binaryReader?
0
_TAD_Commented:


your other option is to set the file share on your file stream.   In my example it would look like:

Stream inputFile = new FileStream(@"C:\temp\frog.jpg",FileMode.Open, FileAccess.Read, FileShare.Read);

0
derekthorntonAuthor Commented:
     This might make things clearer.

public static void HandleFileType(string Filename,TOCRJobInfo mJobInfo,
            ListBox lst,PictureViewer.PictureViewer Viewer)
      {

            Image ocrImage = Image.FromFile(Filename);
            byte[] bitmap32 = new byte[3]
                  {66,208,3};
            byte[] tif14 = new byte[3]
                  {73,0,8};
            byte[] imgBytes = new byte[3]
                  {0,0,0};

            if(Filename.Length != 0)
            {
                  using (BinaryReader brFile = new BinaryReader(
                                 new FileStream(Filename,FileMode.Open,FileAccess.ReadWrite,
                                 FileShare.ReadWrite)))
                  {

                        imgBytes[0] = (byte)brFile.BaseStream.ReadByte();
                        imgBytes[1] = (byte)brFile.BaseStream.ReadByte();
                        imgBytes[2] = (byte)brFile.BaseStream.ReadByte();

                        if(imgBytes[0] == bitmap32[0])
                        {
                              CreateMMFile(brFile,lst,Viewer,Filename);
                              mJobInfo.InputFile = Filename;
                              mJobInfo.JobType = OCRuser.TOCRJOBTYPE_DIBFILE;
                              mJobInfo.PageNo = ocrImage.GetFrameCount(FrameDimension.Page);
                        }
                        if(imgBytes[0] == tif14[0])
                        {
                              
                              mJobInfo.JobType = OCRuser.TOCRJOBTYPE_TIFFFILE;
                              mJobInfo.PageNo = ocrImage.GetFrameCount(FrameDimension.Page);
                        }
                  }
            }
      }

      public class DoWriteNums
      {
            private byte[] buffer;
            private ListBox lst;

            public DoWriteNums(ListBox lst,byte[] buffer)
            {
                  this.lst = lst;
                  this.buffer = buffer;
            }
            public void Execute()
            {
                  for(int i=0;i<buffer.Length-1;i++)
                  {
                        lst.Items.Add(buffer[i].ToString());
                  }
            }
      }
      public static void CreateMMFile(BinaryReader bmpFile,ListBox lst,
            PictureViewer.PictureViewer Viewer,string Filename)
      {
            string FileNameTemporary = Filename.Substring(Filename.Length-4);
            string FileNameTemporaryPath = Filename.Substring(0,Filename.Length-4);
            
            string FileNameNew = FileNameTemporaryPath + "temp" + FileNameTemporary;

            bmpFile.BaseStream.Close();
      
            Stream inputFile = new FileStream(Filename,FileMode.Open);
            byte[] buffer = new byte[inputFile.Length];
            inputFile.Read(buffer,0,buffer.Length);

            Stream memStream = new MemoryStream(buffer);
            Image im = Image.FromStream(memStream);
            Viewer.LoadImage(im);

            Thread myThread = new
                  Thread(new ThreadStart(new DoWriteNums(lst,buffer).Execute));
            myThread.Start();

            MemoryStream msStream = new MemoryStream(buffer,0,buffer.Length,true,true);
      }
0
derekthorntonAuthor Commented:
What if I closed brFile before my If Statements?
0
_TAD_Commented:


I think your issue starts with this line of code:

 using (BinaryReader brFile = new BinaryReader(
                            new FileStream(Filename,FileMode.Open,FileAccess.ReadWrite,
                            FileShare.ReadWrite)))  
{

//stuff here

}



It appears as if you've got your file share property set properly on your filestream, but it is embeded in the binaryWriter object.


Just for giggles, I would take out the "using" statement and just dispose of your objects the long way for right now.  It allows for a little more control and it may be easier to see what is going wrong.


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.