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 control
ConvertCtrl convertctrl = _form.ConvertCtrl;
// input file and output files
string inFile = Path.Combine(LEAD_VARS.MediaDir, "MediaType_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 files
convertctrl.SourceFile = inFile;
// set the preview
convertctrl.Preview = true;
// create a new sample target
SampleTarget st = new SampleTarget();
// set the target media type for the video stream
MediaType mtTarget = new MediaType();
mtTarget.Type = Constants.MEDIATYPE_Video;
mtTarget.SubType = Constants.MEDIASUBTYPE_RGB24;
// set the sample target's accepted media type
st.SetAcceptedMediaType(mtTarget);
// assign the sample target to the capture control
convertctrl.TargetObject = st;
convertctrl.TargetFormat = TargetFormatType.StillImage;
// run the convert
convertctrl.StartConvert();
MediaSample msFrame = st.GetSample(1000);
long sampleStart, sampleStop;
while (msFrame != null)
{
// get the sample target's connected media type
MediaType mtSample = st.GetConnectedMediaType();
// demonstrate media type properties copy
MediaType mtCopy = new MediaType();
CopyMediaTypeAttributes(mtSample, mtCopy);
// get the sample times
msFrame.GetTime(out sampleStart, out sampleStop);
// create the file name for this frame bitmap
sampleFileName = string.Format("{0}_{1}-{2}.bmp", outFilePfx, sampleStart, sampleStop);
// create the bitmap for this frame
WriteSampleBitmap(sampleFileName, msFrame, mtSample); // mtCopy);
try
{
// create a media sample using the captured sample from above
msFrame = st.GetSample(1000);
}
catch (COMException cex)
{
// if we have reached the end of stream we are finished
if (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;
else
throw 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 null
if (pSource == null || pDest == null)
return false;
// get the format data size
fmtDataSize = pSource.FormatSize;
// any format data
if (fmtDataSize > 0)
pDest.SetFormatData(fmtDataSize,
pSource.GetFormatData(fmtDataSize)); // yes, then copy it
else
pDest.FormatSize = 0; // no, just set the dest size to zero
// copy type, subtype, formattype
pDest.Type = pSource.Type;
pDest.SubType = pSource.SubType;
pDest.FormatType = pSource.FormatType;
// set dest fixed size samples and temporal compression
pDest.FixedSizeSamples = pSource.FixedSizeSamples;
pDest.TemporalCompression = pSource.TemporalCompression;
// copy samplesize
pDest.SampleSize = pSource.SampleSize;
return true;
}
private Bitmap WriteSampleBitmap(string outFile, MediaSample ms, MediaType mt)
{
// get the video information
VideoInfoHeader vih = mt.GetVideoFormatData();
// create a bitmap to hold the sample and copy it
Bitmap 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 buffer
bmp.RotateFlip(RotateFlipType.Rotate180FlipX);
// save the image
bmp.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:\LEADTOOLS22\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