C# || How To Resize & Rotate Image, Convert Image To Byte Array, Change Image Format, & Fix Image Orientation Using C#

The following is a module with functions which demonstrates how to resize an image, rotate an image to a specific angle, convert an image to a byte array, change an image format, and fix an image orientation Using C#.

Contents

1. Overview
2. Resize & Rotate - Image
3. Resize & Rotate - Byte Array
4. Resize & Rotate - Memory Stream
5. Convert Image To Byte Array
6. Change Image Format
7. Fix Image Orientation
8. Utils Namespace
9. More Examples


1. Overview

To use the functions in this module, make sure you have a reference to ‘System.Drawing‘ in your project.

One way to do this is, in your Solution Explorer (where all the files are shown with your project), right click the ‘References‘ folder, click ‘Add References‘, then type ‘System.Drawing‘ in the search box, and add a reference to System.Drawing in the results Tab.

Note: Don’t forget to include the ‘Utils Namespace‘ before running the examples!


2. Resize & Rotate – Image

The example below demonstrates the use of ‘Utils.Image.Extensions.Resize‘ and ‘Utils.Image.Extensions.Rotate‘ to resize and rotate an image object.

Resizing and rotating an image returns a newly created image object.


3. Resize & Rotate – Byte Array

The example below demonstrates the use of ‘Utils.Image.Extensions.Resize‘ and ‘Utils.Image.Extensions.Rotate‘ to resize and rotate a byte array.

Resizing and rotating an image returns a newly created image object.


4. Resize & Rotate – Memory Stream

The example below demonstrates the use of ‘Utils.Image.Resize‘ and ‘Utils.Image.Rotate‘ to resize and rotate a memory stream.

Resizing and rotating an image returns a newly created image object.


5. Convert Image To Byte Array

The example below demonstrates the use of ‘Utils.Image.Extensions.ToArray‘ to convert an image object to a byte array.

The optional function parameter allows you to specify the image format.


6. Change Image Format

The example below demonstrates the use of ‘Utils.Image.Extensions.ConvertFormat‘ to convert the image object raw format to another format.

Converting an images format returns a newly created image object.


7. Fix Image Orientation

The example below demonstrates the use of ‘Utils.Image.Extensions.FixOrientation‘ to update the image object to reflect its Exif orientation.

When you view an image in a Photo Viewer, it automatically corrects image orientation if it has Exif orientation data.

Utils.Image.Extensions.FixOrientation‘ makes it so the loaded image object reflects the correct orientation when rendered according to its Exif orientation data.


8. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


9. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Was this article helpful?
👍 YesNo

Leave a Reply