public IActionResult Image(Guid id, int size, string extension)
        {
            const string pngContentType    = "image/png";
            const string svgContentType    = "image/svg+xml";
            var          virtualFileResult = File(Url.Content("~/images/file.svg"), svgContentType);

#if DNX451
            if (size >= 10 * 1024 * 1024)
            {
                return(virtualFileResult);
            }

            var serverApi = HttpContext.GetServerApi();
            var file      = serverApi.GetFileChunk(id, 0, size);
            try
            {
                if (file != null)
                {
                    int        page       = 1;
                    int        dpi        = 150;
                    RenderType RenderType = RenderType.RGB;
                    bool       rotateAuto = false;
                    string     password   = "";

                    var fileName = $"tmp/{id}{extension}";
                    using (var fileStream = System.IO.File.Create(fileName))
                        fileStream.Write(file, 0, file.Length);

                    byte[] thumbnailContent;
                    using (MuPDF pdfDoc = new MuPDF(fileName, password))
                    {
                        pdfDoc.Page = page;
                        Bitmap bm = pdfDoc.GetBitmap(0, 0, dpi, dpi, 0, RenderType, rotateAuto, false, 0);
                        HttpContext.Response.ContentType = pngContentType;
                        using (var ms = new MemoryStream())
                        {
                            bm.Save(ms, ImageFormat.Png);
                            thumbnailContent = ms.ToArray();
                        }
                    }

                    System.IO.File.Delete(fileName);

                    return(File(thumbnailContent, pngContentType, $"{id}.png"));
                }
            }
            catch (Exception ex)
            {
                _logger.LogWarning(1, "Unable to generate thumbnail for file", ex);
            }
#endif
            return(virtualFileResult);
        }
