private byte[] GetThumbnail(Stream source, GraphicInformation information)
        {
            if (information.Type != GraphicFileType.Jpeg)
            {
                return(null);
            }

            // 画像オブジェクトの作成
            using (var orig = System.Drawing.Image.FromStream(source, false, false))
            {
                int[] pils  = orig.PropertyIdList;
                int   index = Array.IndexOf(pils, 0x501b); // サムネイル・データ

                if (index == -1)
                {
                    return(null);
                }
                else
                {
                    // サムネイル・データの取得
                    var    pi       = orig.PropertyItems[index];
                    byte[] jpgBytes = pi.Value;

                    return(jpgBytes);
                }
            }
        }
예제 #2
0
        public void GraphicInformationTest()
        {
            var path        = @"..\..\..\ShibugakiViewer\Assets\Icons\mikan_rect64.png";
            var information = new GraphicInformation(path);

            Assert.AreEqual(GraphicFileType.Png, information.Type);
            Assert.AreEqual(new System.Drawing.Size(64, 64), information.GraphicSize);
        }
예제 #3
0
        private static string getGroupAttributes(GroupObject groupObject, GraphicInformation graphicInfo)
        {
            string[] groupAttribute = new string[] { "none=", "PenColor=", "PenWidth=", "Layer=", "Tile=", "Type=" };                   // check public enum GroupOptions
            string   groupVal1      = string.Format(" {0}\"{1}\"", groupAttribute[(int)graphicInfo.GroupOption], groupObject.key);
            string   groupVal2      = string.Format(" ToolNr=\"{0}\"", groupObject.toolNr);
            string   groupVal3      = string.Format(" ToolName=\"{0}\"", groupObject.toolName);
            string   groupVal4      = string.Format(" PathLength=\"{0:0.0}\"", groupObject.pathLength);
            string   groupVal5      = string.Format(" PathArea=\"{0:0.0}\"", groupObject.pathArea);

            return(string.Format("{0}{1}{2}{3}{4}", groupVal1, groupVal2, groupVal3, groupVal4, groupVal5));
        }
        private void SetAnimation(Record record)
        {
            var path = record?.FullPath;

            var pathChanged = (this.previousPath == null) ? path != null
                : this.previousPath.Equals(path);

            this.previousPath = path;


            this.StopGifAnimation();

            //Gifアニメのストリームを設定
            if (this.IsGifAnimationEnabled && pathChanged && path.HasText())
            {
                Stream stream = null;
                try
                {
                    stream = File.OpenRead(path);

                    var information = new GraphicInformation(stream);

                    if (information.Type == GraphicFileType.Gif)
                    {
                        stream.Position = information.BlankHeaderLength;
                        AnimationBehavior.SetSourceStream(this.AssociatedObject, stream);
                    }
                    else
                    {
                        stream.Dispose();
                    }
                    //AnimationBehavior.SetSourceUri(element, new Uri(path));
                }
                catch
                {
                    stream?.Dispose();
                }
            }
            //else
            //{
            //    //AnimationBehavior.SetSourceUri(element, null);
            //    //element.Source = source;
            //}
        }
        private void SetImage
            (BitmapImage image, Stream stream, GraphicInformation information, bool asThumbnail)
        {
            stream.Position = 0;


            if (asThumbnail && information.Type == GraphicFileType.Jpeg)
            {
                try
                {
                    // Get JPEG thumbnail from header
                    var frame = BitmapFrame.Create
                                    (stream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.OnDemand);
                    var source = frame.Thumbnail;

                    if (source == null)
                    {
                        stream.Position = 0;
                        this.SetStreamSourceToImage(image, stream);
                        return;
                    }

                    using (var ms = new WrappingStream(new MemoryStream()))
                    {
                        var encoder = new JpegBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(source));
                        encoder.Save(ms);
                        ms.Seek(0, SeekOrigin.Begin);

                        this.SetStreamSourceToImage(image, ms);
                        return;
                    }
                }
                catch
                {
                }
                stream.Position = 0;
            }

            this.SetStreamSourceToImage(image, stream);
        }
#pragma warning restore 1998


        private void SetSourceToImage
            (BitmapImage image, Stream stream, GraphicInformation information, bool asThumbnail)
        {
            if (information.BlankHeaderLength == 0)
            {
                this.SetImage(image, stream, information, asThumbnail);
            }
            else
            {
                using (var ms = new WrappingStream(new MemoryStream((int)
                                                                    (stream.Length - information.BlankHeaderLength))))
                {
                    stream.Position = information.BlankHeaderLength;

                    ms.Position = 0;
                    stream.CopyTo(ms);

                    this.SetImage(image, ms, information, asThumbnail);
                }
            }
        }
