使用leadTools从图像c#创建电影

使用leadTools从图像c#创建电影,c#,image,video,leadtools-sdk,C#,Image,Video,Leadtools Sdk,我正在尝试从图像创建电影。 我提供以下链接: //在这里,我正在尝试提到的选项2& 执行ms.SetData(bmpSize,a)时抛出内存不足异常;陈述加上如果我直接通过var a=System.IO.File.ReadAllBytes(imagePath)传递byte[];在ms.SetData中(bmpSize,a);语句,则不会抛出错误,但视频文件未正确创建 有人能帮我吗?您的代码有几个问题: 你所有的图像都是320x240像素吗?如果没有,则在将其作为视频样本发送到Convert控件之

我正在尝试从图像创建电影。 我提供以下链接: //在这里,我正在尝试提到的选项2&

执行ms.SetData(bmpSize,a)时抛出内存不足异常;陈述加上如果我直接通过var a=System.IO.File.ReadAllBytes(imagePath)传递byte[];在ms.SetData中(bmpSize,a);语句,则不会抛出错误,但视频文件未正确创建


有人能帮我吗?

您的代码有几个问题:

  • 你所有的图像都是320x240像素吗?如果没有,则在将其作为视频样本发送到Convert控件之前,应将其调整为这些精确尺寸。 如果您想使用不同的大小,可以,但是所有图像的大小都应该相同,并且您应该相应地修改代码
  • 您正在将TargetFormat属性设置为WMVMux,但输出文件的名称具有“.avi”扩展名。如果要保存AVI文件,请设置TargetFormat=TargetFormatType.AVI
  • 如果在此之后您仍然面临问题,请随时联系support@leadtools.com并提供有关您尝试了什么以及遇到了什么错误的详细信息。对于LEADTOOLS SDK所有者和免费评估用户,电子邮件支持是免费的

    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;
            }