using System; using System.Collections.Generic; using System.Drawing.Drawing2D; using System.Drawing; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using API_NetFramework.Models; using System.Web.WebPages; using System.IO; namespace API_NetFramework.Controllers { public class ImageController : ApiController { string tokenfunction = "Image"; private System.Drawing.Image ResizeImage(System.Drawing.Image imgToResize, Size size) { // Get the image current width int sourceWidth = imgToResize.Width; // Get the image current height int sourceHeight = imgToResize.Height; float nPercent = 0; float nPercentW = 0; float nPercentH = 0; // Calculate width and height with new desired size nPercentW = ((float)size.Width / (float)sourceWidth); nPercentH = ((float)size.Height / (float)sourceHeight); nPercent = Math.Min(nPercentW, nPercentH); // New Width and Height int destWidth = (int)(sourceWidth * nPercent); int destHeight = (int)(sourceHeight * nPercent); Bitmap b = new Bitmap(destWidth, destHeight); Graphics g = Graphics.FromImage((System.Drawing.Image)b); g.InterpolationMode = InterpolationMode.HighQualityBicubic; // Draw image with new width and height g.DrawImage(imgToResize, 0, 0, destWidth, destHeight); g.Dispose(); return (System.Drawing.Image)b; } private System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size) { return (System.Drawing.Image)(new Bitmap(imgToResize, size)); } [HttpGet] [Route("API/GetImageAsBase64")] public IHttpActionResult GetImageAsBase64(int imageid, int ImageWidth = 0, int ImageHeight = 0) { string path = System.Configuration.ConfigurationManager.AppSettings["ImagePath"]; string filename = ""; int defaultheigt = 0; int defaultwidth = 0; APILogging.Log((HttpRequestMessage)Request, "Image-Bezug: " + imageid.ToString(), LogLevelType.Info); //if (SecuringWebApiUsingApiKey.Middleware.ApiKeyMiddleware.Authorized((HttpRequestMessage)Request, tokenfunction) == false) //{ // return Content(HttpStatusCode.Unauthorized, "Invalid Token or API-Key"); //} System.Data.DataSet ds = new System.Data.DataSet(); ds.ReadXml(path + "imageids.xml"); filename = ""; foreach (System.Data.DataRow row in ds.Tables[0].Rows) { if (Convert.ToInt32(row[0]) == imageid) { defaultheigt = Convert.ToInt32(row[3]); defaultwidth = Convert.ToInt32(row[2]); filename = path + row[1].ToString(); } } ds.Dispose(); if (filename == "") { APILogging.Log((HttpRequestMessage)Request, "Image-Bezug - nicht vorhanden: " + imageid.ToString(), LogLevelType.Info); return Content(HttpStatusCode.NotFound, "Image ID: " + imageid.ToString()+"nicht gefunden"); } System.Drawing.Image iimg = System.Drawing.Image.FromFile(filename); System.Drawing.Image imgnew = null; switch (ImageWidth) { case 0: ImageWidth =defaultwidth; ImageHeight = defaultheigt; Bitmap b = new Bitmap(iimg); imgnew = ResizeImage(b, new Size(ImageHeight, ImageWidth)); b = null; break; case -1: imgnew = iimg; break; default: Bitmap b1 = new Bitmap(iimg); imgnew = ResizeImage(b1, new Size(ImageHeight, ImageWidth)); b1 = null; break; } using (MemoryStream m = new MemoryStream()) { string newfilename = path + Guid.NewGuid().ToString() + ".jpg"; imgnew.Save(newfilename); imgnew = System.Drawing.Image.FromFile(newfilename); imgnew.Save(m, imgnew.RawFormat); byte[] imageBytes = m.ToArray(); imgnew.Dispose(); m.Dispose(); //yte[] imageArray = System.IO.File.ReadAllBytes(newfilename); // string base64ImageRepresentation = Convert.ToBase64String(imageArray); System.IO.File.Delete(newfilename); iimg.Dispose(); iimg = null; imgnew = null; //return Ok(base64ImageRepresentation); return Ok(Convert.ToBase64String(imageBytes)); } } } }