DataSize
Size of the format data, in bytes. The format buffer will be set to this size.
Data
Variant containing an array reference. The array must be composed of a single dimension of bytes. This array contains the actual format data to set.
If the method fails, an error is raised. For more information, refer to the Error Codes.
using Leadtools;using Leadtools.Multimedia;using LeadtoolsMultimediaExamples.Fixtures;public bool _result = false;public ConvertCtrlForm _form = new ConvertCtrlForm();public void GetFormatDataExample(){// reference the convert controlConvertCtrl convertctrl = _form.ConvertCtrl;// input file and output filesstring inFile = Path.Combine(LEAD_VARS.MediaDir, "ConvertCtrl_ShortSource.avi");string outFilePfx = Path.Combine(LEAD_VARS.MediaDir, "MediaType_GetFormatDataExample");string sampleFileName;// This example demonstrates how to use SampleTarget and MediaSample// objects to directly access sample frames from a conversion graph.try{// set the source and target filesconvertctrl.SourceFile = inFile;// set the previewconvertctrl.Preview = true;// create a new sample targetSampleTarget st = new SampleTarget();// set the target media type for the video streamMediaType mtTarget = new MediaType();mtTarget.Type = Constants.MEDIATYPE_Video;mtTarget.SubType = Constants.MEDIASUBTYPE_RGB24;// set the sample target's accepted media typest.SetAcceptedMediaType(mtTarget);// assign the sample target to the capture controlconvertctrl.TargetObject = st;convertctrl.TargetFormat = TargetFormatType.StillImage;// run the convertconvertctrl.StartConvert();MediaSample msFrame = st.GetSample(1000);long sampleStart, sampleStop;while (msFrame != null){// get the sample target's connected media typeMediaType mtSample = st.GetConnectedMediaType();// demonstrate media type properties copyMediaType mtCopy = new MediaType();CopyMediaTypeAttributes(mtSample, mtCopy);// get the sample timesmsFrame.GetTime(out sampleStart, out sampleStop);// create the file name for this frame bitmapsampleFileName = string.Format("{0}_{1}-{2}.bmp", outFilePfx, sampleStart, sampleStop);// create the bitmap for this frameWriteSampleBitmap(sampleFileName, msFrame, mtSample); // mtCopy);try{// create a media sample using the captured sample from abovemsFrame = st.GetSample(1000);}catch (COMException cex){// if we have reached the end of stream we are finishedif (cex.ErrorCode == (int)ErrorCode.VFW_E_SAMPLE_REJECTED_EOS|| cex.ErrorCode == (int)ErrorCode.VFW_E_WRONG_STATE)break;else if (cex.ErrorCode == (int)ErrorCode.VFW_E_TIMEOUT)continue;elsethrow cex;}// set the result_result = true;// we'll loop on the state and pump messages for this example.// but you should not need to if running from a Windows Forms application.if (convertctrl.State == ConvertState.Running)Application.DoEvents();}}catch (Exception){_result = false;}}private bool CopyMediaTypeAttributes(MediaType pSource, MediaType pDest){int fmtDataSize;// return error if either source or dest is nullif (pSource == null || pDest == null)return false;// get the format data sizefmtDataSize = pSource.FormatSize;// any format dataif (fmtDataSize > 0)pDest.SetFormatData(fmtDataSize,pSource.GetFormatData(fmtDataSize)); // yes, then copy itelsepDest.FormatSize = 0; // no, just set the dest size to zero// copy type, subtype, formattypepDest.Type = pSource.Type;pDest.SubType = pSource.SubType;pDest.FormatType = pSource.FormatType;// set dest fixed size samples and temporal compressionpDest.FixedSizeSamples = pSource.FixedSizeSamples;pDest.TemporalCompression = pSource.TemporalCompression;// copy samplesizepDest.SampleSize = pSource.SampleSize;return true;}private Bitmap WriteSampleBitmap(string outFile, MediaSample ms, MediaType mt){// get the video informationVideoInfoHeader vih = mt.GetVideoFormatData();// create a bitmap to hold the sample and copy itBitmap bmp = new Bitmap(vih.bmiHeader.biWidth, vih.bmiHeader.biHeight, FormatFromBitCount(vih.bmiHeader.biBitCount));BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);Marshal.Copy(ms.Buffer, 0, bmpData.Scan0, GetBitmapSize(bmp, vih.bmiHeader.biBitCount));bmp.UnlockBits(bmpData);// flip the upside down bufferbmp.RotateFlip(RotateFlipType.Rotate180FlipX);// save the imagebmp.Save(outFile, ImageFormat.Bmp);return bmp;}private PixelFormat FormatFromBitCount(int bitCount){switch (bitCount){case 8:return PixelFormat.Format8bppIndexed;case 16:return PixelFormat.Format16bppRgb555;case 32:return PixelFormat.Format32bppRgb;case 48:return PixelFormat.Format48bppRgb;case 24:return PixelFormat.Format24bppRgb;}throw new Exception("Unrecognized bit count");}private int GetBitmapSize(Bitmap bmp, int bitCount){int pixelSize = (int)Math.Log((double)bitCount);return (bmp.Width * pixelSize + pixelSize & ~3) * bmp.Height;}private int GetBitmapScanRowSize(int bmpSize, int stride, int width){return bmpSize / (stride / width);}static class LEAD_VARS{public const string MediaDir = @"C:\LEADTOOLS23\Media";}
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
