//Change image format and size. static public byte[] ChangeImageFormat(Stream imgStrm, ChangeImageInfo chgImageInfo) { Stream changedStrm; byte[] pixelByteArray; bool noSizeFlg = (chgImageInfo.width == 0 && chgImageInfo.height == 0); Bitmap bmp; if (noSizeFlg) { //When the image size and format to be converted is not designated. bmp = new Bitmap(Image.FromStream(imgStrm)); } else { //When the image size and format to be converted is designated. bmp = new Bitmap(Image.FromStream(imgStrm), chgImageInfo.width, chgImageInfo.height); } changedStrm = new MemoryStream(); bmp.Save(changedStrm, selectImageFormat(chgImageInfo.format)); changedStrm.Position = 0; long strmLength = changedStrm.Length; pixelByteArray = new byte[strmLength]; changedStrm.Read(pixelByteArray, 0, (int)strmLength); return(pixelByteArray); }
//Return image file of specified size and format. public IHttpActionResult Get(string containerName) { string blobName = HttpContext.Current.Request.QueryString["name"]; string sWidth = HttpContext.Current.Request.QueryString["w"]; string sHeight = HttpContext.Current.Request.QueryString["h"]; string sformat = HttpContext.Current.Request.QueryString["f"]; string contentDsp = HttpContext.Current.Request.QueryString["cdsp"]; int outPrm = 0; int.TryParse(sHeight, out outPrm); int imgHeight = outPrm; int.TryParse(sWidth, out outPrm); int imgWidth = outPrm; if (sformat == null || sformat == "") { sformat = "jpg"; } ChangeImageInfo chgImageInfo = new ChangeImageInfo(sformat.ToLower(), imgHeight, imgWidth); CloudBlobContainer container = Helper.FileService.GetContainerInstance(containerName); CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName); var memoryStream = new MemoryStream(); blockBlob.DownloadToStream(memoryStream); byte[] buff = ImageService.ChangeImageFormat(memoryStream, chgImageInfo); if (contentDsp == "at") { string dlFileName = FileService.getFileNameWithoutEtn(blobName) + sformat; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + dlFileName + "\""); } HttpContext.Current.Response.ContentType = "image/" + sformat; HttpContext.Current.Response.Flush(); HttpContext.Current.Response.BinaryWrite(buff); HttpContext.Current.Response.End(); return(Ok()); }