Exemplo n.º 2
0
        internal List <Tiff.PageInfo> GetBitmapsCollectionFromFile(string fileName, int startPage, int endPage, int dpiX,
                                                                   int dpiY, bool color)
        {
            var imagesToSave = new List <Tiff.PageInfo>();

            if (string.IsNullOrEmpty(fileName) || !File.Exists(fileName))
            {
                return(imagesToSave);
            }

            string uP = "", oP = "";

            Dictionary <int, Controls.PdfViewControl.PageInfo> pInfo = Refresh(fileName, ref oP, ref uP);

            if (pInfo == null || pInfo.Count == 0)
            {
                return(imagesToSave);
            }

            try
            {
                using (MuPDF mp = new MuPDF(fileName, oP, true))
                {
                    if (mp == null)
                    {
                        return(imagesToSave);
                    }

                    int stP  = (startPage <= 0 ? 1 : startPage),
                        endP = (endPage <= 0 || endPage > mp.PageCount ? mp.PageCount : endPage);
                    pInfo = pInfo.Where(x => x.Key >= stP && x.Key <= endP).ToDictionary(x => x.Key, x => x.Value);

                    foreach (var p in pInfo)
                    {
                        mp.Page = p.Key;
                        Bitmap bm = mp.GetBitmap((int)Math.Round(mp.Width / 72.0 * dpiX),
                                                 (int)Math.Round(mp.Height / 72.0 * dpiY), dpiX, dpiY, 0,
                                                 color ? RenderType.RGB : RenderType.Monochrome, true, false, 0);

                        imagesToSave.Add(new Tiff.PageInfo {
                            Image = bm
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                Data.Env.WriteToLog(ex);
            }

            return(imagesToSave);
        }
Exemplo n.º 3
0
 internal Bitmap GetBitmap(int width, int height, float dpix, float dpiy, int rotation, RenderType renderType, bool rotateLandscapePages, bool convertToLetter, int maxSize)
 {
     if (checkMP())
     {
         Bitmap bmp = mp.GetBitmap(width, height, dpix, dpiy, rotation, renderType, rotateLandscapePages, convertToLetter, maxSize);
         if (!useLock)
         {
             mp.Dispose();
             mp = null;
         }
         return(bmp);
     }
     return(null);
 }
Exemplo n.º 4
0
        public static List <Tiff.PageInfo> GetPDFImages(string fileName, int from, int to)
        {
            var imagesToSave = new List <Tiff.PageInfo>();

            try
            {
                if (!File.Exists(fileName))
                {
                    return(imagesToSave);
                }

                using (var mp = new MuPDF(fileName, "", true))
                {
                    int start  = (@from > 0) ? @from : 1;
                    int finish = (to > 0 && to <= mp.PageCount) ? to : mp.PageCount;
                    if (start > finish)
                    {
                        finish  = start - finish;
                        start  -= finish;
                        finish += start;
                    }
                    if (finish > mp.PageCount)
                    {
                        finish = mp.PageCount;
                    }
                    int i     = start;
                    var pages = new int[finish - start + 1];
                    pages = pages.Select(x => i++).ToArray();

                    foreach (var p in pages)
                    {
                        mp.Page = p;
                        Bitmap bm = mp.GetBitmap((int)Math.Round(mp.Width / 72.0 * 96.0), (int)Math.Round(mp.Height / 72.0 * 96.0),
                                                 96f, 96f, 0, RenderType.RGB, true, false, 0);
                        imagesToSave.Add(new Tiff.PageInfo {
                            Image = bm, Compression = new CompressionInfo()
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                Data.Env.WriteToLog(ex);
            }

            return(imagesToSave);
        }
Exemplo n.º 5
0
        private void loadPdf(string pdfFilename)
        {
            MuPDF muPdf = new MuPDF(pdfFilename, "");

            pageCount = muPdf.PageCount;
            Array.Resize(ref bitmaps, pageCount);
            MuPDF pdfPage;

            for (int page = 1; page <= pageCount; page++)
            {
                pdfPage           = new MuPDF(pdfFilename, "");
                pdfPage.Page      = page;
                bitmaps[page - 1] = pdfPage.GetBitmap(450, 0, 300, 300, 0, RenderType.RGB, false, false, 10000000);
            }
            numericUpDownPageSelector.Value = 1;
            pictureBox1.Image = bitmaps[0];
        }
Exemplo n.º 6
0
        public override Bitmap LoadPage(int pageNumber)
        {
            if (!File.Exists(fileName))
            {
                throw new Exception("File does not exist.");
            }

            if (bmp != null)
            {
                bmp.Dispose();
            }

            var pdf = new MuPDF(fileName, "");

            pdf.Page      = pageNumber;
            pdf.AntiAlias = true;

            int width  = (int)(pdf.Width * resolution / 72.0F);
            int height = (int)(pdf.Height * resolution / 72.0F);

            int estMemUsageMB = (int)Math.Ceiling(width * height * 3.0 / 1024 / 1024);

            Debug.WriteLine("Estimated memory {0} MB", estMemUsageMB);
            System.Runtime.MemoryFailPoint mfp = null;
            try
            {
                mfp = new System.Runtime.MemoryFailPoint(estMemUsageMB);
                bmp = pdf.GetBitmap(width, height, 72, 72, 0, RenderType.RGB, false, false, 0);
            }
            catch (InsufficientMemoryException)
            {
                throw new Exception("Not enough memory.");
            }
            finally
            {
                mfp.Dispose();
                pdf.Dispose();
            }

            return(bmp);
        }
Exemplo n.º 7
0
        public static byte[] ConvertPdfToTiff(byte[] image, float dpi, RenderType type, bool rotateLandscapePages, bool shrinkToLetter, int maxSizeInPdfPixels, string pdfPassword)
        {
            byte[] output = null;

            if (image == null)
            {
                throw new ArgumentNullException("image");
            }

            using (MuPDF pdfDoc = new MuPDF(image, pdfPassword))
            {
                using (MemoryStream outputStream = new MemoryStream())
                {
                    ImageCodecInfo info = null;
                    foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
                    {
                        if (ice.MimeType == "image/tiff")
                        {
                            info = ice;
                        }
                    }

                    Bitmap saveTif = null;
                    pdfDoc.AntiAlias = false;
                    for (int i = 1; i <= pdfDoc.PageCount; i++)
                    {
                        int Width  = 0; //Zero for no resize.
                        int Height = 0; //Zero for autofit height to width.

                        pdfDoc.Page = i;

                        Bitmap FirstImage = pdfDoc.GetBitmap(Width, Height, dpi, dpi, 0, type, rotateLandscapePages, shrinkToLetter, maxSizeInPdfPixels);
                        if (FirstImage == null)
                        {
                            throw new Exception("Unable to convert pdf to tiff!");
                        }
                        using (EncoderParameters ep = new EncoderParameters(2))
                        {
                            ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
                            ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW);
                            if (type == RenderType.Monochrome)
                            {
                                ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
                            }

                            if (i == 1)
                            {
                                saveTif = FirstImage;
                                saveTif.Save(outputStream, info, ep);
                            }
                            else
                            {
                                ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
                                saveTif.SaveAdd(FirstImage, ep);
                                FirstImage.Dispose();
                            }
                            if (i == pdfDoc.PageCount)
                            {
                                ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush);
                                saveTif.SaveAdd(ep);
                                saveTif.Dispose();
                            }
                        }
                    }
                    output = outputStream.ToArray();
                }
            }
            return(output);
        }
Exemplo n.º 8
0
        public static bool ConvertPdfToTiff(string sourceFile, string outputFile, float dpi, RenderType type, bool rotateLandscapePages, bool shrinkToLetter, int maxSizeInPdfPixels, string pdfPassword)
        {
            bool output = false;

            if (string.IsNullOrEmpty(sourceFile) || string.IsNullOrEmpty(outputFile))
            {
                throw new ArgumentNullException();
            }

            using (MuPDF pdfDoc = new MuPDF(sourceFile, pdfPassword, true))
            {
                using (FileStream outputStream = new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                {
                    ImageCodecInfo info = null;
                    foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
                    {
                        if (ice.MimeType == "image/tiff")
                        {
                            info = ice;
                        }
                    }

                    Bitmap saveTif = null;
                    pdfDoc.AntiAlias = false;
                    for (int i = 1; i <= pdfDoc.PageCount; i++)
                    {
                        pdfDoc.Page = i;

                        Bitmap FirstImage = pdfDoc.GetBitmap(0, 0, dpi, dpi, 0, type, rotateLandscapePages, shrinkToLetter, maxSizeInPdfPixels);
                        if (FirstImage == null)
                        {
                            throw new Exception("Unable to convert pdf to tiff!");
                        }

                        using (EncoderParameters ep = new EncoderParameters(2))
                        {
                            ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
                            ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW);
                            if (type == RenderType.Monochrome)
                            {
                                ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
                            }

                            if (i == 1)
                            {
                                saveTif = FirstImage;
                                saveTif.Save(outputStream, info, ep);
                            }
                            else
                            {
                                ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
                                saveTif.SaveAdd(FirstImage, ep);
                                FirstImage.Dispose();
                            }
                            if (i == pdfDoc.PageCount)
                            {
                                ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush);
                                saveTif.SaveAdd(ep);
                                saveTif.Dispose();
                            }
                        }
                    }
                }
                if (File.Exists(outputFile))
                {
                    output = true;
                }
            }
            return(output);
        }
Exemplo n.º 9
0
        public static bool ConvertPdfToFaxTiff(string sourceFile, string outputFile, float dpi, bool shrinkToLetter, string pdfPassword)
        {
            bool       output      = false;
            const long Compression = (long)EncoderValue.CompressionCCITT4;

            using (MuPDF pdfDoc = new MuPDF(sourceFile, pdfPassword, true))
            {
                using (FileStream outputStream = new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                {
                    ImageCodecInfo info = null;
                    foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
                    {
                        if (ice.MimeType == "image/tiff")
                        {
                            info = ice;
                        }
                    }

                    Bitmap saveTif = null;
                    pdfDoc.AntiAlias = false;
                    for (int i = 1; i <= pdfDoc.PageCount; i++)
                    {
                        int Width = 0;//Zero for no resize.
                        //int Height = 0;//Zero for autofit height to width.
                        float DpiX = dpi;
                        float DpiY = dpi;

                        pdfDoc.Page = i;

                        if (dpi == 200)
                        {
                            Width = 1728;
                            DpiX  = 204;
                            DpiY  = 196;
                        }
                        else if (dpi == 300)
                        {
                            Width = 2592;
                        }
                        else if (dpi == 400)
                        {
                            Width = 3456;
                        }

                        Bitmap FirstImage = pdfDoc.GetBitmap(Width, 0, DpiX, DpiY, 0, RenderType.Monochrome, true, shrinkToLetter, 0);
                        if (FirstImage == null)
                        {
                            throw new Exception("Unable to convert pdf to tiff!");
                        }
                        using (EncoderParameters ep = new EncoderParameters(2))
                        {
                            ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
                            ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, Compression);

                            if (i == 1)
                            {
                                saveTif = FirstImage;
                                saveTif.Save(outputStream, info, ep);
                            }
                            else
                            {
                                ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
                                saveTif.SaveAdd(FirstImage, ep);
                                FirstImage.Dispose();
                            }
                            if (i == pdfDoc.PageCount)
                            {
                                ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush);
                                saveTif.SaveAdd(ep);
                                saveTif.Dispose();
                            }
                        }
                    }
                }
                if (File.Exists(outputFile))
                {
                    output = true;
                }
            }
            return(output);
        }