# Approaches Fix image resolution
Too often, minimizing images programmatically will affect their resolution.
When it comes to manipulating pixels there are actually two methods, resizing and resampling.
**Resizing** is the most basic as it simply enlarges or decreases the size of the pixels to whatever scale is desired.
**Resampling** is a tool that changes the number of pixels in the image itself. This is the most appropriate tool for resizing images for the web since page speed is so important, especially if you are using original photographs straight from your camera.
In this method we asking the program to analyze the image to decide which pixels to throw away or which to add. So if we are looking to reduce image size without losing quality your best tool is resampling.
Here are ways to reduce the size of images by using (resampling) without affecting their resolution.
## Using Built-in GDI+
The `System.Drawing` namespace provides access to GDI+ basic graphics functionality. The following code example demonstrates how to construct a new Bitmap from a file, using the GetPixel and SetPixel methods to recolor the image. It also uses the PixelFormat, Width, and Height properties.
```c#=
using (var srcImage = Image.FromFile(imageFile))
{
var newWidth = (int)(srcImage.Width * scaleFactor);
var newHeight = (int)(srcImage.Height * scaleFactor);
// create new image with new width and height (limited pixels)
using (var newImage = new Bitmap(newWidth, newHeight))
using (var graphics = Graphics.FromImage(newImage))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
newImage.Save(outputFile);
}
}
```
It takes minute to write a function without any library to resampling an image, very easy right?
But, there are some **disadvantages**:
1- **Memory**: The main problem in this approatch that it takes big size of the memory to create or resizing images.
2- **CPU**: Another side-effect of the fact that System.Drawing is more graphics-focused than imaging-focused is that DrawImage() is quite inefficient CPU-wise.
## Using Nuget packages
Many packages used to not for minimize the images only, but to (Rotate, Rounded Corners, Flip, Crop, Watermark, Filter, etc..) as ImageResizer, SixLabors and ImageProcessorm, for example:
### ImageResizer
From their github repo description "ImageResizer is the best image server for ASP.NET. It can be dropped into existing apps and works alongside your CMS or existing platform to eliminate manual image tasks, enable fast and adaptive websites".
From the description it turns out that the main feature of this library is to display images on web pages faster using query string to determine the images' width and height. Its perfect to use it in CMS platforms.
``` html =
<div class="container">
<img src="img.jpg?height=150" />
</div>
```
We use this library in the GRAMMS, which has a library size exceeding 700 kilobytes, as we don't use the basic feature of the library but rather use a small feature, which is to resampling images when they are uploaded into the database.
This lib using System.Drwaing, as mention before it consuming the memory if we upload large size images.
### SkiaSharp
SkiaSharp is the .NET wrapper for Google's Skia cross-platform 2D graphics library, that is maintained by the Xamarin team. SkiaSharp is now compatible with .NET Core, and is extremely fast. As it relies on native libraries, and it has alot of feature for image processing.
### MagicScaler
Using Windows Image Components (WIC), it implements best-of-breed algorithms, linear light processing, and sharpening for the best image resizing quality available. Speed and efficiency are unmatched by anything else on the .NET platform and the important thing that it is focuses exclusively on image resizing.
The only thing not good in this lib that it is not crose platform lib, means if we need to upgrade our system to be .net core on linux, it will not migrated and we should use another lib.
## Conclution
According to the requirements of GRAMMS, we focus on minimizing images size without loss quality when uploading them, taking into consideration the method of using memory and CPU to read and save images.
> 
In a previous research, it was found that MagicScaler succeeded in all tests and found that it is the powerfull and fastest in reading, writing and resampling images, SkiaSharp comes as a second place.
I recommedned to use MagicScaler if we decide to not upgrade GRAMMS to .net core, otherwise SkiaSharp will be the perfect choice becouse it cross platform lib.
## References
https://github.com/saucecontrol/PhotoSauce
https://github.com/dotnet/runtime/issues/21980
https://photosauce.net/blog/post/5-reasons-you-should-stop-using-systemdrawing-from-aspnet
https://devblogs.microsoft.com/dotnet/net-core-image-processing/
https://www.hanselman.com/blog/HowDoYouUseSystemDrawingInNETCore.aspx