Ejemplo n.º 1
0
        private void CreateImages(string sBaseImagePath)
        {
            var vOutPath = AppDomain.CurrentDomain.BaseDirectory;
            var vFileName = Path.GetFileNameWithoutExtension(sBaseImagePath);
            var vImageOutPath = vOutPath + @"\" + vFileName;
            Directory.CreateDirectory(vOutPath);
            var log = false; var verbose = false; var quiet = false;
            Logger.Init(vOutPath, log, verbose, quiet);
            Logger.WriteLine("Begin Resizing Images!");
            Logger.WriteVerbose("Writing files to {0}", vOutPath);
            if (chkiOS.Checked)
            {
                var viOSOutputPath = vImageOutPath + @"\iOSImages";
                Directory.CreateDirectory(viOSOutputPath);
                Logger.WriteVerbose("Created directory {0} for iOS Images", viOSOutputPath);

                var viOSizesFile = vOutPath + @"\SizeJsons\iOSizes.json";
                var vLoadedFileJson = File.ReadAllText(viOSizesFile);
                var viOsSizes = JsonConvert.DeserializeObject<IEnumerable<AppImage>>(vLoadedFileJson).OrderBy(x => x.Width);
                for (int iCount = 0; iCount < chkLstiOS.CheckedItems.Count; iCount++)
                {
                    var vSelSize = chkLstiOS.CheckedItems[iCount].ToString();
                    var vSelectedImage = (from SelImage in viOsSizes
                                          where SelImage.ImageName == vSelSize
                                          select SelImage).SingleOrDefault();
                    Logger.WriteVerbose("Creating Image for {0} of iOS Images", vSelSize);
                    var vNewImageName = viOSOutputPath + "\\" + vFileName + vSelectedImage.ImageName + ".png";

                    var width = Convert.ToInt32(vSelectedImage.Width);
                    var height = Convert.ToInt32(vSelectedImage.Height);
                    if (!ckbWidth.Checked || !ckbHeight.Checked)
                    {
                        var imageBase = Image.FromFile(sBaseImagePath);
                        if (ckbWidth.Checked)
                        {
                            height = width * imageBase.Height / imageBase.Width;
                        }
                        else
                        {
                            width = height * imageBase.Width / imageBase.Height;
                        }
                    }

                    var vNewIMage = Scale(width, height, sBaseImagePath);
                    vNewIMage.Save(vNewImageName);
                    Logger.WriteVerbose("Created Image for {0} of iOS Images", vSelSize);
                }

                if (chkLstiOS.CheckedItems.Count > 0)
                {
                    var vSelectedImage = viOsSizes.Last();
                    Logger.WriteVerbose("Creating Image for last of iOS Images");
                    var vNewImageName = viOSOutputPath + "\\" + vFileName + ".png";

                    var width = Convert.ToInt32(vSelectedImage.Width);
                    var height = Convert.ToInt32(vSelectedImage.Height);
                    if (!ckbWidth.Checked || !ckbHeight.Checked)
                    {
                        var imageBase = Image.FromFile(sBaseImagePath);
                        if (ckbWidth.Checked)
                        {
                            height = width * imageBase.Height / imageBase.Width;
                        }
                        else
                        {
                            width = height * imageBase.Width / imageBase.Height;
                        }
                    }

                    var vNewIMage = Scale(width, height, sBaseImagePath);
                    vNewIMage.Save(vNewImageName);
                    Logger.WriteVerbose("Created Image for last of iOS Images");
                }

                lbliOSTitle.Visible = true;
                lbliOSValue.Text = viOSOutputPath;
                lbliOSValue.Visible = true;
            }
            if (chkAndroid.Checked)
            {
                var vAndroidPath = vImageOutPath + @"\AndroidImages";
                Directory.CreateDirectory(vAndroidPath);
                Logger.WriteVerbose("Created directory {0} for Android Images", vAndroidPath);

                var vDroidSizesFile = vOutPath + @"\SizeJsons\AndroidSizes.json";
                var vDroidSizeJson = File.ReadAllText(vDroidSizesFile);
                var vDroidSizes = JsonConvert.DeserializeObject<IEnumerable<AppImage>>(vDroidSizeJson).OrderBy(x => x.Width);
                for (int iCount = 0; iCount < chlLstAndroid.CheckedItems.Count; iCount++)
                {
                    var vSelSize = chlLstAndroid.CheckedItems[iCount].ToString();
                    var vSelectedImage = (from SelImage in vDroidSizes
                                          where SelImage.ImageName == vSelSize
                                          select SelImage).SingleOrDefault();

                    Logger.WriteVerbose("Creating Image for {0} of Android Images", vSelSize);

                    var imagePath = Path.Combine(vAndroidPath, vSelectedImage.Path);
                    Directory.CreateDirectory(imagePath);

                    var vNewImageName = imagePath + "\\" + vFileName + ".png";

                    var width = Convert.ToInt32(vSelectedImage.Width);
                    var height = Convert.ToInt32(vSelectedImage.Height);
                    if (!ckbWidth.Checked || !ckbHeight.Checked)
                    {
                        var imageBase = Image.FromFile(sBaseImagePath);
                        if (ckbWidth.Checked)
                        {
                            height = width * imageBase.Height / imageBase.Width;
                        }
                        else
                        {
                            width = height * imageBase.Width / imageBase.Height;
                        }
                    }

                    var vNewIMage = Scale(width, height, sBaseImagePath);

                    vNewIMage.Save(vNewImageName);
                    Logger.WriteVerbose("Created Image for {0} of Android Images", vSelSize);
                }


                if (chlLstAndroid.CheckedItems.Count > 0)
                {
                    var vSelectedImage = vDroidSizes.Last();
                    Logger.WriteVerbose("Creating Image for last of Android Images");

                    var vNewImageName = vAndroidPath + "\\" + vFileName + ".png";

                    var width = Convert.ToInt32(vSelectedImage.Width);
                    var height = Convert.ToInt32(vSelectedImage.Height);
                    if (!ckbWidth.Checked || !ckbHeight.Checked)
                    {
                        var imageBase = Image.FromFile(sBaseImagePath);
                        if (ckbWidth.Checked)
                        {
                            height = width * imageBase.Height / imageBase.Width;
                        }
                        else
                        {
                            width = height * imageBase.Width / imageBase.Height;
                        }
                    }

                    var vNewIMage = Scale(width, height, sBaseImagePath);

                    vNewIMage.Save(vNewImageName);
                    Logger.WriteVerbose("Created Image for last of Android Images");
                }


                lblAndroid.Visible = true;
                lblAndroidValue.Text = vAndroidPath;
                lblAndroidValue.Visible = true;
            }
            if (chkUwp.Checked)
            {
                var vUwpPath = vImageOutPath + @"\UwpImages";
                Directory.CreateDirectory(vUwpPath);
                Logger.WriteVerbose("Created directory {0} for Uwp Images", vUwpPath);

                var vUwpSizesFile = vOutPath + @"\SizeJsons\UwpSizes.json";
                var vUwpSizeJson = File.ReadAllText(vUwpSizesFile);
                var vUwpSizes = JsonConvert.DeserializeObject<IEnumerable<AppImage>>(vUwpSizeJson).OrderBy(x => x.Width);
                for (int iCount = 0; iCount < chkLstUwp.CheckedItems.Count; iCount++)
                {
                    var vSelSize = chkLstUwp.CheckedItems[iCount].ToString();
                    var vSelectedImage = (from SelImage in vUwpSizes
                                          where SelImage.ImageName == vSelSize
                                          select SelImage).SingleOrDefault();

                    Logger.WriteVerbose("Creating Image for {0} of UWP Images", vSelSize);
                    var vNewImageName = vUwpPath + "\\" + vFileName + vSelectedImage.ImageName + ".png";

                    var width = Convert.ToInt32(vSelectedImage.Width);
                    var height = Convert.ToInt32(vSelectedImage.Height);
                    if (!ckbWidth.Checked || !ckbHeight.Checked)
                    {
                        var imageBase = Image.FromFile(sBaseImagePath);
                        if (ckbWidth.Checked)
                        {
                            height = width * imageBase.Height / imageBase.Width;
                        }
                        else
                        {
                            width = height * imageBase.Width / imageBase.Height;
                        }
                    }

                    var vNewIMage = Scale(width, height, sBaseImagePath);


                    vNewIMage.Save(vNewImageName);
                    Logger.WriteVerbose("Created Image for {0} of UWP Images", vSelSize);
                }


                if (chkLstUwp.CheckedItems.Count > 0)
                {
                    var vSelectedImage = vUwpSizes.Last();

                    Logger.WriteVerbose("Creating Image for last of UWP Images");
                    var vNewImageName = vUwpPath + "\\" + vFileName + ".png";

                    var width = Convert.ToInt32(vSelectedImage.Width);
                    var height = Convert.ToInt32(vSelectedImage.Height);
                    if (!ckbWidth.Checked || !ckbHeight.Checked)
                    {
                        var imageBase = Image.FromFile(sBaseImagePath);
                        if (ckbWidth.Checked)
                        {
                            height = width * imageBase.Height / imageBase.Width;
                        }
                        else
                        {
                            width = height * imageBase.Width / imageBase.Height;
                        }
                    }

                    var vNewIMage = Scale(width, height, sBaseImagePath);


                    vNewIMage.Save(vNewImageName);
                    Logger.WriteVerbose("Created Image for last of UWP Images");
                }

                lblUWPTitle.Visible = true;
                lblUWPValues.Text = vUwpPath;
                lblUWPValues.Visible = true;
            }
        }