0

I am trying to create movie from images. I am following following links : https://www.leadtools.com/support/forum/posts/t11084- // Here I am trying option 2 mentioned & 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

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) & ~31) / 8;
            return BytesPerLine * (int)bmp.Height;
        }

        private int GetBitmapSize(Bitmap bmp)
        {
            int BytesPerLine = ((bmp.Width * 24 + 31) & ~31) / 8;
            return BytesPerLine * bmp.Height;
        }

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.

Can anybody please help me?

1 Answers1

0

There are a couple of problems with your code:

  1. Are all your images 320x240 pixels? If not, you should resize them to these exact dimensions before delivering them as video samples to the Convert control. If you want to use a different size, you can, but it should be the same size for all images, and you should modify the code accordingly.
  2. You are setting the TargetFormat property to WMVMux, but the name of the output file has “.avi” extension. If you want to save AVI files, set TargetFormat = TargetFormatType.AVI.

If you still face problems after this, feel free to contact support@leadtools.com and provide full details about what you tried and what errors you’re getting. Email support is free for LEADTOOLS SDK owners and also for free evaluation users.

LEADTOOLS Support
  • 2,755
  • 1
  • 12
  • 12