0

大家好。

我正在开发一个安装在运行 Windows Mobile 6.5 的设备上的软件。一方面是正确使用设备的相机。在我的情况下,我想使用 CameraCaptureDialog 如下:

Dim cameraCaptureDialog As New CameraCaptureDialog()
cameraCaptureDialog.Owner = Me
cameraCaptureDialog.Mode = CameraCaptureMode.Still
If cameraCaptureDialog.ShowDialog() = DialogResult.OK AndAlso cameraCaptureDialog.FileName.Length > 0 Then
    Dim sFileNameExt As String = ""
    Dim sFileDir As String = ""
    Dim sPicsDir As String = ""
    Dim sFileSource As String = ""
    sFileSource = Path.GetFullPath(cameraCaptureDialog.FileName)
    sFileDir = Path.GetDirectoryName(cameraCaptureDialog.FileName)
    sFileDir = Path.GetPathRoot(cameraCaptureDialog.FileName)
    sFileNameExt = Path.GetExtension(cameraCaptureDialog.FileName)
    Dim fs As New FileStream(sFileSource, FileMode.OpenOrCreate, FileAccess.Read)
    Dim ImgArtikel(CType(fs.Length, Int32)) As Byte
    fs.Read(ImgArtikel, 0, CType(fs.Length, Int32))
    fs.Close()
    functions.ConnectLocalDB(functions.localconn)
    Dim cmd As SqlCeCommand = functions.localconn.CreateCommand()
    cmd.CommandText = "UPDATE table SET img_Photo=@imgArt "  'assign the newly made image to db-entry
    cmd.Parameters.Add("@imgArt", SqlDbType.Image)
    cmd.Parameters("@imgArt").Value = ImgArtikel
    cmd.ExecuteNonQuery()
    cmd.Dispose()
    If File.Exists(sFileSource) Then File.Delete(sFileSource) 'delete photo after updating db

End If
cameraCaptureDialog.Dispose()

这第一次工作正常(在模拟器和设备上),但是当我启动相同的事件时,我的软件崩溃了。调试时我没有遇到异常,它只是在 ShowDialog() (第四行)崩溃。

有谁知道这里有什么问题?

4

1 回答 1

0

您需要设置一些知道此例程正忙的全局变量。

private cameraWorking As Boolean

免责声明:请注意,我不经常使用 VB,所以我的一些语法可能有点偏离,但这应该给你的想法。)

我注意到你从不使用sPicsDir你分配sFileDir两次。

我也是这个关键字的忠实粉丝Using,它应该为你处理关闭连接和处理对象。

考虑到这一点,我重新编写了您的例程并注释掉了从未使用过或不必要的项目(与Using例程一起)。

If Not cameraWorking Then
  Try
    cameraWorking = True
    Using cameraCaptureDialog As New CameraCaptureDialog()
      cameraCaptureDialog.Owner = Me
      cameraCaptureDialog.Mode = CameraCaptureMode.Still
      If cameraCaptureDialog.ShowDialog() = DialogResult.OK Then
        Dim filename As String = cameraCaptureDialog.FileName
        If Not String.IsNullOrEmpty(filename) Then
          Dim sFileSource As String = Path.GetFullPath(cameraCaptureDialog.FileName)
          'Dim sFileNameExt As String = Path.GetExtension(cameraCaptureDialog.FileName)
          'Dim sFileDir As String = Path.GetDirectoryName(cameraCaptureDialog.FileName)
          'Dim sRootDir As String = Path.GetPathRoot(cameraCaptureDialog.FileName)
          functions.ConnectLocalDB(functions.localconn)
          Using fs As New FileStream(sFileSource, FileMode.OpenOrCreate, FileAccess.Read)
            Dim ImgArtikel(CType(fs.Length, Int32)) As Byte
            fs.Read(ImgArtikel, 0, CType(fs.Length, Int32))
            Using cmd As SqlCeCommand = functions.localconn.CreateCommand()
              cmd.CommandText = "UPDATE table SET img_Photo=@imgArt "  'assign the newly made image to db-entry
              cmd.Parameters.Add("@imgArt", SqlDbType.Image)
              cmd.Parameters("@imgArt").Value = ImgArtikel
              cmd.ExecuteNonQuery()
              'cmd.Dispose()
            End Using
            ' Note that you may still need to explicitly close the stream here
            ' because the GC will likely still have it open in the call below to delete
            ' the actual file.
            fs.Close()
          End Using
          If File.Exists(sFileSource) Then
            File.Delete(sFileSource) 'delete photo after updating db
          End If
        End If
      End If
      'cameraCaptureDialog.Dispose()
    End Using
  Catch err As Exception
    MessageBox.Show(Me, err.Message, "Camera Capture Error")
  Finally
    cameraWorking = False
  End Try

如果这一切都是由于表单上的按钮单击而发生,那么设置会更直观

My.cameraCaptureButton.Enabled = False

在例程开始时,然后记住重新启用Finally块中的按钮。

于 2013-01-30T14:59:03.837 回答