Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        //public static Stream ApplyXmpRecipe(XDocument xdocument, Stream imageStream, String fileType)
        //{
        //    BitmapFrame bitmapFrame = StaticFunctions.getBitmapFrameFromImageStream(imageStream, fileType);
        //    TransformGroup transformGroup = GetRecipeTransformGroup(xdocument, bitmapFrame);

        //    if (transformGroup.Children.Count > 0)
        //    {
        //        TransformedBitmap tb = new TransformedBitmap();
        //        tb.BeginInit();
        //        tb.Source = bitmapFrame;
        //        tb.Transform = transformGroup;
        //        tb.EndInit();

        //        bitmapFrame = BitmapFrame.Create(tb);

        //        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
        //        encoder.Frames.Add(bitmapFrame);
        //        imageStream = new System.IO.MemoryStream();
        //        encoder.Save(imageStream);
        //        imageStream.Position = 0;
        //    }

        //    return imageStream;
        //}

        public static Stream ApplyXmpRecipe(XDocument xdocument, Stream imageStream, String fileType)
        {
            BitmapSource bitmapSource = StaticFunctions.GetBitmapFrameFromImageStream(imageStream, fileType);

            if (ApplyXmpRecipe(xdocument, ref bitmapSource, new 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);
        }
Ejemplo n.º 3
0
        private Stream GetImageThumbnailStream(string type, string imageGuid, string width = null, string height = null)
        {
            if (type != "T" && type != "R" && type != "M")
            {
                throw new Exception("Unsupported image type");
            }

            idCatalogItem catalogItem = null;
            Stream        imageStream = null;

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, readUncommittedTransactionOptions))
            {
                catalogItem = db.idCatalogItem.Include("idFilePath").Single(x => x.GUID == imageGuid);

                if (catalogItem == null)
                {
                    throw new Exception("CatalogItem not found");
                }

                if (type == "R" && catalogItem.idHasRecipe == 0)
                {
                    throw new Exception("Image has no recipe");
                }

                //Check if CatalogItem has a recipe, if yes try to get the recipe image
                if (type == "M" && catalogItem.idHasRecipe > 0)
                {
                    type = "R";
                }

                Boolean keepAspectRatio = Boolean.Parse(ConfigurationManager.AppSettings["KeepAspectRatio"]);
                Boolean setGenericVideoThumbnailOnError = Boolean.Parse(ConfigurationManager.AppSettings["SetGenericVideoThumbnailOnError"]);

                idThumbs thumb = null;
                lock (dbThumbs)
                {
                    //Searching with FirstOrDefault because PhotoSupreme sometimes stores the Thumbnail twice
                    thumb = dbThumbs.idThumbs.FirstOrDefault(x => x.ImageGUID == imageGuid && x.idType == type);

                    //If recipe image is not found, return the M image,
                    //because the programm cannot yet generate the recipe image
                    if (thumb == null && type == "R")
                    {
                        type = "M";
                        //Searching with FirstOrDefault because PhotoSupreme sometimes stores the Thumbnail twice
                        thumb = dbThumbs.idThumbs.FirstOrDefault(x => x.ImageGUID == imageGuid && x.idType == type);
                    }
                }

                if (thumb == null && Boolean.Parse(ConfigurationManager.AppSettings["CreateThumbnails"]))
                {
                    SaveImageThumbnailResult result = StaticFunctions.SaveImageThumbnail(catalogItem, ref db, ref dbThumbs, new List <String>()
                    {
                        type
                    }, keepAspectRatio, setGenericVideoThumbnailOnError);

                    foreach (Exception ex in result.Exceptions)
                    {
                        log.Error(ex.ToString());
                    }

                    if (result.ImageStreams.Count > 0)
                    {
                        imageStream = result.ImageStreams.First();

                        if (IsRequestRest())
                        {
                            WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
                        }

                        log.Info(String.Format("Client {0}:{1} called GetImageThumbnail with type: {2} imageGuid: {3} (returned resizedImageStream)",
                                               clientEndpoint.Address, clientEndpoint.Port, type, imageGuid));
                    }
                    else
                    {
                        log.Info(String.Format("Client {0}:{1} called GetImageThumbnail with type: {2} imageGuid: {3} (returned null)",
                                               clientEndpoint.Address, clientEndpoint.Port, type, imageGuid));
                    }
                }
                else
                {
                    imageStream = new MemoryStream(thumb.idThumb);

                    if (IsRequestRest())
                    {
                        WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
                    }

                    log.Info(String.Format("Client {0}:{1} called GetImageThumbnail with type: {2} imageGuid: {3} (returned imageStream)",
                                           clientEndpoint.Address, clientEndpoint.Port, type, imageGuid));
                }

                scope.Complete();
            }

            if (width != null && height != null)
            {
                TransformGroup transformGroup = new TransformGroup();
                BitmapSource   bitmapSource   = StaticFunctions.GetBitmapFrameFromImageStream(imageStream, "JPG");
                StaticFunctions.Resize(ref bitmapSource, ref transformGroup, int.Parse(width), int.Parse(height));

                if (transformGroup != null && transformGroup.Children.Count > 0)
                {
                    TransformedBitmap tb = new TransformedBitmap();
                    tb.BeginInit();
                    tb.Source    = bitmapSource;
                    tb.Transform = transformGroup;
                    tb.EndInit();

                    bitmapSource = tb;

                    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);
        }