这是完整的vb.net源代码。
Imports System.Runtime.InteropServices
Imports System.IO
Public Class WebCam
'WEb camera constants'
Const WM_CAP_START = &H400S
Const WS_CHILD = &H40000000
Const WS_VISIBLE = &H10000000
Const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10
Const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11
Const WM_CAP_EDIT_COPY = WM_CAP_START + 30
Const WM_CAP_SEQUENCE = WM_CAP_START + 62
Const WM_CAP_FILE_SAVEAS = WM_CAP_START + 23
Const WM_CAP_SET_SCALE = WM_CAP_START + 53
Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52
Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50
Const SWP_NOMOVE = &H2S
Const SWP_NOSIZE = 1
Const SWP_NOZORDER = &H4S
Const HWND_BOTTOM = 1
'--The capGetDriverDescription function retrieves the version description of the capture driver--'
Declare Function capGetDriverDescriptionA Lib "avicap32.dll" _
(ByVal wDriverIndex As Short, _
ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, _
ByVal cbVer As Integer) As Boolean
'--The capCreateCaptureWindow function creates a capture window--'
Declare Function capCreateCaptureWindowA Lib "avicap32.dll" _
(ByVal lpszWindowName As String, ByVal dwStyle As Integer, _
ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, _
ByVal nHeight As Short, ByVal hWnd As Integer, _
ByVal nID As Integer) As Integer
'--This function sends the specified message to a window or windows--'
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, _
<MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer
'--Sets the position of the window relative to the screen buffer--'
Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" _
(ByVal hwnd As Integer, _
ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, _
ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
'--This function destroys the specified window--'
Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
Dim VideoSource As Integer
Dim hWnd As Integer
'__________________________________________________ End of web camera'
'web cam subs'
'--disconnect from video source---'
Private Sub StopPreviewWindow()
SendMessage(hWnd, WM_CAP_DRIVER_DISCONNECT, VideoSource, 0)
DestroyWindow(hWnd)
End Sub
'---list all the various video sources---'
Private Sub ListVideoSources()
lstVideoSources.Items.Clear()
Dim DriverName As String = Space(80)
Dim DriverVersion As String = Space(80)
For i As Integer = 0 To 9
If capGetDriverDescriptionA(i, DriverName, 80, DriverVersion, 80) Then
lstVideoSources.Items.Add(DriverName.Trim)
End If
Next
End Sub
'---save the image---'
Private Sub CaptureImage()
Dim data As IDataObject
Dim bmap As Image
PictureBoxCaptured.Image = bmap
'---copy the image to the clipboard---'
SendMessage(hWnd, WM_CAP_EDIT_COPY, 0, 0)
'---retrieve the image from clipboard and convert it '
' to the bitmap format'
data = Clipboard.GetDataObject()
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
bmap = _
CType(data.GetData(GetType(System.Drawing.Bitmap)), _
Image)
PictureBoxCaptured.Image = bmap
'StopPreviewWindow()'
End If
End Sub
'---preview the selected video source---'
Private Sub PreviewVideo(ByVal pbCtrl As PictureBox)
hWnd = capCreateCaptureWindowA(VideoSource, WS_VISIBLE Or WS_CHILD, 0, 0, 0, _
0, pbCtrl.Handle.ToInt32, 0)
If SendMessage(hWnd, WM_CAP_DRIVER_CONNECT, VideoSource, 0) Then
'---set the preview scale---'
SendMessage(hWnd, WM_CAP_SET_SCALE, True, 0)
'---set the preview rate (ms)---'
SendMessage(hWnd, WM_CAP_SET_PREVIEWRATE, 30, 0)
'---start previewing the image---'
SendMessage(hWnd, WM_CAP_SET_PREVIEW, True, 0)
'---resize window to fit in PictureBox control---'
SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, _
pbCtrl.Width, pbCtrl.Height, _
SWP_NOMOVE Or SWP_NOZORDER)
Else
'--error connecting to video source---'
DestroyWindow(hWnd)
End If
End Sub
Private Sub ButtonWebCamCapture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonWebCamCapture.Click
CaptureImage()
End Sub
Private Sub ButtonWebCamView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonWebCamView.Click
'---stop video in case it is on---'
StopPreviewWindow()
'---check which video source is selected---'
VideoSource = lstVideoSources.SelectedIndex
'---preview the selected video source'
PreviewVideo(PictureBoxLive)
End Sub
Private Sub lstVideoSources_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstVideoSources.SelectedIndexChanged
'---stop video in case it is on---'
StopPreviewWindow()
'---check which video source is selected---'
VideoSource = lstVideoSources.SelectedIndex
'---preview the selected video source'
PreviewVideo(PictureBoxLive)
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
Try
StopPreviewWindow()
PictureBoxLive.Image = Nothing
PictureBoxCaptured.Image = Nothing
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub FormWebCam_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListVideoSources()
If (lstVideoSources.Items.Count > 0) Then
lstVideoSources.SelectedIndex = 0
'---stop video in case it is on---'
StopPreviewWindow()
'---check which video source is selected---'
VideoSource = lstVideoSources.SelectedIndex
'---preview the selected video source'
PreviewVideo(PictureBoxLive)
End If
'PictureBoxCaptured.Image.Dispose()'
End Sub
Private Sub ButtonSaveAndExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSaveAndExit.Click
If (PictureBoxCaptured.Image Is Nothing) Then
If (MessageBox.Show("Image is empty." & vbCrLf & "Do you want to exit ?", "Empty", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes) Then
StopPreviewWindow()
Me.Close()
End If
Else
If (ModuleFunctions.IsFormOpen(Item)) Then
Item.PictureBoxItem.Image = PictureBoxCaptured.Image
ElseIf (ModuleFunctions.IsFormOpen(Customer)) Then
Customer.PictureBoxCaptured.Image = PictureBoxCaptured.Image
ElseIf (ModuleFunctions.IsFormOpen(BillCustomises)) Then
BillCustomises.PictureBoxItem.Image = PictureBoxCaptured.Image
End If
StopPreviewWindow()
Me.Close()
End If
End Sub
Private Sub FormWebCam_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Me.Dispose()
End Sub
End Class
上面的代码与我的旧vb.net项目一起工作正常。 现在我想将该项目转换为c#。我无法仅转换以下功能。
我想将以下vb.net代码转换为c#
'--This function sends the specified message to a window or windows--'
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, _
<MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer
我尝试使用不同的转换器,但却出错了
“转换错误:代码无法转换。详细信息:
- 第1行第9栏:无效的NonModuleDeclaration
请检查原始代码中是否有任何错误,然后重试。 “
我试着转换它,但我最终得到了它。
//--This function sends the specified message to a window or windows--
[DllImport("user32.dll")]
public static extern Boolean SendMessage(int hwnd, int Msg, int wParam, Object lParam);
但这不正确。请帮我解决这个问题。
答案 0 :(得分:2)
您的C#版本应该是这样的:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg,
IntPtr wParam, IntPtr lParam);
答案 1 :(得分:0)
最后,我找到了完整列表的答案。 谢谢大家。
点击here查看完整代码
[DllImport("avicap32.dll")]
protected static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string
lpszWindowName,int dwStyle, int x, int y, int nWidth, int nHeight, int hWndParent, int nID);