Change Logon Background image in Windows Vista installation

Discussion in 'Mixed Languages' started by Alphawaves, Jan 31, 2015.

  1. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,886
    20,247
    180
    #1 Alphawaves, Jan 31, 2015
    Last edited by a moderator: Apr 20, 2017
    I thought i would share the code i created that will change your Windows Vista Logon background image during Windows installation.
    Accepts any image format.

    Updated 09/02/2015

    Code:
    using System;
    using System.Runtime.InteropServices;
    using System.Globalization;
    using System.IO;
    using System.Security.AccessControl;
    using System.Security.Principal;
    using System.Drawing.Imaging;
    using System.Drawing;
    using System.Text;
    
    namespace imageres
    {
        class Program
        {
            [DllImport("kernel32.dll", SetLastError = true)]
            public static extern IntPtr BeginUpdateResource(string pFileName, [MarshalAs(UnmanagedType.Bool)]bool bDeleteExistingResources);
            [DllImport("kernel32.dll", SetLastError = true)]
            public static extern bool UpdateResource(IntPtr hUpdate, string lpType, uint lpName, uint wLanguage, byte[] lpData, uint cbData);
            [DllImport("kernel32.dll", SetLastError = true)]
            public static extern bool EndUpdateResource(IntPtr hUpdate, bool fDiscard);
            public static string RootPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
            public static string msoobe = "";
    
            //Do the work
            static void Main(string[] args)
            {
                try
                {
                    if (IsOS())
                    {
                        if (File.Exists(RootPath + @"\imageres.dll"))
                        {
                            if (!File.Exists(RootPath + @"\imageres.dll.BACKUP"))
                            {
                                File.Copy(RootPath + @"\imageres.dll", RootPath + @"\imageres.dll.BACKUP", true);
                            }
                            TakeOwn(RootPath + @"\imageres.dll");
                            foreach (uint image in imageID)
                            {
                                msoobe = SourceFile(new string[image]);
                                byte[] img = ToJpeg(new Bitmap(SourceFile(new string[image])), ImageFormat.Jpeg);
                                byte[] res = new byte[img.Length];
                                Buffer.BlockCopy(img, 0, res, 0, res.Length);
                                IntPtr ipTarget = BeginUpdateResource(RootPath + @"\imageres.dll", false);
                                if (UpdateResource(ipTarget, "IMAGE", image, Culture(), res, (uint)res.Length))
                                {
                                    EndUpdateResource(ipTarget, false);
                                }
                            }
                            if (File.Exists(RootPath + @"\oobe\msoobe.jpg") && File.Exists(msoobe))
                            {
                                TakeOwn(RootPath + @"\oobe\msoobe.jpg");
                                if (!File.Exists(RootPath + @"\oobe\msoobe.BACKUP.jpg"))
                                {
                                    File.Move(RootPath + @"\oobe\msoobe.jpg", RootPath + @"\oobe\msoobe.BACKUP.jpg");
                                }
                                if (File.Exists(msoobe))
                                {
                                    SaveImage(msoobe, RootPath + @"\oobe\msoobe.jpg", ImageFormat.Jpeg, true);
                                }
                            }
                        }
                    }
                }
                catch
                {
                }
            }
    
            //Get UI culture
            public static uint Culture()
            {
                 return (uint)CultureInfo.GetCultureInfoByIetfLanguageTag(CultureInfo.InstalledUICulture.Name).LCID;
            }
    
            //Check if OS is 6.0
            public static bool IsOS()
            {
               return Environment.OSVersion.Version.Major == 6 & Environment.OSVersion.Version.Minor == 0 ? true : false;
            }
    
            //Get the images to replace in imageres.dll 
            public static int[,] imageID = new int[,] { 
                { 5031 },
                { 5032 },
                { 5033 },
                { 5034 },
                { 5035 },
                { 5036 },
                { 5037 },
                { 5038 },
                { 5039 },
                { 5040 },
                { 5041 },
                { 5042 },
                { 5043 }
            };
    
            //Get the image location from commandline argument
            public static string SourceFile(string[] images)
            {
                foreach (string str in Environment.GetCommandLineArgs())
                {
                    if (str.ToUpper().Contains("/I="))
                    {
                        images = str.Split(new string[] { "/I=" }, StringSplitOptions.None);
                        break;
                    }
                }            
                return images[1];  
            }
    
            //Convert and save image to JPEG from Memory stream
            public static void SaveImage(string input, string output, ImageFormat format, bool overwrite)
            {
                MemoryStream ms = new MemoryStream(File.ReadAllBytes(input));
                Image img = Image.FromStream(ms);
                if (overwrite && File.Exists(output))
                {
                    File.Delete(output);
                }
                img.Save(output, format);
                ms.Close();
            }  
            
            //Convert image to JPEG in Memory stream
            public static byte[] ToJpeg(Image image, ImageFormat format)
            {
                MemoryStream ms = new MemoryStream();
                image.Save(ms, format);
                byte[] img = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(img, 0, (int)ms.Length);
                ms.Close();
                return img;
            }
    
            //Take ownership of imageres.dll
            public static void TakeOwn(string file)
            {
                FileSecurity own = File.GetAccessControl(file);
                SecurityIdentifier Everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
                own.AddAccessRule(new FileSystemAccessRule(Everyone, FileSystemRights.FullControl, AccessControlType.Allow));
                File.SetAccessControl(file, own);
            }
        }
    }
    Usage example in oobe.cmd:
    Code:
    "%~dp0imageres.exe" /I="My image location"
    ie: "%~dp0imageres.exe" /I="%windir%\Setup\scripts\Wallpaper.jpg"
    ie: "%~dp0imageres.exe" /I="%windir%\Setup\scripts\Wallpaper.bmp"
    ie: "%~dp0imageres.exe" /I="%windir%\Setup\scripts\Wallpaper.png"
    
    Download source code:
    Code:
    http://www.datafilehost.com/d/6dd36806
    Download Compiled .exe:
    Code:
    http://www.datafilehost.com/d/67b1d057
    ;)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...