예제 #1
0
        // WARNING: Very ugly code ahead, quick and dirty method to generate an image
        private Dictionary <string, string> BuildCDMBar(string barName, string barNameBackgroundColour, string ingredient1, string ingredient2, string ingredient3, string directoryId, string userImagesPath)
        {
            // Locate base directory for CDM bar assets
            string cdmBarAssetsPath = Path.Combine(GetRequestApplicationPath(), "App_Data", "cdm_bar");

            var    fileId        = Guid.NewGuid().ToString("N");
            string imageFilePath = $"{directoryId}/{fileId}-1.jpg";
            //string imageShareFilePath = $"{directoryId}/{fileId}-2.jpg";
            string imageShareWideFilePath = $"{directoryId}/{fileId}-3.jpg";

            // Count the amount of ingredients
            int ingredientAmount = 0;

            ingredientAmount += !String.IsNullOrEmpty(ingredient1) ? 1 : 0;
            ingredientAmount += !String.IsNullOrEmpty(ingredient2) ? 1 : 0;
            ingredientAmount += !String.IsNullOrEmpty(ingredient3) ? 1 : 0;

            // Create a new canvas based on background image (helps in getting a dynamic dimension also)
            String backgroundImagePath = Path.Combine(cdmBarAssetsPath, "background.png");

            using (var image = new KalikoImage(backgroundImagePath))
            {
                // Composite the image with ingredients by blitting, yeah too many nested IFs
                if (ingredientAmount > 0)
                {
                    var ingredient1ImagePath = Path.Combine(cdmBarAssetsPath, ingredient1.ToLower());
                    using (var ingredientImage = new KalikoImage(ingredient1ImagePath))
                    {
                        // Very important to set the image to the same resolution as the exported file, else proportions won't be maintained
                        ingredientImage.SetResolution(96, 96);
                        image.BlitImage(ingredientImage, 2596, 1090);
                    }

                    if (ingredientAmount > 1)
                    {
                        var ingredient2ImagePath = Path.Combine(cdmBarAssetsPath, ingredient2.ToLower());
                        using (var ingredientImage = new KalikoImage(ingredient2ImagePath))
                        {
                            // Very important to set the image to the same resolution as the exported file, else proportions won't be maintained
                            ingredientImage.SetResolution(96, 96);
                            image.BlitImage(ingredientImage, 2646, 698);
                        }

                        if (ingredientAmount > 2)
                        {
                            var ingredient3ImagePath = Path.Combine(cdmBarAssetsPath, ingredient3.ToLower());
                            using (var ingredientImage = new KalikoImage(ingredient3ImagePath))
                            {
                                // Very important to set the image to the same resolution as the exported file, else proportions won't be maintained
                                ingredientImage.SetResolution(96, 96);
                                image.BlitImage(ingredientImage, 2390, 392);
                            }
                        }
                    }
                }

                // Overlay the CDM Bar with "masking" holes
                var cdmBarImagePath = Path.Combine(cdmBarAssetsPath, $"bar-ingredient-x{ingredientAmount.ToString()}.png");
                using (var cdmBarImage = new KalikoImage(cdmBarImagePath))
                {
                    cdmBarImage.SetResolution(96, 96);
                    image.BlitImage(cdmBarImage);
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();

                // Overlay the text background if any
                if (!(String.IsNullOrEmpty(barNameBackgroundColour) || barNameBackgroundColour.ToLower().Equals("transparent")))
                {
                    var barNameBackgroundImagePath = Path.Combine(cdmBarAssetsPath, $"back-{barNameBackgroundColour.ToLower()}.png");
                    using (var barNameBackgroundImage = new KalikoImage(barNameBackgroundImagePath))
                    {
                        barNameBackgroundImage.SetResolution(96, 96);
                        image.BlitImage(barNameBackgroundImage);
                    }
                }

                // Add the final touch, the "pièce de résistance", the centered angled text!
                var fontPath = Path.Combine(cdmBarAssetsPath, "MonkyBasicRegular.ttf");
                image.SetFont(fontPath, 110, FontStyle.Regular);
                var text = new TextField(barName.ToUpper());
                text.Rotation          = -14.1f;
                text.TextColor         = Color.White;
                text.Alignment         = StringAlignment.Center;
                text.VerticalAlignment = StringAlignment.Center;
                text.TargetArea        = new Rectangle(900, 1231, 1800, 618);
                image.DrawText(text);

                // Create destination directory if it doesn't already exist
                String destinationDirectoryPath = Path.Combine(userImagesPath, directoryId);
                System.IO.Directory.CreateDirectory(destinationDirectoryPath);

                // Create the share image (square aspect ratio)

                /*
                 * String backgroundShareImagePath = Path.Combine(cdmBarAssetsPath, "background-share.png");
                 * using (var shareImage = new KalikoImage(backgroundShareImagePath))
                 * {
                 *  shareImage.SetResolution(96, 96);
                 *  shareImage.BlitImage(image);
                 *
                 *  // Save the square share image
                 *  String destinationShareFilePath = Path.Combine(userImagesPath, imageShareFilePath);
                 *  // Resize it for front-end display
                 *  shareImage.Resize(1200, 1200);
                 *  shareImage.SaveJpg(destinationShareFilePath, 90, true);
                 * }
                 */

                // Create the share image (wide aspect ratio)
                String backgroundShareImageWidePath = Path.Combine(cdmBarAssetsPath, "background-share-wide.png");
                using (var shareImageWide = new KalikoImage(backgroundShareImageWidePath))
                {
                    shareImageWide.SetResolution(96, 96);
                    shareImageWide.BlitImage(image, 1286, -68);
                    // Compose the text onto the image
                    String textShareImageWidePath = Path.Combine(cdmBarAssetsPath, "text-share-wide.png");
                    using (var shareTextWide = new KalikoImage(textShareImageWidePath))
                    {
                        shareTextWide.SetResolution(96, 96);
                        shareImageWide.BlitImage(shareTextWide);
                    }

                    // Save the wide share image
                    String destinationShareWideFilePath = Path.Combine(userImagesPath, imageShareWideFilePath);
                    // Resize it for front-end display
                    shareImageWide.Resize(1200, 630);
                    shareImageWide.SaveJpg(destinationShareWideFilePath, 90, true);
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();

                // Save the composited image
                String destinationFilePath = Path.Combine(userImagesPath, imageFilePath);
                image.SetResolution(96, 96);
                // Resize it for front-end display
                image.Resize(520, 360);
                image.SaveJpg(destinationFilePath, 90, true);
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();

            return(new Dictionary <string, string> {
                { "FileId", fileId },
                { "CompositedImage", imageFilePath },
                { "CompositedImageShare", imageShareWideFilePath },
                { "CompositedImageShareWide", imageShareWideFilePath }
            });
        }