Click here to Skip to main content
15,880,405 members
Articles / Desktop Programming / Windows Forms

The Paper Hanger

Rate me:
Please Sign up or sign in to vote.
4.83/5 (5 votes)
17 May 2012CPOL3 min read 19.9K   475   6   2
A visual Logon UI and Desktop background Paper Hanger (changer) with JPEG file compression.

Image 1

Image 2

Instructions

Windows7 ™, VS11™ Beta, VB11™ Beta. This is for Windows7 ™ only. This program will be obsolete in Windows 8™ as users will be able to do this without this program. So for now this program is for Windows7 ™ users ONLY. This needs to be RunAs ADMIN because of the Registry entries. If you are not an ADMIN or you do not have ADMIN RIGHTS, then this app will display a message to you to apply RunAs admin rights, and then the app will exit gracefully or in other words Shutdown.

Intro

This is a visual style paper hanger (changer) that can change the Logon User Interface background and it can change your Desktop wallpaper. I added a file compression panel so you can compress some JPEGs to a suitable size (<= 256 KB) as Windows7 ™ does not use a pic that is greater than 256 KB for the LogonUI background. You can select a background pic for the LogonUI by clicking on a thumbnail in the DataGridView control at the top of the form. Then click Apply. The first thumbnail in the DatagridView is the Default Background that Windows7 ™ uses. The second thumbnail is the currently used pic. These two will always be the first and second (respectively) in the DatagridView and the rest of the pics that are less than or equal to 256 KB (DESIRED_SIZE). DESIRED_SIZE is a constant that is equal to 262144 (256 KB). This is used to check all images that are loaded in the grid. If larger, then they are skipped.

Background

The background for this idea came from Mr. Wolfy version: Best VB.NET article January 2010 and Julien Manici's WPF Version which are awesome. My version imitates Julien's except I used WinForms and the VS IDE Toolbox Standard Components. I have been using my app for better than a year now and decided to post it. This app however was created in VS11 beta, VB11 beta. I will be posting downloads for VB9.0 and VB10. They are all pretty much the same except for the designs.

Julien's app has some nice effects (MotionCircles animation) and (On-The-Fly) file compression (for JPEGs only) which I thought was excellent. Mine has file compression, but we do it manually. It has the look and feel of PS CS3's file compression. Before we can compress our files, we need to give the file a name to save as. I just add the letter "a" to the selected pic's name. This works fairly well for me.

Image 3

Code for the file compression...

VB
Private Sub DoImageCompression()
    If txtSaveAsJpeg.Text = "" Then
        MessageBox.Show("Please fill in the 'Save As *.jpg' Text box " & _ 
          "with '.jpg' at the end.", "Textbox err", _
          MessageBoxButtons.OK, MessageBoxIcon.Information)
        Exit Sub
    End If

    'Create a new bitmap image.
    Dim bmpToJpeg As New Bitmap(lstImages.SelectedItem.ToString())

    'Set a place holder for the memory stream length.
    Dim file_size As Long

    'Set the compression level.
    Dim compression_level As Long = CType(tbSize.Value, Long)

    ' Save the file into a memory stream.
    Dim memory_stream As MemoryStream = _
        SaveJpegIntoStream(bmpToJpeg, _
        compression_level)

    ' See how big it is.
    file_size = memory_stream.Length

    'Display the correct size with 2 decimal places.
    lblFileSize.Text = "File Size : " & _
      Math.Round((file_size / 1024), 2).ToString & " kb"

    'Set the trackbar value in the text box.
    txtTBValue.Text = tbSize.Value.ToString()

    'This boolean is used to keep the this Sub in check
    'until the desired_size is met by the User.
    'You should try and get as close to 256kb as possible.
    If isSaveAsButtonClicked = False Then
        'Dispose of this stuff as we have to start over
        'after exiting the sub.
        'Clean-Up.
        bmpToJpeg.Dispose()
        memory_stream.Dispose()
        Exit Sub
    End If

    If file_size <= DESIRED_SIZE Then
        ' Save the file.
        ' Display the final image.
        My.Computer.FileSystem.WriteAllBytes(mySaveLocation & txtSaveAsJpeg.Text, _
            memory_stream.ToArray(), False)
        picAfter.ImageLocation = mySaveLocation & txtSaveAsJpeg.Text

        'Show the User the most excellent job they are doing.
        MessageBox.Show("File saved!...")

        'Reset and dispose of some stuff.
        bmpToJpeg.Dispose()
        memory_stream.Dispose()
        btnSaveAs.Enabled = False
        isSaveAsButtonClicked = False
    End If

