Created
February 27, 2018 13:32
-
-
Save jjgriff93/409e7888b139952fcab24a8c5e87a45d to your computer and use it in GitHub Desktop.
C# HTTP Trigger Azure function code to get requested image from blob storage and optionally resize it based on request body parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections.Generic; | |
| using System.Net; | |
| using System.Net.Http; | |
| using Microsoft.Azure.WebJobs; | |
| using Microsoft.Azure.WebJobs.Extensions.Http; | |
| using Microsoft.Azure.WebJobs.Host; | |
| using Newtonsoft.Json; | |
| using Microsoft.WindowsAzure.Storage; | |
| using Microsoft.WindowsAzure.Storage.Blob; | |
| using System.Drawing; | |
| using System.Drawing.Drawing2D; | |
| using System.Drawing.Imaging; | |
| using System.IO; | |
| namespace ImageResizer | |
| { | |
| public static class GetCarImage | |
| { | |
| [FunctionName("GetCarImage")] | |
| public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "GetCarImage/{CarReg}/{ImageAngle}")]HttpRequestMessage request, string CarReg, string ImageAngle, TraceWriter log) | |
| { | |
| log.Info("C# HTTP trigger function processed a request."); | |
| //Set default image size to return if query is missing requested height and width parameters | |
| int requestedHeight = 500; | |
| int requestedWidth = 500; | |
| //Get the query parameters for requested image size | |
| string jsonContent = request.Content.ReadAsStringAsync().Result; | |
| Dictionary<string, int> jsonDictionary = JsonConvert.DeserializeObject<Dictionary<string, int>>(jsonContent); | |
| if (jsonDictionary.ContainsKey("height")) | |
| { | |
| requestedHeight = jsonDictionary["height"]; | |
| } | |
| if (jsonDictionary.ContainsKey("width")) | |
| { | |
| requestedWidth = jsonDictionary["width"]; | |
| } | |
| //Set up link to blob storage for stored car images (for production code, consider moving credentials to KeyVault to abstract credentials away from code level) | |
| string storageConnectionString = "DefaultEndpointsProtocol=https;" | |
| + "AccountName=griffsdemostorage" | |
| + ";AccountKey=<INSERT BLOB STORAGE ACCOUNT KEY HERE>" | |
| + ";EndpointSuffix=core.windows.net"; | |
| CloudStorageAccount blobAccount = CloudStorageAccount.Parse(storageConnectionString); | |
| CloudBlobClient blobClient = blobAccount.CreateCloudBlobClient(); | |
| // Fetch the Car Reg & Image File Name from the path parameters in the request URL and retrieve image | |
| if (ImageAngle != null && CarReg != null) | |
| { | |
| //Get reference to specific car's container from Car Reg (converts to lower case as container names must be lower case) | |
| CloudBlobContainer blobContainer = blobClient.GetContainerReference(CarReg.ToLower()); | |
| //Get reference to image block blob image from ImageFileName parameter the user passed in (images must be in jpg format in the blob service for this to work) | |
| CloudBlockBlob cloudBlockBlob = blobContainer.GetBlockBlobReference(ImageAngle +".jpg"); | |
| //Download the image | |
| MemoryStream streamIn = new MemoryStream(); | |
| cloudBlockBlob.DownloadToStream(streamIn); | |
| Image originalImage = Bitmap.FromStream(streamIn); | |
| //Pass the image and requested file size into our image resizing method to resize the image | |
| Image resizedImage = ResizeImage(originalImage, requestedWidth, requestedHeight); | |
| //Change image back to a stream for passing through to user as Http Content | |
| MemoryStream streamOut = new MemoryStream(); | |
| resizedImage.Save(streamOut, ImageFormat.Png); | |
| streamOut.Position = 0; | |
| //Create the Http response message with the resized image | |
| HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); | |
| response.Content = new StreamContent(streamOut); | |
| response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpg"); | |
| return response; | |
| } | |
| else | |
| { | |
| return request.CreateResponse(HttpStatusCode.BadRequest, "Please include a valid Car Reg and Image Angle (the view of the car you want - i.e. front/rear/left-side/right-side in your request URL, in the following format: API-URL/{CarReg}/{ImageAngle}"); | |
| } | |
| } | |
| //Method to resize images without distortion | |
| public static Bitmap ResizeImage(Image image, int width, int height) | |
| { | |
| var destRect = new Rectangle(0, 0, width, height); | |
| var destImage = new Bitmap(width, height); | |
| destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); | |
| using (var graphics = Graphics.FromImage(destImage)) | |
| { | |
| graphics.CompositingMode = CompositingMode.SourceCopy; | |
| graphics.CompositingQuality = CompositingQuality.HighQuality; | |
| graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; | |
| graphics.SmoothingMode = SmoothingMode.HighQuality; | |
| graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; | |
| using (var wrapMode = new ImageAttributes()) | |
| { | |
| wrapMode.SetWrapMode(WrapMode.TileFlipXY); | |
| graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); | |
| } | |
| } | |
| return destImage; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment