Esempio n. 1
0
 public override async Task <GetThumbnailResponse> GetThumbnail(GetThumbnailRequest request)
 {
     throw new System.NotImplementedException();
 }
        public HttpResponseMessage GetThumbnail([FromUri] GetThumbnailRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            var pageNumber = request.PageNumber;

            if (string.IsNullOrEmpty(request.DocumentId))
            {
                throw new ArgumentException("documentId must not be null");
            }

            if (pageNumber < 0)
            {
                throw new ArgumentException("'pageNumber' must be a value greater than or equals to 0");
            }

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

            if (request.Width < 0 || request.Height < 0)
            {
                throw new ArgumentException("'width' and 'height' must be value greater than or equal to 0");
            }

            // Get the image format
            var saveFormat = SaveImageFormat.GetFromMimeType(request.MimeType);

            // Now load the document
            var cache = ServiceHelper.Cache;

            using (var document = DocumentFactory.LoadFromCache(cache, request.DocumentId))
            {
                DocumentHelper.CheckLoadFromCache(document);
                DocumentHelper.CheckPageNumber(document, pageNumber);

                if (request.Width > 0 && request.Height > 0)
                {
                    document.Images.ThumbnailPixelSize = new LeadSize(request.Width, request.Height);
                }

                var documentPage = document.Pages[pageNumber - 1];
                using (var image = documentPage.GetThumbnailImage())
                {
                    var stream = ImageSaver.SaveImage(image, document.RasterCodecs, saveFormat, request.MimeType, 0, 0);

                    // If we just return the stream, Web Api will try to serialize it.
                    // If the return type is "HttpResponseMessage" it will not serialize
                    // and you can set the content as you wish.
                    var response = new HttpResponseMessage();
                    response.Content = new StreamContent(stream);
                    ServiceHelper.UpdateCacheSettings(response);
                    return(response);
                }
            }
        }