예제 #7
0
        /// <summary>
        /// 画像ファイルの情報を取得
        /// </summary>
        /// <param name="fullPath"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public ImageFileInformation GetImage(string fullPath, PropertiesLevel level)
        {
            ImageFileInformation image = null;
            var hasDecoration          = true;

            try
            {
                var name = System.IO.Path.GetFileName(fullPath);

                var isUnknownType = false;

                var width  = 0;
                var height = 0;
                var length = 0L;

                //画像ファイルのヘッダからサイズを抽出
                if (level >= PropertiesLevel.Size)
                {
                    var graphicInfo = new GraphicInformation(fullPath);

                    width         = graphicInfo.GraphicSize.Width;
                    height        = graphicInfo.GraphicSize.Height;
                    length        = graphicInfo.FileSize;
                    isUnknownType = graphicInfo.Type == GraphicFileType.Unknown;

                    //タグ・評価を付加できないファイルの場合は情報読み取りをスキップ
                    if (graphicInfo.Type != GraphicFileType.Jpeg &&
                        graphicInfo.Type != GraphicFileType.Tiff &&
                        graphicInfo.Type != GraphicFileType.Psd &&
                        graphicInfo.Type != GraphicFileType.Unknown)
                    {
                        hasDecoration = false;
                    }
                }

                var creationTime  = defaultDateTime;
                var lastWriteTime = defaultDateTime;

                //日付情報
                if (level >= PropertiesLevel.Basic)
                {
                    try
                    {
                        var file = new System.IO.FileInfo(fullPath);

                        creationTime  = ImageFileUtility.ConvertDateTime(file.CreationTime);
                        lastWriteTime = ImageFileUtility.ConvertDateTime(file.LastWriteTime);
                    }
                    catch
                    {
                        //No operation
                    }
                }


                image = new ImageFileInformation()
                {
                    DateCreated  = creationTime,
                    DateModified = lastWriteTime,
                    Name         = name,
                    Path         = fullPath,
                    Height       = height,
                    Width        = width,
                    Size         = length,
                    Rating       = 0,
                    Keywords     = null,
                    IsNotFound   = isUnknownType,
                };
            }
            catch
            {
                return(null);
            }


            if (image == null)
            {
                return(null);
            }


            var rating            = 0;
            HashSet <string> tags = null;

            //画像の評価・キーワード(時間かかる)
            if (level >= PropertiesLevel.Shell && hasDecoration)
            {
                try
                {
                    var fileAccesser = this.GetFile(fullPath, image.Name);


                    var kw = fileAccesser.GetDetailsOf(18);
                    if (kw != null)
                    {
                        tags = new HashSet <string>(kw
                                                    .Split(';')
                                                    .Select(x => x.Trim())
                                                    .Where(x => x != null && x.Length > 0)
                                                    .Distinct());
                    }

                    rating = 0;
                    var rateText  = fileAccesser.GetDetailsOf(19);
                    var rateArray = rateText?
                                    .Select(x => x - '0')
                                    .Where(x => x > 0 && x <= 5)
                                    .ToArray();

                    if (rateArray != null && rateArray.Length == 1)
                    {
                        rating = RateConvertingHelper.Reverse(rateArray[0]);
                    }
                }
                catch
                {
                    return(null);
                }
            }

            image.Rating   = rating;
            image.Keywords = tags;

            return(image);
        }
#pragma warning disable 1998
        private async Task <bool> LoadImageMainAsync
            (string fullPath, Size?frameSize, bool asThumbnail, bool isFill, bool cmsEnable)
        {
            this.FullPath = fullPath;

            if (fullPath.IsNullOrWhiteSpace())
            {
                return(false);
            }


            try
            {
                GraphicInformation information = null;

                using (var stream = File.OpenRead(fullPath))
                {
                    this.IsNotFound = false;

                    information = new GraphicInformation(stream);

                    this.Information = information;

                    if (information.IsMetaImage)
                    {
                        return(false);
                    }

                    //描画領域サイズ
                    var frameWidth = (!frameSize.HasValue || frameSize.Value.Width < 1)
                        ? 16.0 : frameSize.Value.Width;
                    var frameHeight = (!frameSize.HasValue || frameSize.Value.Height < 1)
                        ? 16.0 : frameSize.Value.Height;

                    //画像サイズ
                    var imageWidth  = information.GraphicSize.Width;
                    var imageHeight = information.GraphicSize.Height;

                    //デコードサイズ
                    var loadWidth  = -1.0;
                    var loadHeight = -1.0;


                    if (frameSize.HasValue &&
                        (imageWidth > frameWidth * resizeThreshold ||
                         imageHeight > frameHeight * resizeThreshold ||
                         asThumbnail))
                    {
                        //Resize

                        var verticalRate   = imageHeight / frameHeight;
                        var horizontalRate = imageWidth / frameWidth;

                        if (isFill)
                        {
                            if (horizontalRate > verticalRate)
                            {
                                loadHeight = frameHeight;
                            }
                            else
                            {
                                loadWidth = frameWidth;
                            }
                        }
                        else
                        {
                            if (horizontalRate > verticalRate)
                            {
                                loadWidth = frameWidth;
                            }
                            else
                            {
                                loadHeight = frameHeight;
                            }
                        }

                        this.Quality = (asThumbnail)
                            ? ImageQuality.LowQuality : ImageQuality.Resized;
                    }
                    else
                    {
                        //Original size

                        if (imageHeight > maxSize || imageWidth > maxSize)
                        {
                            if (imageHeight > imageWidth)
                            {
                                loadHeight = maxSize;
                            }
                            else
                            {
                                loadWidth = maxSize;
                            }
                        }

                        this.Quality = ImageQuality.OriginalSize;
                    }

                    var image = new BitmapImage();

                    image.BeginInit();
                    image.CacheOption   = BitmapCacheOption.OnLoad;
                    image.CreateOptions = BitmapCreateOptions.None;

                    if (loadWidth > 0)
                    {
                        image.DecodePixelWidth = (int)Math.Round(loadWidth);
                    }
                    else if (loadHeight > 0)
                    {
                        image.DecodePixelHeight = (int)Math.Round(loadHeight);
                    }

                    this.SetSourceToImage(image, stream, information, asThumbnail);

                    this.Image = image;
                }

                return(true);
            }
            catch (FileNotFoundException)
            {
                this.IsNotFound = true;
                return(false);
            }
            catch (DirectoryNotFoundException)
            {
                this.IsNotFound = true;
                return(false);
            }
            catch (ArgumentException)
            {
                this.IsNotFound = true;
                return(false);
            }
            catch (NotSupportedException)
            {
                //this.IsNotFound = true;
                return(false);
            }
        }