End Sub

When we are dragging the trackbar, on the MouseUp event, the suggested size shows in the text box below the trackbar. You need to do this a few times until you are close to 256 KB as possible without going over the DESIRED_SIZE. Once you get what you want, your pic is displayed next to the original on the left side, compressed file on the right.

I decided to add the Standard Wallpaper changer as this is a paper hanger.

Image 4

Using the code

To set our standard wallpaper, we need to add some pics to our listbox. *.bmps used to be the standard with MS but this changed when Windows Vista came out. Now we can select from a vast array of wallpaper pics, *.bmp,*.jpg,*.png, and so on.

Setting and saving our wallpaper...

VB
Private Sub SetWallpaper(ByVal img As Image)
    imageLocation = My.Computer.FileSystem.CombinePath(_
      My.Computer.FileSystem.SpecialDirectories.MyPictures, WallpaperPath)
    picStandardWP.ImageLocation = imageLocation

    'We could not do this with XP, 2000, or anything before Vista
    'because wallpaper could only be in *.bmp format.
    If imageLocation.EndsWith(".bmp") Then
        img.Save(imageLocation, System.Drawing.Imaging.ImageFormat.Bmp)
    ElseIf imageLocation.EndsWith(".jpg") Then
        img.Save(imageLocation, System.Drawing.Imaging.ImageFormat.Jpeg)
    ElseIf imageLocation.EndsWith(".png") Then
        img.Save(imageLocation, System.Drawing.Imaging.ImageFormat.Png)
    End If

    SystemParametersInfo(SPI_SETDESKWALLPAPER, 0&, imageLocation, _
                         SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE)
End Sub

First Time Run and Loading

When first running the The Paper Hanger, if it does not have Admin rights, then it will display a message stating this fact and let you know that it will shutdown after which you can do the right_click thing and add "RunAs admin" under the Security tab. Here is the code that does this....

VB
Private Sub frmPaperHanger_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
    Dim myPrincipal As WindowsPrincipal = _
        CType(System.Threading.Thread.CurrentPrincipal, WindowsPrincipal)

    If (myPrincipal.IsInRole(WindowsBuiltInRole.Administrator) = True) Then

        pnlLogonUI.Location = New Point(96, 179)
        pnlLogonUI.Show()

        pnlStandardWallpaper.Location = ptStandardWP
        pnlStandardWallpaper.Hide()

        pnlOptions.Location = ptOptions
        pnlOptions.Hide()

        pnlCompressFiles.Location = ptCompressionFiles
        pnlCompressFiles.Hide()

        pnlAbout.Location = ptAbout
        pnlAbout.Hide()

        LoadAndInitialize()
    End If
Catch ex As Exception
    MessageBox.Show("The Program does not currently have (runas admin) " & _ 
      "permission. Please set this.", "UAC Permission Needed", _
      MessageBoxButtons.OK, MessageBoxIcon.Information)
    Application.Exit()
End Try

Points of Interest

History

  • Uploaded The Paper Hanger: 04·20·2012.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Retired
United States United States
I am currently retired.
I have no degree but I have some programming experience
when I was in college(Cobol, Pascal).

My accomplishments thus far are;
Best VB.Net article for January(2009)
Best VB.Net article for July(2009)

Comments and Discussions

 
GeneralMy vote of 5 Pin
Guido R.Lefvre21-Apr-12 3:42
Guido R.Lefvre21-Apr-12 3:42 
GeneralRe: My vote of 5 Pin
rspercy6521-Apr-12 4:58
rspercy6521-Apr-12 4:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.