public void Build_CreatesTargetDirectory_WhenAskedTo()
 {
     using (var source = GetBitmapStream(100, 100))
         using (var targetStream = new MemoryStream())
         {
             source.Seek(1, SeekOrigin.Begin);
             var originalPosition = source.Position;
             var directory        = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
             Assume.That(!Directory.Exists(directory));
             var destinationPath = Path.Combine(directory, "test.jpg");
             try
             {
                 var instructions = new Instructions {
                     Width = 50
                 };
                 ImageBuilder.Build(source, destinationPath, true, instructions);
                 Assert.True(Directory.Exists(directory));
             }
             finally
             {
                 File.Delete(destinationPath);
                 Directory.Delete(directory, false);
             }
         }
 }
Esempio n. 2
0
        public static void ProcessImage(IEnumerable <IPlugin> extensions, object source, object target = null, ResizeSettings resizeSettings = null)
        {
            resizeSettings = resizeSettings ?? new ResizeSettings()
            {
            };

            target = target ?? new MemoryStream();

            var builderConfig = new ImageResizer.Configuration.Config();

            foreach (var extension in extensions)
            {
                extension.Install(builderConfig);
            }

            var imageBuilder = new ImageBuilder(builderConfig.Plugins.ImageBuilderExtensions, builderConfig.Plugins,
                                                builderConfig.Pipeline, builderConfig.Pipeline);

            Console.WriteLine("processing " + source);
            if (source is string && source.ToString().StartsWith("http"))
            {
                source = new MemoryStream(new WebClient().DownloadData(source.ToString()));
            }

            imageBuilder.Build(source, target, resizeSettings, false);
        }
 public void EncodeImage(int width, int height, OutputFormat format, int?jpegQuality = null)
 {
     using (var source = GetBitmapStream(width, height))
         using (var _ = Stream.Null)
         {
             var instructions = new Instructions {
                 Format = format, JpegQuality = jpegQuality ?? 90
             };
             ImageBuilder.Build(source, _, instructions);
         }
 }
 public void Build_LeavesTargetStreamOpen_WhenAskedTo()
 {
     using (var _ = GetBitmapStream(100, 100))
         using (var destination = new MemoryStream())
         {
             var instructions = new Instructions {
                 Width = 50
             };
             ImageBuilder.Build(_, destination, true, instructions);
             Assert.True(destination.CanRead);
         }
 }
 public void Build_LeavesSourceStreamOpen_WhenAskedTo(SourceOptions sourceOptions)
 {
     using (var source = GetBitmapStream(100, 100))
         using (var _ = Stream.Null)
         {
             var instructions = new Instructions {
                 Width = 50
             };
             ImageBuilder.Build(source, sourceOptions, _, instructions);
             Assert.True(source.CanRead);
         }
 }
 public void Build_ClosesSourceStream_WhenNotAskedToLeaveItOpen(SourceOptions sourceOptions)
 {
     using (var source = GetBitmapStream(100, 100))
         using (var _ = Stream.Null)
         {
             var instructions = new Instructions {
                 Width = 50
             };
             ImageBuilder.Build(source, sourceOptions, _, instructions);
             Assert.False(source.CanRead);
         }
 }
 public void Build_ClosesTargetStream_WhenNotAskedToLeaveItOpen()
 {
     using (var _ = GetBitmapStream(100, 100))
         using (var destination = new MemoryStream())
         {
             var instructions = new Instructions {
                 Width = 50
             };
             ImageBuilder.Build(_, destination, instructions);
             Assert.False(destination.CanRead);
         }
 }
 public void Build_DoesNotRewindSourceStream_WhenNotAskedTo(SourceOptions sourceOptions)
 {
     using (var source = GetBitmapStream(100, 100))
         using (var _ = Stream.Null)
         {
             source.Seek(1, SeekOrigin.Begin);
             var originalPosition = source.Position;
             var instructions     = new Instructions {
                 Width = 50
             };
             ImageBuilder.Build(source, sourceOptions | LeaveOpen, _, instructions);
             Assume.That(source.CanSeek);
             Assert.AreNotEqual(originalPosition, source.Position);
         }
 }
 public void Build_ProducesBitmapLargerThanOriginal_GivenScaleMode(ScaleMode scale)
 {
     using (var source = GetBitmapStream(100, 100))
         using (var destination = new MemoryStream())
         {
             var instructions = new Instructions {
                 Scale = scale, Width = 200, Height = 200
             };
             ImageBuilder.Build(source, destination, true, instructions);
             using (var output = new Bitmap(destination))
             {
                 Assert.AreEqual(output.Width, 200);
                 Assert.AreEqual(output.Height, 200);
             }
         }
 }
