Processing images in C# easily using ImageProcessor

Have you ever wanted to manipulate images from your C# program but don’t know how or may be you started writing your custom code from scratch and you found that it’s a lot harder and painful than it looks. From now on don’t push yourself too hard most of the work is done for you in ImageProcessor

ImageProcessor is an open source library written in C# that allows developers to process any type of image on the fly from their application. It doesn’t matter if you’re writing a desktop or a web application, ImageProcessor will make processing image a simple and easy task to perform.

Now, don’t you even think that this library will allow you to do all the stuff Adobe Photoshop is capable of doing because that’s not the truth, ImageProcessor will only let you perform some of the basic image manipulation like:
Resize, Crop, Rotate, Watermark, change image quality, change the brightness … etc

Installing ImageProcessor

Because ImageProcessor is a nuget package, you can install it directly from your Package Manager Console by running this command

PM> Install-Package ImageProcessor

Simple example

                byte[] photoBytes = File.ReadAllBytes(imagePath); // change imagePath with a valid image path
                int quality = 70;
                var format = ImageFormat.Png; // we gonna convert a jpeg image to a png one
                var size = new Size(200, 200);

                using (var inStream = new MemoryStream(photoBytes))
                {
                    using (var outStream = new MemoryStream())
                    {
                        // Initialize the ImageFactory using the overload to preserve EXIF metadata.
                        using (var imageFactory = new ImageFactory(preserveExifData: true))
                        {
                            // Do your magic here
                            imageFactory.Load(inStream)
                                .Rotate(new RotateLayer(90))
                                .RoundedCorners(new RoundedCornerLayer(190,true,true,true,true))
                                .Watermark(new TextLayer()
                                           {
                                               DropShadow = true,
                                               Font = "Arial",
                                               Text = "Watermark",
                                               Style = FontStyle.Bold,
                                               TextColor = Color.DarkBlue
                                           })
                                .Resize(size)
                                .Format(format)
                                .Quality(quality)
                                .Save(outStream);
                        }
                    }
                }
Result

I’m going to test this code snippet on this image
initial-image

The result will be this:

result

 

 

What I really like about ImageProcessor is their Fluent API to run multiple actions on the selected image, this API is very handy and simple to use and understand for each and every developer depending on his level.
If you really like this library please feel free to contribute to its success: https://github.com/JimBobSquarePants/ImageProcessor

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s