public void Run() { // TODO: // Grab an image from a public URL and write a function thats rescale the image to a desired format // The use of 3rd party plugins is permitted // For example: 100x80 (thumbnail) and 1200x1600 (preview) // Install-Package ImageLibrary // http://kaliko.com/image-library/ Directory.CreateDirectory(@"C:\MyImages"); var image = new KalikoImage(@"https://web.usask.ca/images/homer.jpg"); image.SaveJpg(@"C:\MyImages\org.jpg", 99); // Create thumbnail by cropping KalikoImage thumb = image.Scale(new CropScaling(128, 128)); thumb.SaveJpg(@"C:\MyImages\thumbnail-fit.jpg", 99); // Create preview 1200x1600 image.Resize(1600, 1200); image.SaveJpg(@"C:\MyImages\preview.jpg", 99); Console.WriteLine("Images saved to C:\\MyImages\\"); }
private string SaveImage() { if (!DoesImageNeedHandling) { return(_originalPath); } var image = new KalikoImage(Server.MapPath(_originalPath)); // TODO: Temporary fix for selecting full image, try to do this without loading the image first.. if (IsCropValueFullImage(image)) { return(_originalPath); } if (_hasCropValues) { image.Crop(_cropX, _cropY, _cropW, _cropH); } if (_width > 0 || _height > 0) { image.Resize(_width, _height); } var imagePath = GetNewImagePath(); var serverPath = Server.MapPath(imagePath); if (File.Exists(serverPath)) { var originalServerPath = Server.MapPath(_originalPath); if (File.GetLastWriteTime(originalServerPath) < File.GetLastWriteTime(serverPath)) { return(imagePath); } File.Delete(serverPath); } // TODO: Config quality image.SaveJpg(Server.MapPath(imagePath), 90); return(imagePath); }
private void ProcessImage(KalikoImage image) { image.Resize(XSIZE, YSIZE); // for faster operations / debugging // operating on image NormalizationFilter normalization = new NormalizationFilter(); image.ApplyFilter(normalization); FastGaussianBlurFilter gaussian = new FastGaussianBlurFilter(5f); image.ApplyFilter(gaussian); Histogram histogram = new Histogram(image); byte filteredThreshold = histogram.GetThresholdLevel(); ThresholdFilter threshold = new ThresholdFilter(filteredThreshold); image.ApplyFilter(threshold); }
public void CropAndResizeImage(HttpPostedFileBase imageFile, string outPutFilePath, string outPuthFileName, int width, int height, bool pngFormat = false) { try { Image image = Image.FromStream(imageFile.InputStream); //if (!width.HasValue) //{ // width = image.Width; //} //if (!height.HasValue) //{ // height = new int?(image.Height); //} KalikoImage kalikoImage = new KalikoImage(image); kalikoImage.Resize(width, height); //KalikoImage kalikoImage1 = kalikoImage.Scale(new CropScaling(width, height)); //KalikoImage kalikoImage1 = kalikoImage.Scale(new FitScaling(width.Value, height.Value)); if (!Directory.Exists(HttpContext.Current.Server.MapPath(string.Concat("~/", outPutFilePath)))) { Directory.CreateDirectory(HttpContext.Current.Server.MapPath(string.Concat("~/", outPutFilePath))); } string str = HttpContext.Current.Server.MapPath(string.Concat("~/", Path.Combine(outPutFilePath, outPuthFileName))); if (!pngFormat) { kalikoImage.SaveJpg(str, 99); } else { kalikoImage.SavePng(str); } kalikoImage.Dispose(); //kalikoImage.Dispose(); } catch (Exception exception) { throw new Exception(exception.Message); } }
// 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 } }); }