none
bitmap source to image to gif file in vb.net

    Question

  • Im trying to convert to image type from bitmap source type and here is my code ive been trying to do but I havent been able to get this to work:

     Dim source As BitmapSource = BitmapSource.Create(640, 480, 96, 96,
        PixelFormats.Gray16, Nothing, PixelData, 640 * 2)
                Dim test As System.Drawing.Bitmap
                Dim outstream As MemoryStream = New MemoryStream
                Dim enc As BitmapEncoder = New GifBitmapEncoder
                
    
                enc.Frames.Add(BitmapFrame.Create(depthImage.Source))
                enc.Save(outstream)
                test = New System.Drawing.Bitmap(outstream)
                test.Save("c:\myfile.gif")

    I just want to save to file with the above code.

    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda

    Friday, February 24, 2012 9:08 PM

All replies

  • Hi The Thinker,

    I think we don't need to involve System.Drawing.Bitmap to save a bitmap. You can try the following code to accomplish this.

                Dim outstream As FileStream = New FileStream(@"c:\myfile.gif", FileMode.Create)
                Dim enc As BitmapEncoder = New GifBitmapEncoder
    
                enc.Frames.Add(BitmapFrame.Create(depthImage.Source))
                enc.Save(outstream)


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, February 27, 2012 5:31 AM
  •   Private Sub Sensor_DepthFrameReady(sender As Object, e As DepthImageFrameReadyEventArgs)
    
            Dim ReceivedData As Boolean = False
    
            Using DFrame As DepthImageFrame = e.OpenDepthImageFrame()
                If DFrame Is Nothing Then
                    ' The image processing took too long. More than 2 frames behind.
                Else
                    ReDim PixelData(DFrame.PixelDataLength - 1)
                    DFrame.CopyPixelDataTo(PixelData)
                    ReceivedData = True
    
                End If
    
            End Using
           
    
            If ReceivedData Then
                Dim source As BitmapSource = BitmapSource.Create(640, 480, 96, 96,
        PixelFormats.Gray16, Nothing, PixelData, 640 * 2)
                depthImage.Source = source
                Dim enc As BitmapEncoder = New GifBitmapEncoder
                Dim outstream As FileStream = New FileStream("c:\myfile.gif", FileMode.Create)
                enc.Frames.Add(BitmapFrame.Create(depthImage.Source))
                enc.Save(outstream)
                outstream.Close()
    
    
            End If
    
    
        End Sub

    The code is in vb.net but its about the same as c# so even if its in c# I can make minor adjustments to make it work in vb.net. But I posted my full sub for the above but Im taking it from the kinect depth image source but as you can see i just initialize the kinect then capture from the depth source.

    The other one or two lines with pixel are just preping the depth source and in if received data then if statement code if i just replace everything with this one line it shows the image in an image control:

    depthImage.Source = source

    DFrame variable has an framenumber property but other then that I think I need to limit the frames.

    It should be too much different to save this to file though.


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda


    • Edited by The Thinker Monday, February 27, 2012 3:04 PM
    Monday, February 27, 2012 2:59 PM
  • Hi The Thinker,

    I didn't see any question in your reply. Do you have any problems with this code?


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, February 29, 2012 6:53 AM
  • It wont save the file I think I did something wrong or I need to open the stream one frame at a time because it wont save. But OpenNextFrame seems like the method to use and it has a milliseconds pause before it gives the next frame.

    In my next post I will give the exact error message so you can help me but even though you gave the code in c# it would be very similar in vb.net


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda


    • Edited by The Thinker Wednesday, February 29, 2012 1:07 PM
    Wednesday, February 29, 2012 1:03 PM