locked
Silverlight webcam Image Capture RRS feed

  • Question

  • Hi

    i developed a silverlight Appplication which captures webcam, i created a observable collection to add captured images, now i want to save one of the images and change it to jpeg format, how can i save the images captured from webcam please help me as its very urgent requirement.

     

    Thanku

    Sathish babu

    • Moved by Ricky_Brundritt Thursday, February 10, 2011 3:24 PM wrong category (From:Bing Maps: Map Control and Web services Development)
    Friday, June 18, 2010 4:50 PM

Answers

  • Ok, here's your morning breakfast.  I hope you mark these posts as answered if it helps you.

    To get out of Silverlight, simply pass binary to an asp handler.  I am doing an example here of a direct write to a file so if you started with a jpg, you'll finish with a jpg.  If you wanted to go from png to jpg for example, you would do something like this before you wrote the file out as I was talking about earlier:

     image.Save(ImageStream, imageFormat)

    But to get to the guts as you would like to see, here is the silverlight side of the code:

      Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    
        Dim filename As String = "exampleStaticFilename"
    
        Dim client As New WebClient
        Dim binaryDataAsStringForExample As String = "This would of course be replaced by your binary stream data from any source..."
        Dim fileContent As Byte() = System.Text.Encoding.UTF8.GetBytes(binaryDataAsStringForExample)
        Dim postUri As New Uri(Application.Current.Host.Source, "/Services/WebPost.ashx?filename=" & filename)
    
        AddHandler client.OpenWriteCompleted, AddressOf PostData_OpenWriteCompletedEventHandler
    
        client.OpenWriteAsync(postUri, Nothing, fileContent)
    
      End Sub
    
      Private Sub PostData_OpenWriteCompletedEventHandler(ByVal sender As Object, ByVal e As OpenWriteCompletedEventArgs)
    
        Dim fileContent As Byte() = TryCast(e.UserState, Byte())
        Dim outputStream As IO.Stream = e.Result
    
        outputStream.Write(fileContent, 0, fileContent.Length)
        outputStream.Close()
    
      End Sub

    and then the "/Services/WebPost.ashx" file on the ASP side of things:

    Imports System.Web
    Imports System.Web.Services
    Imports System.IO
    
    Public Class WebPost
      Implements System.Web.IHttpHandler
    
      Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    
        Dim filename As String = Nothing
    
        For Each queryString As String In context.Request.QueryString.AllKeys
    
          Select Case queryString
    
            Case "filename"
              filename = context.Request.QueryString("filename")
    
          End Select
    
        Next
    
        'exit request if no filename is found
        If filename Is Nothing Then
          Return
        End If
    
        'process the stream and write it to a file
        Using inputStream As Stream = context.Request.InputStream
    
          Dim folder As String = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Uploaded"))
    
          ' assume using "Uploaded" folder for uploaded files.
          If Not System.IO.Directory.Exists("Uploaded") Then
            System.IO.Directory.CreateDirectory(folder)
          End If
    
          Using fs As New FileStream(folder & "\" & filename, FileMode.OpenOrCreate)
    
            Dim fileContent As Byte() = New Byte(inputStream.Length - 1) {}
    
            inputStream.Read(fileContent, 0, fileContent.Length)
            fs.Write(fileContent, 0, fileContent.Length)
            fs.Flush()
    
          End Using
    
        End Using
    
      End Sub
    
      ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
          Return False
        End Get
      End Property
    
    End Class
    Enjoy!  ;)

    • Proposed as answer by Lexus9999 Monday, June 21, 2010 1:30 PM
    • Marked as answer by sathee Tuesday, June 22, 2010 11:51 AM
    Monday, June 21, 2010 1:29 PM

