In this article we will learn how we can compress and resize image in Web API using postman and how we can save the compressed image. Here are the steps:
Step 1
In the first step, create a simple web API project. Go to File, then New and Project. Choose ASP.NET MVC 4 Web Application from the list, then provide the application name as you wish and click the OK button, then select Web API. Set the path to the location input where you want to create the application.
Let's see the Solution Explorer:
In our solution explorer we have an Uploadcontroller and ImgFloder.
Uploadcontroller contains the Post method. let's see the code for compressing and re-sizing the image.
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Web;
- using System.Web.Http;
-
- namespace image_uplode.Controllers
- {
- public class UplodeController : ApiController
- {
-
- [HttpPost]
- public HttpResponseMessage Post()
- {
- HttpResponseMessage response = new HttpResponseMessage();
- var httpRequest = HttpContext.Current.Request;
- if (httpRequest.Files.Count > 0)
- {
- var docfiles = new List<string>();
- foreach (string file in httpRequest.Files)
- {
- var postedFile = httpRequest.Files[file];
- var filePath1 = HttpContext.Current.Server.MapPath("~/ImgFolder/" + postedFile.FileName);
-
- Stream strm = postedFile.InputStream;
-
- Compressimage(strm, filePath1, postedFile.FileName);
-
- }
- response = Request.CreateResponse(HttpStatusCode.Created, docfiles);
- }
- else
- {
- response = Request.CreateResponse(HttpStatusCode.BadRequest);
- }
- return response;
- }
-
-
- public static void Compressimage(Stream sourcePath, string targetPath, String filename)
- {
-
-
- try
- {
- using (var image = Image.FromStream(sourcePath))
- {
- float maxHeight = 900.0f;
- float maxWidth = 900.0f;
- int newWidth;
- int newHeight;
- string extension;
- Bitmap originalBMP = new Bitmap(sourcePath);
- int originalWidth = originalBMP.Width;
- int originalHeight = originalBMP.Height;
-
- if (originalWidth > maxWidth || originalHeight > maxHeight)
- {
-
-
- float ratioX = (float)maxWidth / (float)originalWidth;
- float ratioY = (float)maxHeight / (float)originalHeight;
- float ratio = Math.Min(ratioX, ratioY);
- newWidth = (int)(originalWidth * ratio);
- newHeight = (int)(originalHeight * ratio);
- }
- else
- {
- newWidth = (int)originalWidth;
- newHeight = (int)originalHeight;
-
- }
- Bitmap bitMAP1 = new Bitmap(originalBMP, newWidth, newHeight);
- Graphics imgGraph = Graphics.FromImage(bitMAP1);
- extension = Path.GetExtension(targetPath);
- if (extension == ".png" || extension == ".gif")
- {
- imgGraph.SmoothingMode = SmoothingMode.AntiAlias;
- imgGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
- imgGraph.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
-
- bitMAP1.Save(targetPath, image.RawFormat);
-
- bitMAP1.Dispose();
- imgGraph.Dispose();
- originalBMP.Dispose();
- }
- else if (extension == ".jpg")
- {
-
- imgGraph.SmoothingMode = SmoothingMode.AntiAlias;
- imgGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
- imgGraph.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
- ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
- Encoder myEncoder = Encoder.Quality;
- EncoderParameters myEncoderParameters = new EncoderParameters(1);
- EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
- myEncoderParameters.Param[0] = myEncoderParameter;
- bitMAP1.Save(targetPath, jpgEncoder, myEncoderParameters);
-
- bitMAP1.Dispose();
- imgGraph.Dispose();
- originalBMP.Dispose();
-
- }
-
-
- }
-
- }
- catch (Exception)
- {
- throw;
-
- }
- }
-
-
- public static ImageCodecInfo GetEncoder(ImageFormat format)
- {
-
- ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
-
- foreach (ImageCodecInfo codec in codecs)
- {
- if (codec.FormatID == format.Guid)
- {
- return codec;
- }
- }
- return null;
- }
-
- }
- }
After that run your application.
Now call the post method, here I am using postman for post call, let's see what we need for post call. It is shown in the following screenshot:
In the above snapshot I clicked on the body select form-data and in key column of the form-data wrote file and choose file from dropdown list.
Also add one more row
size : original, after that choose the file from directory.
After that click
Headers and add one row
Accept: multipart/form-data.
Summary
In this article we learned about compressing images and re-sizing them in web API using postman.