LEADTOOLS Multimedia (Leadtools.Multimedia assembly)

FormatType Property

Show in webframe
Example 



Gets or sets the format buffer type.
Syntax
'Declaration
 
Public Property FormatType As String
'Usage
 
Dim instance As MediaType
Dim value As String
 
instance.FormatType = value
 
value = instance.FormatType
public string FormatType {get; set;}
public:
property String^ FormatType {
   String^ get();
   void set (    String^ value);
}

Property Value

A string containing the GUID for the format buffer type.
Remarks
The returned string contains a formatted Globally Unique Identifier (GUID) of the form {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}. For more information on the available GUIDs, refer to the DirectShow documentation. Assignment can raise an error exception. For more information, refer to the Error Codes.
Example
Copy Code  
Imports Leadtools
Imports Leadtools.Multimedia
Imports LeadtoolsMultimediaExamples.Fixtures

Public _result As Boolean = False
Public _form As ConvertCtrlForm = New ConvertCtrlForm()
Public Sub GetFormatDataExample()
   ' reference the convert control
   Dim convertctrl As ConvertCtrl = _form.ConvertCtrl

   ' input file and output files
   Dim inFile As String = Path.Combine(LEAD_VARS.MediaDir, "MediaType_ShortSource.avi")
   Dim outFilePfx As String = Path.Combine(LEAD_VARS.MediaDir, "MediaType_GetFormatDataExample")
   Dim sampleFileName As String

   ' 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
      Dim st As SampleTarget = New SampleTarget()

      ' set the target media type for the video stream
      Dim mtTarget As MediaType = New MediaType()
      mtTarget.Type = Leadtools.Multimedia.Constants.MEDIATYPE_Video
      mtTarget.SubType = Leadtools.Multimedia.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()

      Dim msFrame As MediaSample = st.GetSample(1000)
      Dim sampleStart, sampleStop As Long

      Do While Not msFrame Is Nothing
         ' get the sample target's connected media type
         Dim mtSample As MediaType = st.GetConnectedMediaType()

         ' demonstrate media type properties copy
         Dim mtCopy As MediaType = New MediaType()
         CopyMediaTypeAttributes(mtSample, mtCopy)

         ' get the sample times
         msFrame.GetTime(sampleStart, 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 cex As COMException
            ' if we have reached the end of stream we are finished
                   If cex.ErrorCode = CInt(ErrorCode.VFW_E_SAMPLE_REJECTED_EOS) _
                      OrElse cex.ErrorCode = CInt(ErrorCode.VFW_E_WRONG_STATE) Then
                       Exit Do
                   ElseIf cex.ErrorCode = CInt(ErrorCode.VFW_E_TIMEOUT) Then
                       Continue Do
                   Else
                       Throw cex
                   End If
         End Try

         ' 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 Then
            Application.DoEvents()
         End If
      Loop
   Catch e1 As Exception
      _result = False
   End Try
End Sub

Private Function CopyMediaTypeAttributes(ByVal pSource As MediaType, ByVal pDest As MediaType) As Boolean
   Dim fmtDataSize As Integer

   ' return error if either source or dest is null
   If pSource Is Nothing OrElse pDest Is Nothing Then
      Return False
   End If

   ' get the format data size
   fmtDataSize = pSource.FormatSize

   ' any format data
   If fmtDataSize > 0 Then
      pDest.SetFormatData(fmtDataSize, pSource.GetFormatData(fmtDataSize)) ' yes, then copy it
   Else
      pDest.FormatSize = 0 ' no, just set the dest size to zero
   End If

   ' 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
End Function

Private Function WriteSampleBitmap(ByVal outFile As String, ByVal ms As MediaSample, ByVal mt As MediaType) As Bitmap
   ' get the video information 
   Dim vih As VideoInfoHeader = mt.GetVideoFormatData()

   ' create a bitmap to hold the sample and copy it
   Dim bmp As Bitmap = New Bitmap(vih.bmiHeader.biWidth, vih.bmiHeader.biHeight, FormatFromBitCount(vih.bmiHeader.biBitCount))
   Dim bmpData As BitmapData = 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
End Function

Private Function FormatFromBitCount(ByVal bitCount As Integer) As PixelFormat
   Select Case 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
   End Select
   Throw New Exception("Unrecognized bit count")
End Function

Private Function GetBitmapSize(ByVal bmp As Bitmap, ByVal bitCount As Integer) As Integer
   Dim pixelSize As Integer = CInt(Math.Log(CDbl(bitCount)))
   Return (bmp.Width * pixelSize + pixelSize And (Not 3)) * bmp.Height
End Function

Private Function GetBitmapScanRowSize(ByVal bmpSize As Integer, ByVal stride As Integer, ByVal width As Integer) As Integer
   Return CInt(bmpSize / (stride / width))
End Function

Public NotInheritable Class LEAD_VARS
Public Const MediaDir As String = "C:\Program Files (x86)\LEAD Technologies\LEADTOOLS 18\Media"
End Class
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:\Program Files (x86)\LEAD Technologies\LEADTOOLS 18\Media";
}
Requirements

Target Platforms

See Also

Reference

MediaType Class
MediaType Members

 

 


Products | Support | Contact Us | Copyright Notices
© 2006-2014 All Rights Reserved. LEAD Technologies, Inc.

Leadtools.Multimedia requires a Multimedia or Multimedia Suite license and unlock key. For more information, refer to: LEADTOOLS Toolkit Features