本文介绍了VB.NET:如何从vb.net中的视频文件捕获图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经编写了一个代码,使用网络摄像机可以从其中录制视频并将其保存到HDD,但是现在根据客户要求,他需要播放该视频并从视频中捕获静止图像并将其保存到图片框中.的,这就是我遇到问题的地方.

1)当我使用Windows Media Player播放录制的视频时,我不知道如何通过触发事件从视频中捕获图像



2)我还需要知道我是否可以在Picturebox工具上播放视频(我尝试使用winmm.lib和Mci,但它对我有帮助)

因此,我非常感谢您的帮助.,



在此先多谢了

Mansoor Khan

Hi,

I have written a code from which using a web camera one can record a video and save it onto the HDD but now according to a client requirement he needs to play that video and capture still images from the video and save it onto a picture box''s and here is where i am having issues.

1)when i use windows media player to play the recorded video i don how to capture the image from the video by triggering an event



2) also i need to know if i can play the video on picturebox tool(i tried using the winmm.lib and Mci but it din work out for me)

Hence i would really appreciate ur help.,



Thanks a lot in advance

Mansoor Khan

推荐答案

Imports System.Runtime.InteropServices
Public Class WebCam
    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
    Public Sub PreviewImage()
        Label1.Text = "LOADING"
        Application.DoEvents()
        Label1.Visible = True
        Application.DoEvents()
        hWnd = capCreateCaptureWindowA(VideoSource, WS_VISIBLE Or WS_CHILD, 0, 0, 0, _
            0, PictureBox1.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, PictureBox1.Width, PictureBox1.Height, SWP_NOMOVE Or SWP_NOZORDER)
            SendMessage(hWnd, WM_CAP_SET_SCALE, True, 0)
            Label1.Visible = False
        Else
            '--error connecting to video source---
            DestroyWindow(hWnd)
        End If
        Application.DoEvents()

    End Sub
    Public Sub StopPreviewWindow()
        SendMessage(hWnd, WM_CAP_DRIVER_DISCONNECT, VideoSource, 0)
        DestroyWindow(hWnd)
    End Sub
    Public Sub CaptureImage()
        Label1.Text = "Capturing Image"
        Application.DoEvents()
        Label1.Visible = True
        Application.DoEvents()
        Dim data As IDataObject
        Dim bmap As Image

        '---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)
            Dim imgshow As New Bitmap(bmap, PictureBox1.Width.ToString, PictureBox1.Height.ToString)
            PictureBox1.Image = imgshow
            Application.DoEvents()
            StopPreviewWindow()
            Application.DoEvents()
            Label1.Text = "Image Captured"
            Application.DoEvents()
            Label1.Visible = False
        End If
    End Sub

    Public Sub SaveImage(ByVal sImagePath As String, ByVal sImageName As String, Optional ByVal sFileType As String = "jpg")
        Label1.Text = "Saving Image"
        Application.DoEvents()
        Label1.Visible = True
        Application.DoEvents()
        If sFileType = "jpg" Then
            PictureBox1.Image.Save(sImagePath + "\" + sImageName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
        ElseIf sFileType = "bmp" Then
            PictureBox1.Image.Save(sImagePath + "\" + sImageName + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp)
        ElseIf sFileType = "gif" Then
            PictureBox1.Image.Save(sImagePath + "\" + sImageName + ".gif", System.Drawing.Imaging.ImageFormat.Gif)
        ElseIf sFileType = "png" Then
            PictureBox1.Image.Save(sImagePath + "\" + sImageName + ".png", System.Drawing.Imaging.ImageFormat.Png)
        End If
        Label1.Text = "Image Saved"
        Application.DoEvents()
        Label1.Visible = False
    End Sub

End Class




您可以按以下方式使用它




you can use it as follows

WebCam1.CaptureImage()
       WebCam1.SaveImage("C:\images", DateTime.Now.Millisecond.ToString)
       WebCam1.PreviewImage()


这篇关于VB.NET:如何从vb.net中的视频文件捕获图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 06:19