Esempio n. 10
0
 public void Build_ProducesBitmapWithSmallerMaxAndSameAspectRatio_GivenFitModeMax()
 {
     using (var sourceStream = GetBitmapStream(100, 66))
         using (var targetStream = new MemoryStream())
         {
             var instructions = new Instructions {
                 Width = 12, Height = 34
             };
             ImageBuilder.Build(sourceStream, targetStream, true, instructions);
             using (var output = new Bitmap(targetStream))
             {
                 Assert.AreEqual(output.Width, 12);
                 Assert.AreEqual(output.Height, 8);
             }
         }
 }
Esempio n. 11
0
 public void Build_ProducesBitmapWithExactSpecifiedWidthAndHeight_GivenFitMode(FitMode mode)
 {
     using (var sourceStream = GetBitmapStream(100, 100))
         using (var targetStream = new MemoryStream())
         {
             var instructions = new Instructions {
                 Mode = mode, Width = 12, Height = 34
             };
             ImageBuilder.Build(sourceStream, targetStream, true, instructions);
             using (var output = new Bitmap(targetStream))
             {
                 Assert.AreEqual(output.Width, 12);
                 Assert.AreEqual(output.Height, 34);
             }
         }
 }
Esempio n. 12
0
 /// <summary>
 /// Shortuct to CurrentImageBuilder.Build (Useful for COM clients). Also creates a destination folder if needed, unlike the normal .Build() call.
 ///
 /// </summary>
 /// <param name="source"></param>
 /// <param name="dest"></param>
 /// <param name="settings"></param>
 public void BuildImage(object source, object dest, string settings)
 {
     if (dest is string)
     {
         string d = dest as string;
         //If it's not a virtual path, make sure the directory exists.
         if (!string.IsNullOrEmpty(d) && !d.StartsWith("~") && !(d.Contains("/")) && d.Contains("\\"))
         {
             d = Path.GetDirectoryName(d);
             if (!Directory.Exists(d))
             {
                 Directory.CreateDirectory(d);
             }
         }
     }
     CurrentImageBuilder.Build(source, dest, new ResizeSettings(settings));
 }
Esempio n. 13
0
        public void Build_Succeeds_EvenWhenSourceStreamPositionNotAt0(SourceOptions sourceOptions)
        {
            TestDelegate action = () =>
            {
                using (var source = GetBitmapStream(100, 100))
                    using (var _ = Stream.Null)
                    {
                        source.Seek(17, SeekOrigin.Begin);
                        var instructions = new Instructions {
                            Width = 50
                        };
                        ImageBuilder.Build(source, sourceOptions, _, instructions);
                    }
            };

            Assert.DoesNotThrow(action);
        }
Esempio n. 14
0
        public void Build_CannotWriteToNonExistentDirectory_WhenNotAskedToCreateIt()
        {
            TestDelegate action = () =>
            {
                using (var source = GetBitmapStream(100, 100))
                {
                    source.Seek(1, SeekOrigin.Begin);
                    var originalPosition = source.Position;
                    var directory        = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
                    Assume.That(!Directory.Exists(directory));
                    var destinationPath = Path.Combine(directory, "test.jpg");
                    var instructions    = new Instructions {
                        Width = 50
                    };
                    ImageBuilder.Build(source, destinationPath, instructions);
                }
            };

            Assert.Throws <DirectoryNotFoundException>(action);
        }
Esempio n. 15
0
        public async Task <HttpResponseMessage> UploadImage()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                this.Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
            }
            var uploadFolder = "~/Resource/ImageFiles";
            var provider     = GetMultipartProvider(uploadFolder);
            var result       = await Request.Content.ReadAsMultipartAsync(provider);

            var originalFileName = GetDeserializedFileName(result.FileData.First());

            var uploadedFileInfo = new FileInfo(result.FileData.First().LocalFileName);

            var username = GetFormData(result);
            var destDir  = uploadFolder + "/" + username;
            var destPath = destDir + "/" + originalFileName;


            var    destLocalDir  = HttpContext.Current.Server.MapPath(destDir);
            var    destLocalPath = HttpContext.Current.Server.MapPath(destPath);
            string host          = Request.GetRequestContext().Url.Request.Headers.Host;
            var    destThumbPath = destDir + "/" + originalFileName.Split('.')[0] + "_thumb." +
                                   originalFileName.Split('.')[1];

            var destLocalThumbPath = HttpContext.Current.Server.MapPath(destThumbPath);

            Directory.CreateDirectory(destLocalDir);
            File.Move(uploadedFileInfo.FullName, destLocalPath);
            ResizeSettings settings = new ResizeSettings("width=100&height=100");
            ImageBuilder   builder  = ImageBuilder.Current;

            builder.Build(destLocalPath, destLocalThumbPath, settings);
            var user = await
                       _usersRepo.SaveProfileImagePath(username, destPath.Replace("~", "http://" + host), destThumbPath.Replace("~", "http:/ /" + host));

            var returnData = "ImageUploaded";

            return(this.Request.CreateResponse(HttpStatusCode.OK, new { returnData }));
        }
