コード例 #1
0
        public static bool GenerateThumbnail(Stream Source, string SourceMimeType, out Image Thumbnail)
        {
            if (Source != null)
            {
                // GDI+ (jpg, png, gif, bmp)
                if (SourceMimeType.Equals("image/jpeg", StringComparison.OrdinalIgnoreCase) || SourceMimeType.Contains("jpg") ||
                    SourceMimeType.Equals("image/png", StringComparison.OrdinalIgnoreCase) || SourceMimeType.Contains("png") ||
                    SourceMimeType.Equals("image/gif", StringComparison.OrdinalIgnoreCase) || SourceMimeType.Contains("gif") ||
                    SourceMimeType.Equals("image/bmp", StringComparison.OrdinalIgnoreCase) || SourceMimeType.Contains("bmp"))
                {
                    try
                    {
                        using (Image sourceImage = Image.FromStream(Source))
                        {
                            Thumbnail = sourceImage.ResizeImage(48, 48, Brushes.Black);
                            using (Image mimeTypeIcon = Properties.Resources.MimeType_img16)
                            {
                                Thumbnail.EmbedIconOverlay(mimeTypeIcon);
                            }
                            return true;
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToExceptionless().Submit();
                    }

                }

                // PDF
                if (SourceMimeType.Equals("application/pdf", StringComparison.OrdinalIgnoreCase) || SourceMimeType.Contains("pdf"))
                {
                    try
                    {
                        using (var pdfiumDocument = PdfiumViewer.PdfDocument.Load(Source))
                        {

                            if (pdfiumDocument.PageCount > 0)
                            {
                                var pageSize = pdfiumDocument.PageSizes[0];
                                var size = ImagingExtensions.CalculateResize((int)pageSize.Width, (int)pageSize.Height, 48, 48);

                                using (var sourceImage = pdfiumDocument.Render(0, (int)size.Width, (int)size.Height, 72, 72, true))
                                {
                                    Thumbnail = sourceImage.ResizeImage(48, 48, Brushes.White);
                                    using (Image mimeTypeIcon = Properties.Resources.MimeType_pdf16)
                                    {
                                        Thumbnail.EmbedIconOverlay(mimeTypeIcon);
                                    }
                                    return true;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToExceptionless().Submit();
                    }
                }
            }
            Thumbnail = null;
            return false;
        }