title: SharpAvi Image to ByteArray to Frame to AVI
body: <p>I'm having some trouble converting an image to a video using the SharpAVI.dll.</p>
  
  <p>I have managed to produce a video file using a randomly generated byte array by using the documentation on SharpAVI's website: </p>
  
  <p><a href="https://sharpavi.codeplex.com/wikipage?title=Getting%20Started&amp;referringTitle=Documentation" rel="nofollow">Getting Started with SharpAVI</a></p>
  
  <p>So the next step I thought I would take was to take an Image, create a Bitmap image, convert the bitmap to a byte array and then simply save the byte array to each frame of the video file. When I run the program, I get no errors or anything and a video file of an appropriate file size is produced however the video file is unreadable and will not open. I'm really struggling to see why this won't work. Any help would be greatly appreciated!</p>
  
  <p>My Code:</p>
  
  <pre><code>private void GenerateSingleImageVideo()
      { 
          string imagePath = textBoxImagePath.Text;
          Bitmap thisBitmap;
  
          //generate bitmap from image file
          using (Stream BitmapStream = System.IO.File.Open(imagePath, FileMode.Open))
          {
              Image img = Image.FromStream(BitmapStream);
              thisBitmap = new Bitmap(img);
          }
  
          //convert the bitmap to a byte array
          byte[] byteArray = BitmapToByteArray(thisBitmap);
  
          //creates the writer of the file (to save the video)
          var writer = new AviWriter(textBoxFileName.Text + ".avi")
          {
              FramesPerSecond = int.Parse(textBoxFrameRate.Text),
              EmitIndex1 = true
          };
  
          var stream = writer.AddVideoStream();
          stream.Width = thisBitmap.Width;
          stream.Height = thisBitmap.Height;
          stream.Codec = KnownFourCCs.Codecs.Uncompressed;
          stream.BitsPerPixel = BitsPerPixel.Bpp32;
  
          int numberOfFrames = ((int.Parse(textBoxFrameRate.Text)) * (int.Parse(textBoxVideoLength.Text)));
          int count = 0;
  
          while (count &lt;= numberOfFrames)
          {
              stream.WriteFrame(true, byteArray, 0, byteArray.Length);
              count++;
          }
          writer.Close();
          MessageBox.Show("Done");
  
  
      }
  
      private byte[] BitmapToByteArray(Bitmap img)
      {
          ImageConverter converter = new ImageConverter();
          return (byte[])converter.ConvertTo(img, typeof(byte[]));
      }
  </code></pre>