Webcambild aus Picturebox speichern

  • VB.NET

Es gibt 5 Antworten in diesem Thema. Der letzte Beitrag () ist von ErfinderDesRades.

    Webcambild aus Picturebox speichern

    Hallo,

    ich stehe vor einem Problem.

    Ich habe eine Picturebox und die zeigt mir mein Webcambild an.

    Warum kann den Pictureboxinhalt (sprich das Webcambild) nicht einfach mit

    VB.NET-Quellcode

    1. PictureBox1.Image.Save(SaveFileDialog1.FileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)


    speichern????



    Gibt es vllt. jemand der es weiß und Ahnung hat, wie man es relaisieren könne? 8o
    Das geht garnicht so einfach. Denn das Bild wird direkt auf die PictureBox gezeichnet. Deine Anwendung bekommt vom Bild garnichts mit.

    Du könntest aber folgendes probieren:
    Erstelle eine neue Bitmap (Using Bpm As New Bitmap(Breite, Höhe)) mit der gewünschten Höhe und Breite (wird vermutlich die Größe der PictureBox sein).
    Probier dann, den Aufruf, den Du mit dem Handle der PictureBox machst nochmal zu machen. Aber diesmal gibst Du nicht das Handle der PictureBox an, sondern Bpm.GetHbitmap().
    Wenn das klappt, wird das Bild auf die Bitmap gezeichnet. Dann rufst Du die Save-Methode auf, wie Du das im Startpost wolltest.
    Kannst Du das Zeichnen auch unterbrechen? Denn das würde ich direkt vor dem Aufruf der Save-Methode machen. Wenn Du pech hast, wird genau in dem Moment auf die Bitmap gezeichnet, in dem Du sie speicherst. Das gibt höchstwahrscheinlich eine Exception.
    "Luckily luh... luckily it wasn't poi-"
    -- Brady in Wonderland, 23. Februar 2015, 1:56
    Desktop Pinner | ApplicationSettings | OnUtils
    Hi, hab ich mal aus diesem Forum, also sollte so sein,
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Class Form1
    2. Const WM_CAP As Short = &H400S
    3. Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
    4. Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
    5. Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
    6. Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
    7. Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
    8. Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
    9. Const WS_CHILD As Integer = &H40000000
    10. Const WS_VISIBLE As Integer = &H10000000
    11. Const SWP_NOMOVE As Short = &H2S
    12. Const SWP_NOSIZE As Short = 1
    13. Const SWP_NOZORDER As Short = &H4S
    14. Const HWND_BOTTOM As Short = 1
    15. Dim iDevice As Integer = 0
    16. Dim hHwnd As Integer
    17. Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Object) As Integer
    18. 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
    19. Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
    20. 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 hWndParent As Integer, ByVal nID As Integer) As Integer
    21. Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, ByVal cbVer As Integer) As Boolean
    22. Private Sub LoadDeviceList()
    23. Dim strName As String = Space(100)
    24. Dim strVer As String = Space(100)
    25. Dim bReturn As Boolean
    26. Dim x As Integer = 0
    27. Do
    28. bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
    29. If bReturn Then lstDevices.Items.Add(strName.Trim)
    30. x += 1
    31. Loop Until bReturn = False
    32. End Sub
    33. Private Sub OpenPreviewWindow()
    34. Dim iHeight As Integer = picCapture.Height
    35. Dim iWidth As Integer = picCapture.Width
    36. hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, 480, picCapture.Handle.ToInt32, 0)
    37. If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
    38. SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
    39. SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
    40. SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
    41. SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, SWP_NOMOVE Or SWP_NOZORDER)
    42. btnSave.Enabled = True
    43. btnStop.Enabled = True
    44. btnStart.Enabled = False
    45. Else
    46. DestroyWindow(hHwnd)
    47. btnSave.Enabled = False
    48. End If
    49. End Sub
    50. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    51. LoadDeviceList()
    52. End Sub
    53. Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
    54. Dim data As IDataObject
    55. Dim bmap As Image
    56. SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
    57. data = Clipboard.GetDataObject()
    58. If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
    59. bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
    60. PicCapture.Image = bmap
    61. ClosePreviewWindow()
    62. BtnSave.Enabled = False
    63. BtnStop.Enabled = False
    64. btnStart.Enabled = True
    65. If SfdImage.ShowDialog = DialogResult.OK Then
    66. bmap.Save(SfdImage.FileName, Imaging.ImageFormat.Bmp)
    67. End If
    68. End If
    69. End Sub
    70. Private Sub ClosePreviewWindow()
    71. SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)
    72. DestroyWindow(hHwnd)
    73. End Sub
    74. Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    75. OpenPreviewWindow()
    76. btnStart.Enabled = False
    77. BtnStop.Enabled = True
    78. End Sub
    79. Private Sub BtnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStop.Click
    80. ClosePreviewWindow()
    81. btnStart.Enabled = True
    82. BtnStop.Enabled = False
    83. End Sub
    84. Private Sub PicCapture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PicCapture.Click
    85. AutoScroll = True
    86. End Sub
    87. End Class


    Du benötigst eine PictureBox, genannt PicCapture,
    3 Buttons,
    und ggf. eine ListBox.

    und ein SafeFileDialog.

    Mfg.eniking1998