Posts Tagged ‘c#’

I really enjoy image manipulation in code. Sure, resizing photos is fun in Photoshop, but there’s something viscerally enjoyable when you change images with your own code.

I’ve talked about image resizing libraries like ImageResizer before, but there’s certainly room for more than one. Today I want to showcase ImageProcessor, an open source “collection of lightweight libraries written in C# that allows you to manipulate images on-the-fly using .NET 4+.” ImageProcessor is available on GitHub.

ImageProcessor

ImageProcessor methods include; Resize, Rotate, Rounded Corners, Flip, Crop, Watermark, Filter, Saturation, Brightness, Contrast, Quality, Format, Vignette, Gaussian Blur, Gaussian Sharpen, and Transparency.

ImageProcessor has a notable number of configuration options for web apps, and a supporting ImageProcessor.Web package as well. It’s an impressive body of work.

I like this simple example of loading, resizing, and saving an image with their fluent API:

// Read a file and resize it.
byte[] photoBytes = File.ReadAllBytes(file);
int quality = 70;
ImageFormat format = ImageFormat.Jpeg;
Size size = new Size(150, 0)
 
using (MemoryStream inStream = new MemoryStream(photoBytes))
{
    using (MemoryStream outStream = new MemoryStream())
    {
        using (ImageFactory imageFactory = new ImageFactory())
        {
            // Load, resize, set the format and quality and save an image.
            imageFactory.Load(inStream)
                        .Resize(size)
                        .Format(format)
                        .Quality(quality)
                        .Save(outStream);
        }
 
        // Do something with the stream.
    }
}

You can easily chain functions with the API, like tinting and constraning:

imageFactory.Load(inStream)
            .Constrain(size)
            .Tint(Color.FromArgb(255, 106, 166, 204))
            .Format(format)
            .Save(outStream);

When you add ImageProcessor.Web it adds caching that takes pressure off your web servers. You can easily add HttpHandlers to watermark an image, for example, and cache the result.

This is a library that has as a lot of potential. Since it’s open source, I’m sure they’d appreciate help from the community! Personally, I think they could use more Unit Tests and more examples.

Head over to https://github.com/JimBobSquarePants/ImageProcessor and star this project! Get involved, file issues, and contribute!

RELATED LINKS

 

Source: http://www.hanselman.com/blog/CategoryView.aspx?category=NuGetPOW

Advertisements

LINQ Query Samples

Posted: September 15, 2013 in c#, linq
Tags: , ,

Complete examples of the linq queries… Look this:

Link: LINQ Query Samples

C# – StringFormat

Posted: February 1, 2012 in c#
Tags: , , ,

Exemplos:

http://blog.stevex.net/string-formatting-in-csharp/
http://www.programadorjr.com.br/?p=29

MSDN:

http://msdn.microsoft.com/en-us/library/0c899ak8.aspx