private Stream GetImageStream(string imageGuid, string width = null, string height = null) { idCatalogItem catalogItem = null; Boolean keepAspectRatio = Boolean.Parse(ConfigurationManager.AppSettings["KeepAspectRatio"]); catalogItem = db.idCatalogItem.Include("idFilePath").Single(x => x.GUID == imageGuid); if (catalogItem == null) { throw new Exception("CatalogItem not found"); } Stream imageStream = StaticFunctions.GetImageFileStream(StaticFunctions.GetImageFilePath(catalogItem)); BitmapSource bitmapSource = StaticFunctions.GetBitmapFrameFromImageStream(imageStream, catalogItem.idFileType); System.Xml.Linq.XDocument recipeXDocument = null; try { recipeXDocument = StaticFunctions.GetRecipeXDocument(db, catalogItem); } catch (Exception ex) { log.Error(String.Format("Error in 'GetImageStream' when applying recipe on imageGuid {0}: {1}", catalogItem.GUID, ex.ToString())); } TransformGroup transformGroup = new TransformGroup(); if (width != null && height != null) { StaticFunctions.Resize(ref bitmapSource, ref transformGroup, int.Parse(width), int.Parse(height)); } Rotation rotation = StaticFunctions.Rotate(ref bitmapSource, ref transformGroup); if (Recipe.ApplyXmpRecipe(recipeXDocument, ref bitmapSource, transformGroup)) { BitmapFrame transformedBitmapFrame = BitmapFrame.Create(bitmapSource); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(transformedBitmapFrame); imageStream = new System.IO.MemoryStream(); encoder.Save(imageStream); } imageStream.Position = 0; return(imageStream); }
public Stream GetFile(string imageGuid) { Stream fileStream = null; try { log.Info(String.Format("Client {0}:{1} called GetFile with imageGuid: {2}", clientEndpoint.Address, clientEndpoint.Port, imageGuid)); idCatalogItem catalogItem; using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, readUncommittedTransactionOptions)) { catalogItem = db.idCatalogItem.Include("idFilePath").Single(x => x.GUID == imageGuid); scope.Complete(); } if (catalogItem == null) { throw new Exception("CatalogItem not found"); } fileStream = StaticFunctions.GetImageFileStream(StaticFunctions.GetImageFilePath(catalogItem));; if (IsRequestRest()) { WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream"; WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Size", fileStream.Length.ToString()); } return(fileStream); } catch (Exception ex) { if (fileStream != null) { fileStream.Close(); } log.Error(ex.ToString()); throw ex; } }