Esempio n. 16
0
        //private static void BuildAppConfigFile(string root_path, AppProject project)
        //{
        //    GameProperties props = new GameProperties()
        //    {
        //        Title = project.Title,
        //        FrameRate = project.FrameRate,
        //        CanvasWidth = project.CanvasWidth,
        //        CanvasHeight = project.CanvasHeight,
        //        Fullscreen = project.StartFullscreen,
        //        PreloadResourcePaks = project.PreloadPaks
        //    };

        //    File.WriteAllBytes(Path.Combine(root_path, "Config.json"),
        //        JsonSerializer.PrettyPrintByteArray(JsonSerializer.Serialize(props)));

        //}

        private static List <ResourcePak> BuildProjectResources(GameAssetsManifest manifest)
        {
            var resource_groups = manifest.Resources;

            var results = new List <ResourcePak>();

            foreach (var resource_group in resource_groups)
            {
                var pak = new ResourcePak(resource_group.Key);

                Console.WriteLine($"Creating resource Pak: {pak.Name}");

                if (resource_group.Value.Images != null)
                {
                    foreach (var image_info in resource_group.Value.Images)
                    {
                        var pixmap_data = ImageBuilder.Build(image_info.Id, image_info.Path);

                        pak.Images.Add(image_info.Id, pixmap_data);

                        pak.TotalResourcesCount++;

                        Console.WriteLine($"Added Image: {pixmap_data.Id}");
                    }
                }

                if (resource_group.Value.Shaders != null)
                {
                    foreach (var shader_info in resource_group.Value.Shaders)
                    {
                        var shader_data = ShaderBuilder.Build(shader_info.Id, shader_info.VsPath, shader_info.FsPath);

                        pak.Shaders.Add(shader_info.Id, shader_data);

                        pak.TotalResourcesCount++;

                        Console.WriteLine($"Added Shader: {shader_data.Id}");
                    }
                }

                if (resource_group.Value.Fonts != null)
                {
                    foreach (var font_info in resource_group.Value.Fonts)
                    {
                        var build_params = new FontBuildParams()
                        {
                            Id             = font_info.Id,
                            Path           = font_info.Path,
                            Size           = font_info.Size,
                            CharRangeLevel = font_info.CharRangeLevel,
                            PaddingLeft    = font_info.Padding != null ? font_info.Padding[0] : 0,
                            PaddingRight   = font_info.Padding != null ? font_info.Padding[1] : 0,
                            PaddingUp      = font_info.Padding != null ? font_info.Padding[2] : 0,
                            PaddingDown    = font_info.Padding != null ? font_info.Padding[3] : 0,
                            DropShadow     = font_info.DropShadow,
                            ShadowOffsetX  = font_info.ShadowOffsetX,
                            ShadowOffsetY  = font_info.ShadowOffsetY,
                            ShadowColor    = font_info.ShadowColor != null?Color.FromHex(font_info.ShadowColor) : Color.Black
                        };

                        var font_data = FontBuilder.Build(build_params);

                        pak.Fonts.Add(font_info.Id, font_data);

                        pak.TotalResourcesCount++;

                        Console.WriteLine($"Added Font: {font_data.Id}");
                    }
                }

                if (resource_group.Value.Atlases != null)
                {
                    foreach (var atlas_info in resource_group.Value.Atlases)
                    {
                        var atlas_data = AtlasBuilder.Build(atlas_info.Id, atlas_info.Path, atlas_info.Regions);

                        pak.Atlases.Add(atlas_data.Id, atlas_data);

                        pak.TotalResourcesCount++;

                        Console.WriteLine($"Added Atlas: {atlas_data.Id}");
                    }
                }

                if (resource_group.Value.TextFiles != null)
                {
                    foreach (var text_file_info in resource_group.Value.TextFiles)
                    {
                        var text_file_data = TextBuilder.Build(text_file_info.Id, text_file_info.Path);
                        pak.TextFiles.Add(text_file_info.Id, text_file_data);

                        pak.TotalResourcesCount++;

                        Console.WriteLine($"Added TextFile: {text_file_data.Id}");
                    }
                }

                results.Add(pak);
                Console.WriteLine($"Built PAK with {pak.TotalResourcesCount} resources.");
            }

            return(results);
        }
