title: Create movie from images c# using leadTools
body: <p>I am trying to create movie from images.
  I am following following links :
  <a href="https://www.leadtools.com/support/forum/posts/t11084-" rel="nofollow noreferrer">https://www.leadtools.com/support/forum/posts/t11084-</a>  // Here I am trying option 2 mentioned &amp; <a href="https://social.msdn.microsoft.com/Forums/sqlserver/en-US/b61726a4-4b87-49c7-b4fc-8949cd1366ac/visual-c-visual-studio-2017-how-do-you-convert-jpg-images-to-video-in-visual-c?forum=csharpgeneral" rel="nofollow noreferrer">https://social.msdn.microsoft.com/Forums/sqlserver/en-US/b61726a4-4b87-49c7-b4fc-8949cd1366ac/visual-c-visual-studio-2017-how-do-you-convert-jpg-images-to-video-in-visual-c?forum=csharpgeneral</a></p>
  
  <pre><code>void convert()
   {
                  bmp = new Bitmap(320, 240, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                  // create sample source object
                  SampleSource smpsrc = new SampleSource();
                  ConvertCtrl convertCtrl = new ConvertCtrl();
  
                  // create a new media type wrapper
                  MediaType mt = new MediaType();
  
                  double AvgTimePerFrame = (10000000 / 15);
  
                  // set the type to 24-bit RGB video
                  mt.Type = Constants.MEDIATYPE_Video;
                  mt.SubType = Constants.MEDIASUBTYPE_RGB24;
  
                  // set the format
                  mt.FormatType = Constants.FORMAT_VideoInfo;
  
                  VideoInfoHeader vih = new VideoInfoHeader();
                  int bmpSize = GetBitmapSize(bmp);
  
                  // setup the video info header
                  vih.bmiHeader.biCompression = 0; // BI_RGB
                  vih.bmiHeader.biBitCount = 24;
                  vih.bmiHeader.biWidth = bmp.Width;
                  vih.bmiHeader.biHeight = bmp.Height;
                  vih.bmiHeader.biPlanes = 1;
                  vih.bmiHeader.biSizeImage = bmpSize;
                  vih.bmiHeader.biClrImportant = 0;
                  vih.AvgTimePerFrame.lowpart = (int)AvgTimePerFrame;
                  vih.dwBitRate = bmpSize * 8 * 15;
  
                  mt.SetVideoFormatData(vih, null, 0);
  
                  // set fixed size samples matching the bitmap size
                  mt.SampleSize = bmpSize;
                  mt.FixedSizeSamples = true;
  
                  // assign the source media type
                  smpsrc.SetMediaType(mt);
  
                  // select the LEAD compressor
                  convertCtrl.VideoCompressors.MCmpMJpeg.Selected = true;
  
  
                  convertCtrl.SourceObject = smpsrc;
  
                  convertCtrl.TargetFile = @"D:\Projects\LEADTool_Movie_fromImage\ImageToVideo_LeadTool\ImageToVideo_LeadTool\Images\Out\aa.avi";
                  //convertCtrl.TargetFile = "C:\\Users\\vipul.langalia\\Documents\\count.avi";
                  convertCtrl.TargetFormat = TargetFormatType.WMVMux;
                  convertCtrl.StartConvert();
  
                   BitmapData bmpData;
              int i = 1;
              byte[] a = new byte[bmpSize];
              System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);
              var imgs = GetAllFiles();
              foreach (var item in imgs)
              {
                  bmpSize = GetBitmapSize(item);
  
                  MediaSample ms = smpsrc.GetSampleBuffer(30000);
                  ms.SyncPoint = true;
  
  
                  bmpData = item.LockBits(rect, ImageLockMode.ReadWrite, item.PixelFormat);
                  Marshal.Copy(bmpData.Scan0, a, 0, bmpSize);
                  item.UnlockBits(bmpData);
  
                  ms.SetData(bmpSize, a);
  
                    SetSampleTime(ms, i, AvgTimePerFrame);
                  smpsrc.DeliverSample(1000, ms);
  
                  i++;
  
              }
  
                   smpsrc.DeliverEndOfStream(1000);
                   }
  
                   byte[] GetByteArrayFroMWritableBitmap(WriteableBitmap bitmapSource)
          {
              var width = bitmapSource.PixelWidth;
              var height = bitmapSource.PixelHeight;
              var stride = width * ((bitmapSource.Format.BitsPerPixel + 7) / 8);
  
              var bitmapData = new byte[height * stride];
  
              bitmapSource.CopyPixels(bitmapData, stride, 0);
              return bitmapData;
          }
  
          private int GetBitmapSize(WriteableBitmap bmp)
          {
              int BytesPerLine = (((int)bmp.Width * 24 + 31) &amp; ~31) / 8;
              return BytesPerLine * (int)bmp.Height;
          }
  
          private int GetBitmapSize(Bitmap bmp)
          {
              int BytesPerLine = ((bmp.Width * 24 + 31) &amp; ~31) / 8;
              return BytesPerLine * bmp.Height;
          }
  </code></pre>
  
  <p>It is throwing out of memory exception when execute  ms.SetData(bmpSize, a); statement. Plus If I directly pass byte[] by  var a = System.IO.File.ReadAllBytes(imagePath); in  ms.SetData(bmpSize, a); statement then it will not throw error but video file is not properly created.</p>
  
  <p>Can anybody please help me?</p>