views:

250

answers:

3

Hello everyone,

I am using Windows Vista x64 + VSTS 2008. I am debugging the sample program from the following URL (associated sample code for this article),

http://www.codeproject.com/KB/audio-video/CaptureScreenAsVideo.aspx?display=PrintAll&fid=129831&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=101&select=1160633

But I met with null pointer exception when press the write to file button, here is my screen snapshot.

http://tinypic.com/view.php?pic=14cbfop&s=5

Any ideas what is wrong?

thanks in advance, George

EDIT1: here is the whole source code.

Imports WMEncoderLib
Imports WMPREVIEWLib

Imports System.IO
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(48, 80)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(216, 23)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Write To file and Close application"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim Encoder As WMEncoder
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Create a WMEncoder object.

        Encoder = New WMEncoder
        ' Retrieve the source group collection and add a source group. 

        Dim SrcGrp As IWMEncSourceGroup2
        Dim SrcGrpColl As IWMEncSourceGroupCollection
        SrcGrpColl = Encoder.SourceGroupCollection
        SrcGrp = SrcGrpColl.Add("SG_1")

        ' Add a video and audio source to the source group.
        Dim SrcVid As IWMEncVideoSource2
        Dim SrcAud As IWMEncAudioSource
        SrcVid = SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO)
        SrcAud = SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO)

        ' Identify the source files to encode.
        SrcVid.SetInput("ScreenCap://ScreenCapture1")
        SrcAud.SetInput("Device://Default_Audio_Device")

        ' Choose a profile from the collection.
        Dim ProColl As IWMEncProfileCollection
        Dim Pro As IWMEncProfile
        Dim i As Integer
        Dim lLength As Long

        ProColl = Encoder.ProfileCollection
        lLength = ProColl.Count
        'For i = 0 To lLength - 1
        '    Console.WriteLine(ProColl.Item(i).Name)
        'Next
        For i = 0 To lLength - 1
            Pro = ProColl.Item(i)
            If Pro.Name = "Windows Media Video 8 for Local Area Network (384 Kbps)" Then
                SrcGrp.Profile = Pro
                Exit For
            End If
        Next

        ' Fill in the description object members.
        Dim Descr As IWMEncDisplayInfo
        Descr = Encoder.DisplayInfo
        Descr.Author = "Armoghan Asif"
        Descr.Copyright = "Copyright information"
        Descr.Description = "Text description of encoded content"
        Descr.Rating = "Rating information"
        Descr.Title = "Title of encoded content"

        ' Add an attribute to the collection.
        Dim Attr As IWMEncAttributes
        Attr = Encoder.Attributes
        Attr.Add("URL", "www.adnare.com")

        ' Specify a file object in which to save encoded content.
        Dim File As IWMEncFile
        File = Encoder.File
        File.LocalFileName = "C:\OutputFile.avi"

        ' Crop 2 pixels from each edge of the video image.
        SrcVid.CroppingBottomMargin = 2
        SrcVid.CroppingTopMargin = 2
        SrcVid.CroppingLeftMargin = 2
        SrcVid.CroppingRightMargin = 2

        ' Start the encoding process.
        Encoder.Start()

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Encoder.RunState Then
            Encoder.Stop()
            Application.Exit()
        End If
    End Sub
End Class
+1  A: 

You probably don't have the encoder. Check the prerequisites (See this line: "You will be needing Windows Media Encoder 9 Series and SDK.").

Stefan Steinegger
I have installed Encoder but still met with this issue. You can try to install it and reproduce my issue?
George2
+2  A: 

Did you install Windows Media Encoder 9 Series and SDK as instructed by the article? My guess is that this is the culprit.

Ayman Hourieh
I have installed both of them, but still met with this issue. BTW: you can try to install it and reproduce my issue.
George2
I'm sorry; I don't use Windows or know VB, so I cannot test.
Ayman Hourieh
The code could be easily converted to C#, just tens of line of code. But the same issue. :-(
George2
+1  A: 

Is this code still intact?

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' Create a WMEncoder object.
    Encoder = New WMEncoder

The implication is that that code is missing or quietly failing.

If you put a breakpoint on the line following Encoder = New WMEncoder, what's the value of Encoder?

RichieHindle
@RichieHindle, I am confused about what you mean. Could you write down the correct code in your mind please? Code makes everything clear. :-)
George2
That code comes directly from the sample code in the CodeProject download you pointed to. I'm wondering whether you've changed the sample code such that the Encoder variable is no longer being initialised.
RichieHindle
@RichieHindle, I am using the original code, not changing anything. You can try to run this program (original code from codeproject article) and reproduce my issue.
George2
I have just posted all the code I am using. Let me know if you found any issues dealing with initialization. :-)
George2