コード例 #1
0
        //public static BitmapFrame Resize(BitmapFrame photo, int width, int height, BitmapScalingMode scalingMode, Boolean keepAspectRatio)
        //{
        //    if (keepAspectRatio)
        //    {
        //        Double ratio = photo.Height / photo.Width;
        //        height = Convert.ToInt32(width * ratio);
        //    }

        //    DrawingGroup group = new DrawingGroup();
        //    RenderOptions.SetBitmapScalingMode(group, scalingMode);
        //    group.Children.Add(new ImageDrawing(photo, new Rect(0, 0, width, height)));
        //    DrawingVisual targetVisual = new DrawingVisual();
        //    DrawingContext targetContext = targetVisual.RenderOpen();
        //    targetContext.DrawDrawing(group);
        //    RenderTargetBitmap target = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
        //    targetContext.Close();
        //    target.Render(targetVisual);
        //    BitmapFrame targetFrame = BitmapFrame.Create(target, photo.Thumbnail, (BitmapMetadata)photo.Metadata, photo.ColorContexts);
        //    return targetFrame;
        //}

        public static Rotation Rotate(ref BitmapSource bitmapSource, ref TransformGroup transformGroup)
        {
            Rotation rotation = StaticFunctions.GetRotation(bitmapSource);
            if (rotation != Rotation.Rotate0)
            {
                RotateTransform rotateTransform = new RotateTransform();
                        
                switch (rotation)
                {
                    case Rotation.Rotate90:
                        rotateTransform.Angle = 90;
                        break;
                    case Rotation.Rotate180:
                        rotateTransform.Angle = 180;
                        break;
                    case Rotation.Rotate270:
                        rotateTransform.Angle = 270;
                        break;
                }

                transformGroup.Children.Add(rotateTransform);
            }

            return rotation;
        }
コード例 #2
0
        public static SaveImageThumbnailResult SaveImageThumbnail(idCatalogItem catalogItem, ref IDImagerDB db, ref IDImagerDB dbThumbs,
            List<String> types, Boolean keepAspectRatio, Boolean setGenericVideoThumbnailOnError)
        {
            SaveImageThumbnailResult result = new SaveImageThumbnailResult();
            Stream imageStream = null;
            String filePath = null;

            try
            {          
                filePath = GetImageFilePath(catalogItem);
                BitmapFrame bitmapFrame;

                if (ImageFileExtensions.Contains(catalogItem.idFileType))
                {
                    imageStream = GetImageFileStream(filePath);
                    bitmapFrame = GetBitmapFrameFromImageStream(imageStream, catalogItem.idFileType);
                }
                else if (VideoFileExtensions.Contains(catalogItem.idFileType))
                {
                    try
                    {
                        bitmapFrame = BitmapFrame.Create((BitmapSource)GenerateVideoThumbnail(filePath, new TimeSpan(0, 0, 0)));
                    }
                    catch (Exception ex)
                    {
                        if (setGenericVideoThumbnailOnError)
                        {
                            result.Exceptions.Add(new Exception(String.Format("Video thumbnail generation for imageGUID {0} file {1} failed. Generic thumbnails has been set.", catalogItem.GUID, filePath), ex));
                            
                            Assembly assembly = Assembly.GetExecutingAssembly();
                            Stream genericVideoThumbnailStream = assembly.GetManifestResourceStream(@"IDBrowserServiceCode.Images.image_ph2.png");
                            bitmapFrame = BitmapFrame.Create(genericVideoThumbnailStream);
                        }
                        else
                        {
                            result.Exceptions.Add(new Exception(String.Format("Video thumbnail generation for imageGUID {0} file {1} failed.", catalogItem.GUID, filePath), ex));
                            return result;
                        }
                    }
                }
                else
                {
                    throw new Exception(String.Format("File type {0} not supported", catalogItem.idFileType));
                }           

                foreach (String type in types)
                {
                    int imageWidth;
                    int imageHeight;

                    if (type.Equals("T"))
                    {
                        imageWidth = 160;
                        imageHeight = 120;
                    }
                    else
                    {
                        imageWidth = Int32.Parse(ConfigurationManager.AppSettings["MThumbmailWidth"]);
                        imageHeight = Int32.Parse(ConfigurationManager.AppSettings["MThumbnailHeight"]);
                    }

                    XDocument recipeXDocument = null;
                    if (type.Equals("T") || type.Equals("R"))
                    {
                        if (catalogItem.idHasRecipe > 0)
                            recipeXDocument = GetRecipeXDocument(db, catalogItem);
                    }
                                        
                    TransformGroup transformGroup = new TransformGroup();
                    
                    if (bitmapFrame.PixelWidth > imageWidth && bitmapFrame.PixelHeight > imageHeight)
                    {
                        double scaleX;
                        double scaleY;
                        
                        foreach (ScaleTransform existingScaleTransform in transformGroup.Children.OfType<ScaleTransform>().ToList())
                            transformGroup.Children.Remove(existingScaleTransform);

                        if (bitmapFrame.PixelWidth > bitmapFrame.PixelHeight)
                        {
                            scaleX = (double)imageWidth / (double)bitmapFrame.PixelWidth;
                            scaleY = (double)imageHeight / (double)bitmapFrame.PixelHeight;
                        }
                        else
                        {
                            scaleX = (double)imageHeight / (double)bitmapFrame.PixelHeight;
                            scaleY = (double)imageWidth / (double)bitmapFrame.PixelWidth;
                        }

                        ScaleTransform scaleTransform = new ScaleTransform(scaleX, scaleY, 0, 0);
                        transformGroup.Children.Add(scaleTransform);
                    }

                    Rotation rotation = StaticFunctions.GetRotation(bitmapFrame);
                    if (rotation != Rotation.Rotate0)
                    {
                        RotateTransform rotateTransform = new RotateTransform();

                        switch (rotation)
                        {
                            case Rotation.Rotate90:
                                rotateTransform.Angle = 90;
                                break;
                            case Rotation.Rotate180:
                                rotateTransform.Angle = 180;
                                break;
                            case Rotation.Rotate270:
                                rotateTransform.Angle = 270;
                                break;
                        }

                        transformGroup.Children.Add(rotateTransform);
                    }

                    Stream resizedImageStream = imageStream;
                    BitmapSource bitmapSource = bitmapFrame;

                    if (Recipe.ApplyXmpRecipe(recipeXDocument, ref bitmapSource, transformGroup))
                    {
                        BitmapFrame transformedBitmapFrame = BitmapFrame.Create(bitmapSource);

                        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                        encoder.Frames.Add(transformedBitmapFrame);
                        resizedImageStream = new System.IO.MemoryStream();
                        encoder.Save(resizedImageStream);
                        resizedImageStream.Position = 0;
                    }

                    lock (dbThumbs)
                    {
                        idThumbs newThumb = new idThumbs();
                        newThumb.GUID = Guid.NewGuid().ToString().ToUpper();
                        newThumb.ImageGUID = catalogItem.GUID;
                        newThumb.idThumb = StreamToByteArray(resizedImageStream);
                        newThumb.idType = type;

                        dbThumbs.idThumbs.Add(newThumb);
                    }

                    result.ImageStreams.Add(resizedImageStream);
                }

                if (imageStream != null) { imageStream.Close(); }
                dbThumbs.SaveChanges(); 
            }
            catch (Exception ex)
            {
                if (imageStream != null) { imageStream.Close(); }
                result.Exceptions.Add(new Exception(String.Format("Error generating thumbnail for imageGUID {0} file {1}", catalogItem.GUID, filePath), ex));
            }

            return result;
        }