Can this code be changed to winforms VB instead of WPF? The parts I need changed are the Run and CreateImage subs and a Import or two.
' xxx = areas that need to be changed.
This class was originally written in C# WPF VS2008 Pro by Julien Mancini for
his excellent version of a Logon Background Changer.
This class is used to get a *.bmp or *.png or *.jpg file as close to 256kb
as possible so the Windows 7 LogonUI can use it.
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.IO
Imports System.Windows.Media.Imaging
Imports System.Windows.Media
Imports System.Windows
Imports System.ComponentModel
Imports System.Threading
Class ImagesCreation
Public Shared imgResolutions As Integer(,) = New Integer(,) {{1280, 960}, {1024, 768}, {1600, 1200}, {1440, 900}, {1920, 1200}, {1280, 768}, _
{1360, 768}, {1024, 1280}, {960, 1280}, {900, 1440}, {768, 1280}, {768, 1360}}
Private imgPath As String
Public Sub New(imgPath As String)
Me.imgPath = imgPath
End Sub
Public Sub run(sender As Object, args As DoWorkEventArgs)
Thread.Sleep(200)
Dim bgWorker As BackgroundWorker = TryCast(sender, BackgroundWorker)
Try
Dim bmpSrc As New CachedBitmap(New BitmapImage(New Uri(imgPath)), BitmapCreateOptions.None, BitmapCacheOption.OnLoad)
For i As Integer = 0 To imgResolutions.GetLength(0) - 1
Dim w As Integer = imgResolutions(i, 0)
Dim h As Integer = imgResolutions(i, 1)
createImage(bmpSrc, w, h, "background" & w & "x" & h & ".jpg")
bgWorker.ReportProgress(i + 1)
Next
Dim screenWidth As Integer = CInt(Math.Truncate(SystemParameters.PrimaryScreenWidth))
Dim screenHeight As Integer = CInt(Math.Truncate(SystemParameters.PrimaryScreenHeight))
createImage(bmpSrc, screenWidth, screenHeight, "backgroundDefault.jpg")
bgWorker.ReportProgress(100)
Catch ex As Exception
bgWorker.ReportProgress(100, ex)
End Try
End Sub
Private Sub createImage(bmpSrc As BitmapSource, w As Integer, h As Integer, filename As String)
Dim srcW As Integer = bmpSrc.PixelWidth
Dim srcH As Integer = bmpSrc.PixelHeight
Dim ratioSrc As Double = CDbl(srcW) / srcH
Dim ratioDest As Double = CDbl(w) / h
Dim newW As Integer
Dim newH As Integer
If ratioDest < ratioSrc Then
newH = srcH
newW = CInt(Math.Truncate(srcW / ratioSrc * ratioDest))
Else
newW = srcW
newH = CInt(Math.Truncate(srcH * ratioSrc / ratioDest))
End If
Dim bmp As New CroppedBitmap(bmpSrc, New Int32Rect((srcW - newW) \ 2, (srcH - newH) \ 2, newW, newH))
Dim drawingVisual As New DrawingVisual()
Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()
drawingContext.DrawImage(bmp, New Rect(0, 0, w, h))
drawingContext.Close()
Dim tg As New RenderTargetBitmap(w, h, 96, 96, PixelFormats.Pbgra32)
tg.Render(drawingVisual)
Dim found As Boolean = False
Dim ms As MemoryStream = Nothing
Dim quality As Integer = 94
Dim [step] As Char = "d"C
Dim delta As Integer = -8
Dim topInterval As Integer = 101
While Not found
If ms IsNot Nothing Then
ms.Close()
End If
ms = New MemoryStream()
Dim encoder As New JpegBitmapEncoder()
quality += delta
encoder.QualityLevel = quality
encoder.Frames.Add(BitmapFrame.Create(tg))
encoder.Save(ms)
Dim pos As Long = ms.Position
If pos < 256 * 1000 Then
If quality = 100 Then
found = True
ElseIf [step] = "d"C Then
delta = Math.Max(1, Math.Abs(delta \ 2))
[step] = "i"C
ElseIf [step] = "i"C Then
delta = Math.Max(1, Math.Abs(delta \ 2))
If delta = 1 AndAlso topInterval = quality + delta Then
found = True
End If
ElseIf [step] = "e"C Then
found = True
End If
Else
If [step] = "i"C AndAlso delta = 1 Then
[step] = "e"C
delta = -1
ElseIf delta <> -8 Then
delta = -Math.Max(1, Math.Abs(delta \ 2))
[step] = "d"C
End If
topInterval = quality
End If
End While
ms.Position = 0
Dim folder As String = Environment.GetFolderPath(Environment.SpecialFolder.System) & "\oobe\info\backgrounds\"
If Not Directory.Exists(folder) Then
Directory.CreateDirectory(folder)
End If
Dim fs As New FileStream(folder & filename, FileMode.OpenOrCreate, FileAccess.Write)
fs.SetLength(0)
Dim octet As Integer
While (InlineAssignHelper(octet, ms.ReadByte())) <> -1
fs.WriteByte(CByte(octet))
End While
fs.Close()
ms.Close()
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
target = value
Return value
End Function
End Class