private string GetPath_(PatternImage image) { Require.NotNull(image, "image"); Contract.Ensures(Contract.Result <string>() != null); return(Path.Combine(_config.PatternDirectory, image.RelativePath)); }
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 void CreateNewPattern(PatternImage pattern) { using (var uow = UnitOfWork.Create()) { uow.PatternImageRepository.AddNewPattern(pattern); uow.Commit(); } }
public void SaveImage(PatternImage patternImage) { var uploader = new GcsUploader(HttpContext.Current.Request.MapPath("~")); var extension = "." + patternImage.ContentType.Replace("image/", ""); var name = patternImage.Id.ToString() + extension; patternImage.CdnUrl = uploader.Upload(name, patternImage.ContentType, patternImage.RawData); patternImagesRepository.Save(patternImage); }
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); }
public void ChangeAvatar(ApiUser apiUser, Stream fileStream) { var user = usersRepository.GetById(apiUser.Id); if (user != null) { var pngRawData = ImagesLogic.CreateAvatar(fileStream); var patternImage = new PatternImage() { RawData = pngRawData, ContentType = "image/png" }; imagesLogic.SaveImage(patternImage); user.Avatar = patternImage; usersRepository.SaveChanges(); } }
private void drawPattern(Pattern pat) { texturePat = new Texture2D(pat.W, pat.H); patternImage.texture = texturePat; patternImage.material.mainTexture = texturePat; ImageProcessor.DrawPattern(pat, texturePat); PatternImage pi = null; if (pat is PatternImage) { pi = (PatternImage)pat; } else if (pat is PatternGroup && (((PatternGroup)pat) [0]) is PatternImage) { pi = (PatternImage)(((PatternGroup)pat) [0]); } GameObject.Find("Ratio Slider").GetComponent <Slider> ().value = (pat.Tol - 0.0f) / 1.0f; if (pi != null) { GameObject.Find("Threshold Slider").GetComponent <Slider> ().value = (pi.Thrs [1] - 0.0f) / 1.0f; Debug.Log("Thres: " + pi.Thrs [1]); } texturePat.Apply(); }