All replies

  • You can post the binary to the server and then the server can save the binary as a file, and using imageformats you can do something like

    image.Save(ImageStream, imageFormat)

    where the imagestream is your binary posted from silverlight and imageformat can be [imageFormat.Jpeg]

    Thats the way I've done it in the past to get out of the silverlined sandbox.

    • Proposed as answer by Lexus9999 Friday, June 18, 2010 7:35 PM
    Friday, June 18, 2010 5:34 PM
  • hi

    Thanks for the answer can u please post me the sample code as you have done this,

    Sunday, June 20, 2010 11:15 AM
  • Ok, here's your morning breakfast.  I hope you mark these posts as answered if it helps you.

    To get out of Silverlight, simply pass binary to an asp handler.  I am doing an example here of a direct write to a file so if you started with a jpg, you'll finish with a jpg.  If you wanted to go from png to jpg for example, you would do something like this before you wrote the file out as I was talking about earlier:

     image.Save(ImageStream, imageFormat)

    But to get to the guts as you would like to see, here is the silverlight side of the code:

      Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    
        Dim filename As String = "exampleStaticFilename"
    
        Dim client As New WebClient
        Dim binaryDataAsStringForExample As String = "This would of course be replaced by your binary stream data from any source..."
        Dim fileContent As Byte() = System.Text.Encoding.UTF8.GetBytes(binaryDataAsStringForExample)
        Dim postUri As New Uri(Application.Current.Host.Source, "/Services/WebPost.ashx?filename=" & filename)
    
        AddHandler client.OpenWriteCompleted, AddressOf PostData_OpenWriteCompletedEventHandler
    
        client.OpenWriteAsync(postUri, Nothing, fileContent)
    
      End Sub
    
      Private Sub PostData_OpenWriteCompletedEventHandler(ByVal sender As Object, ByVal e As OpenWriteCompletedEventArgs)
    
        Dim fileContent As Byte() = TryCast(e.UserState, Byte())
        Dim outputStream As IO.Stream = e.Result
    
        outputStream.Write(fileContent, 0, fileContent.Length)
        outputStream.Close()
    
      End Sub

    and then the "/Services/WebPost.ashx" file on the ASP side of things:

    Imports System.Web
    Imports System.Web.Services
    Imports System.IO
    
    Public Class WebPost
      Implements System.Web.IHttpHandler
    
      Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    
        Dim filename As String = Nothing
    
        For Each queryString As String In context.Request.QueryString.AllKeys
    
          Select Case queryString
    
            Case "filename"
              filename = context.Request.QueryString("filename")
    
          End Select
    
        Next
    
        'exit request if no filename is found
        If filename Is Nothing Then
          Return
        End If
    
        'process the stream and write it to a file
        Using inputStream As Stream = context.Request.InputStream
    
          Dim folder As String = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Uploaded"))
    
          ' assume using "Uploaded" folder for uploaded files.
          If Not System.IO.Directory.Exists("Uploaded") Then
            System.IO.Directory.CreateDirectory(folder)
          End If
    
          Using fs As New FileStream(folder & "\" & filename, FileMode.OpenOrCreate)
    
            Dim fileContent As Byte() = New Byte(inputStream.Length - 1) {}
    
            inputStream.Read(fileContent, 0, fileContent.Length)
            fs.Write(fileContent, 0, fileContent.Length)
            fs.Flush()
    
          End Using
    
        End Using
    
      End Sub
    
      ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
          Return False
        End Get
      End Property
    
    End Class
    Enjoy!  ;)

    • Proposed as answer by Lexus9999 Monday, June 21, 2010 1:30 PM
    • Marked as answer by sathee Tuesday, June 22, 2010 11:51 AM
    Monday, June 21, 2010 1:29 PM
  • can you explain this in c# code , i get little bit idea  but still no  overall concept. please if possible i can show you my code can you tel me what i have need to do for storing image direct into database when i click the capture button.

    i have used silverlight 4 and visual studio 2010.

    Friday, July 16, 2010 6:36 AM
  • #1. This question has been answered very thoroughly by Lexus9999 and marked as answer by the OP.

    #2. This question does not relate to Bing Maps.

    If you need specific help about storing an image from a webcam to a DB in silverlight, please post a new question in the appropriate (Silverlight) forum. If you have "no overall concept", maybe try investing in a good Silverlight book or training course.


    Beginning Spatial with SQL Server http://www.apress.com/book/view/1430218290
    Friday, July 16, 2010 7:51 AM
  • Sathish,

     

    do you have any code for capturing a webcam image or any form of image source into a sql or mysql database.

    I am open to java or xbasic

     

     

    Thanks

    W

    Thursday, February 10, 2011 2:27 PM