By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,953 Members | 1,143 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Run AVI Files inside a Picture Box

Ali Rizwan
100+
P: 927
Hi all,
Following Code will allow you to run any AVI file in a Picture Box.
Comment and Rate it!

API Declarations

Expand|Select|Wrap|Line Numbers
  1. Const WS_CHILD = &H40000000                    
  2.  
  3. Private Declare Function mciSendString Lib "winmm.dll" Alias _                    
  4.     "mciSendStringA" (ByVal lpstrCommand As String, _                    
  5.     ByVal lpstrReturnString As String, ByVal uReturnLength As Long, _                    
  6.     ByVal hwndCallback As Long) As Long                    
  7.  
  8. Private Declare Function mciGetErrorString Lib "winmm.dll" Alias _                    
  9.     "mciGetErrorStringA" (ByVal dwError As Long, _                    
  10.     ByVal lpstrBuffer As String, ByVal uLength As Long) As Long                    
  11.  
  12. Private Declare Function GetShortPathName Lib "kernel32.dll" Alias _                    
  13.     "GetShortPathNameA" (ByVal lpszLongPath As String, _                    
  14.     ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Module CODE

Expand|Select|Wrap|Line Numbers
  1. ' FileName is a string containing the full path of the file.                    
  2. ' Window is the PictureBox in which you want that the movie                    
  3. ' is played; the movie is automatically resized to the picture box                    
  4.  
  5. Sub PlayAVIPictureBox(FileName As String, ByVal Window As PictureBox)                    
  6.     Dim RetVal As Long                    
  7.     Dim CommandString As String                    
  8.     Dim ShortFileName As String * 260                    
  9.     Dim deviceIsOpen As Boolean                    
  10.  
  11.     ' Retrieve short file name format                    
  12.     RetVal = GetShortPathName(FileName, ShortFileName, Len(ShortFileName))                    
  13.     FileName = Left$(ShortFileName, RetVal)                    
  14.  
  15.     ' Open the device                    
  16.     CommandString = "Open " & FileName & " type AVIVideo alias AVIFile parent " & _
  17.                        CStr(Window.hWnd)                    & " style " & CStr(WS_CHILD)                    
  18.     RetVal = mciSendString(CommandString, vbNullString, 0, 0&)                    
  19.     If RetVal Then GoTo error                    
  20.         ' remember that the device is now open                    
  21.         deviceIsOpen = True                    
  22.         ' Resize the movie to PictureBox size                    
  23.         CommandString = "put AVIFile window at 0 0 " & CStr _                    
  24.         (Window.ScaleWidth / Screen.TwipsPerPixelX) & " " & _                    
  25.         CStr(Window.ScaleHeight / Screen.TwipsPerPixelY)                    
  26.         RetVal = mciSendString(CommandString, vbNullString, 0, 0&)                    
  27.     If RetVal <> 0 Then GoTo error                    
  28.  
  29.     ' Play the file                    
  30.     CommandString = "Play AVIFile wait"                    
  31.     RetVal = mciSendString(CommandString, vbNullString, 0, 0&)                    
  32.     If RetVal <> 0 Then GoTo error                    
  33.  
  34.     ' Close the device                    
  35.     CommandString = "Close AVIFile"                    
  36.     RetVal = mciSendString(CommandString, vbNullString, 0, 0&)                    
  37.     If RetVal <> 0 Then GoTo error                    
  38.  
  39.     Exit Sub                    
  40.  
  41. error:                    
  42.     ' An error occurred.                    
  43.     ' Get the error description                    
  44.     Dim ErrorString As String                    
  45.     ErrorString = Space$(256)                    
  46.     mciGetErrorString RetVal, ErrorString, Len(ErrorString)                    
  47.     ErrorString = Left$(ErrorString, InStr(ErrorString, vbNullChar) - 1)                    
  48.  
  49.     ' close the device if necessary                    
  50.     If deviceIsOpen Then                    
  51.         CommandString = "Close AVIFile"                    
  52.         mciSendString CommandString, vbNullString, 0, 0&                    
  53.     End If                    
  54.  
  55.     ' raise a custom error, with the proper description                    
  56.     Err.Raise 999, , ErrorString                    
  57.  
  58. End Sub 

Using in a Form

Expand|Select|Wrap|Line Numbers
  1. 'Create a Form with a PictureBox and a Command Button
  2. Option Explicit
  3.  
  4. Private Sub Command1_Click()
  5.     PlayAVIPictureBox "c:\winnt\clock.avi", Picture1
  6. End Sub
Regards
>> ALI <<
Feb 25 '08 #1
Share this Article
Share on Google+
2 Comments


P: 18
This is amazing,very good
Mar 29 '08 #2

Plater
Expert 5K+
P: 7,872
Maybe it is because I am not using VB6, but using this code elsewhere, while technically "working" is a little queezy.
If I move the window that the picturebox is on, the video doesn't stay with it.
Like if i move the window 10 pixels to the right, the left 10 pixels on the video will be gone (as if the video was drawn on a laayer BEHIND the gui window and that layer doesn't move with the gui window)
Any thoughts?
Jul 30 '08 #3