コード例 #1
0
        public ImageInfo Info(Uri uri, int pageNumber = 1)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            if (pageNumber < 0)
            {
                throw new ArgumentOutOfRangeException("pageNumber", "must be a value greater than or equal to 0");
            }

            var page = pageNumber;

            // Default is page 1
            if (page == 0)
            {
                page = 1;
            }

            // Use a temp file, much faster than calling Load/Info from a URI directly
            // In a production service, you might want to create a caching mechanism
            string tempFile = Path.GetTempFileName();

            try
            {
                // Force the uri to be fully qualified, reject everything else for security reasons
                if (uri.IsFile || uri.IsUnc)
                {
                    throw new ArgumentException("URL cannot be local file or UNC path.");
                }

                // Download the file
                if (File.Exists(HttpContext.Current.Server.MapPath("~/" + uri.LocalPath.ToString())))
                {
                    using (WebClient client = new WebClient())
                        client.DownloadFile(uri, tempFile);
                }
                else
                {
                    ImageInfo imginfo = new ImageInfo();
                    return(imginfo);
                    //throw new FileNotFoundException("Master set detail does not exists.");
                }

                using (RasterCodecs codecs = new RasterCodecs())
                {
                    // Initialize the options for RasterCodecs
                    ServiceHelper.InitCodecs(codecs, 0);

                    using (CodecsImageInfo info = codecs.GetInformation(tempFile, true, page))
                    {
                        ImageInfo imageInfo = new ImageInfo();
                        imageInfo.Uri             = uri.ToString();
                        imageInfo.FormatId        = (int)info.Format;
                        imageInfo.FormatName      = info.Format.ToString();
                        imageInfo.MimeType        = RasterCodecs.GetMimeType(info.Format);
                        imageInfo.Width           = info.Width;
                        imageInfo.Height          = info.Height;
                        imageInfo.BitsPerPixel    = info.BitsPerPixel;
                        imageInfo.BytesPerLine    = info.BytesPerLine;
                        imageInfo.SizeDisk        = info.SizeDisk;
                        imageInfo.SizeMemory      = info.SizeMemory;
                        imageInfo.Compression     = info.Compression;
                        imageInfo.ViewPerspective = GetViewPerspectiveName(info.ViewPerspective);
                        imageInfo.Order           = info.Order.ToString();
                        imageInfo.ColorSpace      = info.ColorSpace.ToString();
                        imageInfo.PageNumber      = info.PageNumber;
                        imageInfo.TotalPages      = info.TotalPages;
                        imageInfo.HasResolution   = info.HasResolution;
                        imageInfo.XResolution     = info.XResolution;
                        imageInfo.YResolution     = info.YResolution;
                        imageInfo.IsRotated       = info.IsRotated;
                        imageInfo.IsSigned        = info.IsSigned;
                        imageInfo.HasAlpha        = info.HasAlpha;

                        imageInfo.BrowserCompatible = false;

                        switch (info.Format)
                        {
                        case RasterImageFormat.Png:
                        case RasterImageFormat.Gif:
                        case RasterImageFormat.Jpeg:
                        case RasterImageFormat.Jpeg411:
                        case RasterImageFormat.Jpeg422:
                        case RasterImageFormat.JpegLab:
                        case RasterImageFormat.JpegLab411:
                        case RasterImageFormat.JpegLab422:
                        case RasterImageFormat.JpegRgb:
                            imageInfo.BrowserCompatible = true;
                            break;

                        default:
                            break;
                        }

                        return(imageInfo);
                    }
                }
            }

            catch (Exception ex)
            {
                Log(string.Format("Info - Error:{1}{0}TempFile:{2}{0}Uri:{3}, PageNumber:{4}", Environment.NewLine, ex.Message, tempFile, uri, page));
                throw;
            }
            finally
            {
                if (File.Exists(tempFile))
                {
                    try
                    {
                        File.Delete(tempFile);
                    }
                    catch { }
                }
            }
        }