代码导致崩溃(vb.net)
我有一些代码,它执行时崩溃,什么似乎是错的?代码导致崩溃(vb.net)
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
一旦它开始崩溃,我不明白这一点
感谢
通常这将是因为你的引用是旧的或不适合你的操作系统。所以也许你在64位Vista上,你引用的DLL是16位?那种事。
有可能从字面上是一百个理由为什么这个代码崩溃。
首先,考虑将Form_Load事件从Form_Load事件中修改,以便从窗体上的某个按钮上执行此操作,以便将应用程序的启动与此Windows Media Encoder作业启动隔离。
然后在整个代码中设置断点,并找出它到底有多远。另外,当你说它“崩溃”,这是什么意思?如果Windows Media Converter或任何其他常规DirectX组件引发错误,则通常会有与其关联的错误编号/代码。你有错误吗?
最后,你的代码是否真的达到了Encoder.Start()?
当我打开开始调试时,它只是崩溃vshost,所以我不能提供线路对不起 – jmasterx 2009-09-01 02:17:59
只需在您的代码中添加Debug.Print语句,然后他们会给你一个清晰的想法,你会得到多远。如果你愿意为解决方案工作,总有一种方法可以确定错误 – 2009-09-01 06:58:01
至少将它隔离到导致问题的几行代码,以帮助我们帮助您。 – 2009-09-01 02:02:25
它在哪里崩溃?浏览代码并找到哪条线路和错误消息 – benPearce 2009-09-01 02:05:11
我们无法从此处看到错误消息。也许你可以告诉我们? – RBarryYoung 2009-09-01 02:10:52