Esempio n. 17
0
 public void Build_ThrowsArgumentNullException_WhenInstructionsNull()
 {
     Assert.Throws <ArgumentNullException>(() => ImageBuilder.Build(Stream.Null, Stream.Null, (Instructions)null));
 }
Esempio n. 18
0
        //private static void BuildAppConfigFile(string root_path, AppProject project)
        //{
        //    GameProperties props = new GameProperties()
        //    {
        //        Title = project.Title,
        //        FrameRate = project.FrameRate,
        //        CanvasWidth = project.CanvasWidth,
        //        CanvasHeight = project.CanvasHeight,
        //        Fullscreen = project.StartFullscreen,
        //        PreloadResourcePaks = project.PreloadPaks
        //    };

        //    File.WriteAllBytes(Path.Combine(root_path, "Config.json"),
        //        JsonSerializer.PrettyPrintByteArray(JsonSerializer.Serialize(props)));

        //}

        private static List <ResourcePak> BuildProjectResources(GameAssetsManifest manifest)
        {
            var resource_groups = manifest.Resources;

            var results = new List <ResourcePak>();

            foreach (var(groupKey, group) in resource_groups)
            {
                var pak = new ResourcePak(groupKey);

                Console.WriteLine($"Creating resource Pak: {pak.Name}");

                if (group.Images != null)
                {
                    foreach (var image_info in group.Images)
                    {
                        var pixmap_data = ImageBuilder.Build(image_info.Id, image_info.Path);

                        pak.Images.Add(image_info.Id, pixmap_data);

                        pak.TotalResourcesCount++;

                        Console.WriteLine($"Added Image: {pixmap_data.Id}");
                    }
                }

                if (group.Shaders != null)
                {
                    foreach (var shader_info in group.Shaders)
                    {
                        var shader_data = ShaderBuilder.Build(shader_info.Id, shader_info.VsPath, shader_info.FsPath);

                        pak.Shaders.Add(shader_info.Id, shader_data);

                        pak.TotalResourcesCount++;

                        Console.WriteLine($"Added Shader: {shader_data.Id}");
                    }
                }

                if (group.Fonts != null)
                {
                    foreach (var font_info in group.Fonts)
                    {
                        var build_params = new FontBuildParams()
                        {
                            Id          = font_info.Id,
                            LineSpacing = font_info.LineSpacing,
                            Spacing     = font_info.Spacing,
                            DefaultChar = font_info.DefaultChar,
                            Faces       = font_info.Faces.Select(f => new FontFace()
                            {
                                CharRanges = f.CharRanges.Select(CharRange.GetFromKey).ToList(),
                                Path       = f.Path,
                                Size       = f.Size,
                            }).ToList()
                        };

                        var font_data = FontBuilder.Build(build_params);

                        pak.Fonts.Add(font_info.Id, font_data);

                        pak.TotalResourcesCount++;

                        Console.WriteLine($"Added Font: {font_data.Id}");
                    }
                }

                if (group.Atlases != null)
                {
                    foreach (var atlas_info in group.Atlases)
                    {
                        var atlas_data = AtlasBuilder.Build(atlas_info.Id, atlas_info.Path, atlas_info.Regions);

                        pak.Atlases.Add(atlas_data.Id, atlas_data);

                        pak.TotalResourcesCount++;

                        Console.WriteLine($"Added Atlas: {atlas_data.Id}");
                    }
                }

                if (group.TextFiles != null)
                {
                    foreach (var text_file_info in group.TextFiles)
                    {
                        var text_file_data = TextBuilder.Build(text_file_info.Id, text_file_info.Path);
                        pak.TextFiles.Add(text_file_info.Id, text_file_data);

                        pak.TotalResourcesCount++;

                        Console.WriteLine($"Added TextFile: {text_file_data.Id}");
                    }
                }

                results.Add(pak);
                Console.WriteLine($"Built PAK with {pak.TotalResourcesCount} resources.");
            }

            return(results);
        }
Esempio n. 19
0
 public void Build_ThrowsArgumentNullException_WhenSourceNull()
 {
     Assert.Throws <ArgumentNullException>(() => ImageBuilder.Build((Stream)null, Stream.Null, new Instructions()));
 }
Esempio n. 20
0
 public ImageJob Build(ImageJob job)
 {
     return(CurrentImageBuilder.Build(job));
 }
Esempio n. 21
0
 public void Build_ThrowsArgumentNullException_WhenDestinationPathNull()
 {
     Assert.Throws <ArgumentNullException>(() => ImageBuilder.Build(Stream.Null, (string)null, new Instructions()));
 }