public IEnumerable <Product> FilterBySize(IEnumerable <Product> products, Enums.Size size) { foreach (var product in products) { if (product.Size == size) { yield return(product); } } }
public Product(string name, Enums.Color color, Enums.Size size) { if (name == null) { throw new ArgumentNullException(paramName: nameof(name)); } Name = name; Color = color; Size = size; }
public Animal(string name, Enums.Size size, Enums.Diet diet) { Name = name; Size = size; Diet = diet; }
public SizeSpecification(Enums.Size size) { this.size = size; }
public static IEnumerable <Product> FilterBySizeAndColor(IEnumerable <Product> products, Enums.Size size, Enums.Color color) { foreach (var p in products) { if (p.Size == size && p.Color == color) { yield return(p); } } } // state space explosion
public Product(string name, Enums.Color color, Enums.Size size) { Name = name; Color = color; Size = size; }
public void AddAnimal(string name, Enums.Size size, Enums.Diet diet) { animals.Add(new Animal(name, size, diet)); }
public Size(string name, Enums.Size sizeCode) { Name = name; SizeCode = sizeCode; }
#pragma warning disable CS0162 // Unreachable code detected public void Execute(string[] args) { // If you set a number to zero (0), it will resize on the other specified axis. var width = 200; var height = 0; // Enums.Size.Both - for both up and down. // Enums.Size.Up - only upsize. // Enums.Size.Down - only downsize. // Enums.Size.Force - force size, that is, break aspect ratio. const Enums.Size size = Enums.Size.Both; // Just for example. var buffer = File.ReadAllBytes(Filename); // Find the name of the load operation vips will use to load a buffer // so that we can work out what options to pass to NewFromBuffer(). var loader = Image.FindLoadBuffer(buffer); if (loader == null) { // No known loader is found, stop further processing. throw new Exception("Invalid or unsupported image format. Is it a valid image?"); } var loadOptions = new VOption { { "access", Enums.Access.Sequential }, { "fail_on", FailOn } }; var stringOptions = ""; if (LoaderSupportPage(loader)) { // -1 means "until the end of the document", handy for animated images. loadOptions.Add("n", -1); stringOptions = "[n=-1]"; } int inputWidth; int inputHeight; int pageHeight; bool isCmyk; bool isLabs; bool embeddedProfile; Image image = null; try { image = (Image)Operation.Call(loader, loadOptions, buffer); // Or: // image = Image.NewFromBuffer(buffer, kwargs: loadOptions); // (but the loader is already found, so the above will be a little faster). inputWidth = image.Width; inputHeight = image.Height; // Use 64-bit unsigned type, to handle PNG decompression bombs. if ((ulong)(inputWidth * inputHeight) > MaxImageSize) { throw new Exception( "Image is too large for processing. Width x height should be less than 71 megapixels."); } pageHeight = image.PageHeight; isCmyk = image.Interpretation == Enums.Interpretation.Cmyk; isLabs = image.Interpretation == Enums.Interpretation.Labs; embeddedProfile = image.Contains(VipsMetaIccName); } catch (VipsException e) { throw new Exception("Image has a corrupt header.", e); } finally { // We're done with the image; dispose early image?.Dispose(); } string importProfile = null; string exportProfile = null; Enums.Intent?intent = null; // Ensure we're using a device-independent color space if ((embeddedProfile || isCmyk) && !isLabs) { // Embedded profile; fallback in case the profile embedded in the image // is broken. No embedded profile; import using default CMYK profile. importProfile = isCmyk ? "cmyk" : "srgb"; // Convert to sRGB using embedded or import profile. exportProfile = "srgb"; // Use "perceptual" intent to better match imagemagick. intent = Enums.Intent.Perceptual; } // Scaling calculations var thumbnailWidth = width; var thumbnailHeight = height; if (width > 0 && height > 0) // Fixed width and height { var xFactor = (double)inputWidth / width; var yFactor = (double)pageHeight / height; if (xFactor > yFactor) // Or: if (xFactor < yFactor) { thumbnailHeight = (int)Math.Round(pageHeight / xFactor); } else { thumbnailWidth = (int)Math.Round(inputWidth / yFactor); } } else if (width > 0) // Fixed width { if (size == Enums.Size.Force) { thumbnailHeight = pageHeight; height = pageHeight; } else { // Auto height var yFactor = (double)inputWidth / width; height = (int)Math.Round(pageHeight / yFactor); // Height is missing, replace with a huuuge value to prevent // reduction or enlargement in that axis thumbnailHeight = VipsMaxCoord; } } else if (height > 0) // Fixed height { if (size == Enums.Size.Force) { thumbnailWidth = inputWidth; width = inputWidth; } else { // Auto width var xFactor = (double)pageHeight / height; width = (int)Math.Round(inputWidth / xFactor); // Width is missing, replace with a huuuge value to prevent // reduction or enlargement in that axis thumbnailWidth = VipsMaxCoord; } } else // Identity transform { thumbnailWidth = inputWidth; width = inputWidth; thumbnailHeight = pageHeight; height = pageHeight; } // Note: don't use "image.ThumbnailImage". Otherwise, none of the very fast // shrink-on-load tricks are possible. This can make thumbnailing of large // images extremely slow. using var thumb = Image.ThumbnailBuffer(buffer, thumbnailWidth, stringOptions, thumbnailHeight, size, false, importProfile: importProfile, exportProfile: exportProfile, intent: intent); thumb.WriteToFile("thumbnail.webp", new VOption { { "strip", true } }); // Or: /*buffer = thumb.WriteToBuffer(".webp", new VOption * { * {"strip", true} * });*/ Console.WriteLine("See thumbnail.webp"); }