public ActionResult GetPdfPageImage(int page)
        {
            var pdfFilePath = HttpContext.Server.MapPath("~/App_Data/Sample3.PDF");

            var pdfToImg = new PdfToImageConverter();

            pdfToImg.ScaleTo = 800;

            var memStream = new MemoryStream();

            pdfToImg.GenerateImage(pdfFilePath, page, ImageFormat.Png, memStream);
            return(File(memStream.ToArray(), "image/png"));
        }
Ejemplo n.º 2
0
        public async Task <byte[]> getImagePreview(TFile file)
        {
            try
            {
                byte[] outPreview = null;
                bool   topImage   = false;
                if (file.TipoArchivo.Contains("video"))
                {
                    try {
found:
                        string nameMD5 = DateTime.Now.Ticks.GetHashCode().ToString("x").ToUpper();
                        var VideoPath = AppDomain.CurrentDomain.BaseDirectory + @"PreviewFile\Video_" + nameMD5 + "." + file.Nombre.Split('.')[file.Nombre.Split('.').Length - 1];
                        if (File.Exists(VideoPath))
                        {
                            goto found;
                        }
                        File.WriteAllBytes(VideoPath, file.ArchivoData);
                        var imageFile = AppDomain.CurrentDomain.BaseDirectory + @"PreviewFile\VidImage_" + nameMD5 + ".JPG";
                        var urlFile   = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
                        FFmpeg.SetExecutablesPath(urlFile + @"\MediaToolkit", "ffmpeg");
                        var infoVideo = await FFmpeg.GetMediaInfo(VideoPath);

                        var conversion = await FFmpeg.Conversions.FromSnippet.Snapshot(VideoPath, imageFile, infoVideo.Duration / 2.0);

                        await conversion.Start();

                        outPreview = File.ReadAllBytes(imageFile);
                        File.Delete(VideoPath);
                        File.Delete(imageFile);
                    }
                    catch (Exception e) {
                        outPreview = new byte[0];
                    }
                }
                else if (file.TipoArchivo.Contains("pdf"))
                {
                    try
                    {
again:
                        string nameMD5 = DateTime.Now.Ticks.GetHashCode().ToString("x").ToUpper();
                        var pdfFile = AppDomain.CurrentDomain.BaseDirectory + @"PreviewFile\DocPdf_" + nameMD5 + ".pdf";
                        if (File.Exists(pdfFile))
                        {
                            goto again;
                        }
                        File.WriteAllBytes(pdfFile, file.ArchivoData);
                        var imageFile = AppDomain.CurrentDomain.BaseDirectory + @"PreviewFile\DocImg_" + nameMD5 + ".JPG";
                        var pdfToImg  = new PdfToImageConverter();
                        pdfToImg.ScaleTo = 3000; // fit 200x200 box
                        pdfToImg.GenerateImage(pdfFile, 1, ImageFormat.Jpeg, imageFile);
                        outPreview = File.ReadAllBytes(imageFile);
                        File.Delete(pdfFile);
                        File.Delete(imageFile);
                        topImage = true;
                    }
                    catch (Exception e)
                    {
                        outPreview = new byte[0];
                    }
                }
                else
                {
                    outPreview = file.ArchivoData;
                }
                Stream stream  = new MemoryStream(outPreview);
                Image  imageIn = Image.FromStream(stream);
                Bitmap bmPhoto = new Bitmap(imageIn);

                // recortar imagen
                int sourceWidth = bmPhoto.Width;
                int sourceHeight = bmPhoto.Height;
                int cuadro = 0;
                int x = 0, y = 0;
//                double div = (minWidth * 1.0 / minHeight * 1.0);
                if (sourceWidth > sourceHeight)
                {
                    cuadro = sourceHeight;
                    x      = sourceWidth / 2 - sourceHeight / 2;
                }
                else
                {
                    cuadro = sourceWidth;
                    if (topImage)
                    {
                        y = 0;
                    }
                    else
                    {
                        y = sourceHeight / 2 - sourceWidth / 2;
                    }
                }
                Rectangle cropArea = new Rectangle(x, y, cuadro, cuadro);

                Bitmap bmpCrop = bmPhoto.Clone(cropArea, bmPhoto.PixelFormat);

                //cambiar tamaño
                Bitmap bmpRsize = resizeImage(bmpCrop);
                // cambiar resolucion
                bmpRsize.SetResolution(50, 50);
                MemoryStream ms = new MemoryStream();

                bmpRsize.Save(ms, imageIn.RawFormat);
                return(ms.ToArray());
            }
            catch (Exception ex)
            {
                return(new byte[0]);
            }
        }