public static Stream GetVector(ApiPattern pattern, string filter) { var bitmap = GetMergedBitmap(pattern); var stream = FiltersHandler.ApplySvgFilter(bitmap, filter); return(stream); }
public ApiPattern CreateOrUpdate(ApiPattern apiPattern, Guid authorId, int scale, String filterName) { var patternImage = imagesLogic.CanvasesToPatternImage(apiPattern, scale, filterName); imagesLogic.SaveImage(patternImage); var jsonRawData = JsonConvert.SerializeObject(apiPattern.Canvases); var originalRawData = Encoding.UTF8.GetBytes(jsonRawData); var originalImage = new PatternImage() { RawData = originalRawData }; patternImagesRepository.Save(originalImage); var user = usersRepository.GetById(authorId); var pattern = patternsRepository.GetById(apiPattern.Id); if (pattern != null && pattern.Author != null && pattern.Author.Id == authorId) { ApplyChanges(apiPattern, pattern, patternImage, originalImage, user); patternsRepository.SaveChanges(); } if (pattern == null) { pattern = new Pattern(); ApplyChanges(apiPattern, pattern, patternImage, originalImage, user); patternsRepository.Save(pattern); } var savedApiPattern = patternsRepository.GetApiPattern(pattern.Id, authorId); return(savedApiPattern); }
public static List <String> GetTags(ApiPattern pattern) { var tags = new List <String>() { Strings.SeamlessPattern }; if (pattern.FilterName == "tiles") { tags.Add(Strings.TilesTexture); } if (pattern.FilterName == "knitting") { tags.Add(Strings.KnittingTexture); } if (pattern.FilterName == "embroidery") { tags.Add(Strings.EmbroideryTexture); } if (String.IsNullOrEmpty(pattern.FilterName) || pattern.FilterName == "none") { tags.Add(Strings.PixelArt); } return(tags); }
public String ConvertToBase64(ApiPattern apiPattern) { var image = imagesLogic.CanvasesToPatternImage(apiPattern, apiPattern.Scale, apiPattern.FilterName); var base64Image = Convert.ToBase64String(image.RawData); var result = @"data:" + image.ContentType + ";base64," + base64Image; return(result); }
public ApiPattern Index(ApiPattern apiPattern) { var currentUserId = CurrentUser != null ? CurrentUser.Id : new Guid(); var savedApiPattern = patternsLogic.CreateOrUpdate(apiPattern, currentUserId, apiPattern.Scale, apiPattern.FilterName); var url = Url.Link("Pattern", new { Controller = "Patterns", Action = "Pattern", id = savedApiPattern.Id }); savedApiPattern.Url = url; return(savedApiPattern); }
private static void ApplyChanges(ApiPattern apiPattern, Pattern pattern, PatternImage patternImage, PatternImage originalImage, User user) { pattern.PatternImage = patternImage; pattern.OriginalImage = originalImage; pattern.Author = user; pattern.Name = apiPattern.Name ?? ""; pattern.IsDraft = apiPattern.IsDraft; pattern.FilterName = apiPattern.FilterName; }
public PatternImage CanvasesToPatternImage(ApiPattern pattern, int scale, String filterName) { if (scale < 1 || scale > 4) { throw new ArgumentException("Scale can't be less than 1 and more than 4"); } var mergedBitmap = GetMergedBitmap(pattern); mergedBitmap = FiltersHandler.ApplyFilter(mergedBitmap, scale, filterName); byte[] bytes; String contentType; using (var stream = new MemoryStream()) { var pixelsCount = mergedBitmap.Width * mergedBitmap.Height; if (pixelsCount < 500 * 500) { mergedBitmap.Save(stream, ImageFormat.Png); contentType = "image/png"; } else { var quality = pixelsCount < 700 * 700 ? 100L : pixelsCount < 1000 * 1000 ? 80L : 60L; ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg); Encoder myEncoder = Encoder.Quality; var myEncoderParameters = new EncoderParameters(1); var myEncoderParameter = new EncoderParameter(myEncoder, quality); myEncoderParameters.Param[0] = myEncoderParameter; mergedBitmap.Save(stream, jgpEncoder, myEncoderParameters); contentType = "image/jpeg"; } stream.Close(); bytes = stream.ToArray(); } var result = new PatternImage() { RawData = bytes, ContentType = contentType }; return(result); }
private static Bitmap GetMergedBitmap(ApiPattern pattern) { var canvases = pattern.Canvases; var visibilities = pattern.LayersVisibility; var img = Base64ToImage(pattern.Canvases[0]); var mergedBitmap = new Bitmap(img.Width, img.Height); using (var graphics = Graphics.FromImage(mergedBitmap)) { graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, mergedBitmap.Width, mergedBitmap.Height); for (var i = canvases.Count - 1; i >= 0; i--) { if (visibilities == null || visibilities.Count < i || visibilities[i]) { var image = Base64ToImage(canvases[i]); var bitmap = new Bitmap(image); graphics.DrawImage(bitmap, 0, 0); } } } return(mergedBitmap); }
public ActionResult Index(Guid?id = null) { var currentUserId = CurrentUser != null ? CurrentUser.Id : new Guid(); var pattern = new ApiPattern() { }; if (id != null) { var savedPattern = patternsLogic.GetWithState(id.Value, currentUserId); if (savedPattern != null && savedPattern.Author != null && savedPattern.Author.Id == currentUserId) { pattern = savedPattern; } } var model = new IndexModel() { Pattern = pattern, CurrentUser = CurrentUser }; return(View(model)); }
public static String ImageUrl(this UrlHelper urlHelper, ApiPattern patternImage) { return(String.IsNullOrEmpty(patternImage.ImageCdnUrl) ? urlHelper.Action("Index", "Images", new { id = patternImage.ImageId }) : patternImage.ImageCdnUrl); }