Private Sub FileToWmp(ByVal inFile As String,ByVal outFile As String)
Dim bdFile As BitmapDecoder = Nothing ' Container for bitmap frames
Dim readFile As FileStream = File.OpenRead(inFile) ' Read the source file into a FileStream object
' Set the BitmapDecoder object from the source file
bdFile = BitmapDecoder.Create(readFile, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None)
Dim writeFile As FileStream = File.OpenWrite(outFile) ' Prepare the output file
Dim wbeFile As New WmpBitmapEncoder() ' All the magic done by WmpBitmapEncoder
wbeFile.ImageQualityLevel = 0.9F ' Set the quality level to... pretty good
wbeFile.Frames.Add(bdFile.Frames(0)) ' Add the bitmap frame to the encoder object
wbeFile.Save(writeFile) ' Write the output file
writeFile.Close()
readFile.Close()
End Sub
Private Sub FileToWmp(ByVal outFile As String, ByVal img As Image, ByVal quality As Long)
Dim bit As Bitmap = New Bitmap(img, img.Width, img.Height)
Dim bitmap As IntPtr = bit.GetHbitmap
Dim palette As IntPtr
Dim sourceRect As Windows.Int32Rect = New Windows.Int32Rect(0, 0, img.Width, img.Height)
Dim sizeOptions As BitmapSizeOptions = BitmapSizeOptions.FromEmptyOptions
Dim image As BitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( _
bitmap, palette, sourceRect, sizeOptions)
Dim stream As New FileStream(outFile, FileMode.Create)
Dim encoder As New WmpBitmapEncoder() ' All the magic done by WmpBitmapEncoder
encoder.ImageQualityLevel = quality / 100 ' Set the quality level to a percentage
encoder.Frames.Add(BitmapFrame.Create(image)) ' Add the bitmap frame to the encoder object
encoder.Save(stream)
stream.Close()
End Sub