internal string ImageResize(HttpPostedFileBase FileUpload1, int width, int height)
        {
            string fileName = FileUpload1.FileName.Replace(" ", "");

            UploadedFileName = HttpContext.Current.Server.MapPath("~/images/Thumbnails/" + DateTime.Now.ToShortDateString().Trim().Replace(':', '_').Replace('.', '_') + DateTime.Now.ToShortTimeString().Trim().Replace(':', '_').Replace('.', '_') + fileName + ".png");
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(FileUpload1.InputStream);
            Bitmap            image          = loadedDocument.ExportAsImage(0);
            Bitmap            bmp1           = ImageResize(image, width, height);
            ImageCodecInfo    jgpEncoder     = GetEncoder(bmp1.RawFormat.Equals(ImageFormat.Png) ? ImageFormat.Png : ImageFormat.Jpeg);

            System.Drawing.Imaging.Encoder myEncoder =
                System.Drawing.Imaging.Encoder.Quality;
            EncoderParameters myEncoderParameters = new EncoderParameters(1);
            EncoderParameter  myEncoderParameter  = new EncoderParameter(myEncoder,
                                                                         50L);

            myEncoderParameter           = new EncoderParameter(myEncoder, 100L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            using (FileStream stream = File.Create(UploadedFileName))
            {
                bmp1.Save(stream, jgpEncoder, myEncoderParameters);
            }
            loadedDocument.Close(true);
            return("/images/Thumbnails/" + DateTime.Now.ToShortDateString().Trim().Replace(':', '_').Replace('.', '_') + DateTime.Now.ToShortTimeString().Trim().Replace(':', '_').Replace('.', '_') + fileName + ".png");
        }
        internal Tuple <string, string> ImageResize(HttpPostedFileBase FileUpload1, int width, int height, int bigWidth, int bigHeight)
        {
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(FileUpload1.InputStream);
            string            fileName       = FileUpload1.FileName.Replace(" ", "");

            UploadedFileName = HttpContext.Current.Server.MapPath("~/images/Upload/" + DateTime.Now.ToShortDateString().Trim().Replace(':', '_').Replace('.', '_') + DateTime.Now.ToShortTimeString().Trim().Replace(':', '_').Replace('.', '_') + fileName + ".png");
            Bitmap         image      = loadedDocument.ExportAsImage(0);
            Bitmap         bmp1       = ImageResize(image, bigWidth, bigHeight);
            ImageCodecInfo jgpEncoder = GetEncoder(bmp1.RawFormat.Equals(ImageFormat.Png) ? ImageFormat.Png : ImageFormat.Jpeg);

            System.Drawing.Imaging.Encoder myEncoder =
                System.Drawing.Imaging.Encoder.Quality;
            EncoderParameters myEncoderParameters = new EncoderParameters(1);
            EncoderParameter  myEncoderParameter  = new EncoderParameter(myEncoder,
                                                                         50L);

            myEncoderParameter           = new EncoderParameter(myEncoder, 100L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            using (FileStream stream = File.Create(UploadedFileName))
            {
                bmp1.Save(stream, jgpEncoder, myEncoderParameters);
            }
            UploadedFileName = "~/images/Upload/" + UploadedFileName.Split('\\')[UploadedFileName.Split('\\').Length - 1].ToString();
            string imageUrlThumbnail = HttpContext.Current.Server.MapPath("~/images/Thumbnails/" + DateTime.Now.ToShortDateString().Trim().Replace(':', '_').Replace('.', '_') + DateTime.Now.ToShortTimeString().Trim().Replace(':', '_').Replace('.', '_') + fileName + ".png");

            System.Drawing.Image    i         = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(UploadedFileName));
            System.Drawing.Image    thumbnail = new System.Drawing.Bitmap(width, height);
            System.Drawing.Graphics g         = System.Drawing.Graphics.FromImage(thumbnail);
            g.DrawImage(i, 0, 0, width, height);
            loadedDocument.Close(true);
            thumbnail.Save(imageUrlThumbnail);
            return(new Tuple <string, string>("/images/Upload/" + DateTime.Now.ToShortDateString().Trim().Replace(':', '_').Replace('.', '_') + DateTime.Now.ToShortTimeString().Trim().Replace(':', '_').Replace('.', '_') + fileName, "/images/Thumbnails/" + DateTime.Now.ToShortDateString().Trim().Replace(':', '_').Replace('.', '_') + DateTime.Now.ToShortTimeString().Trim().Replace(':', '_').Replace('.', '_') + fileName + ".png"));
        }
        public ActionResult ExportasImage(string Browser, string submit1)
        {
            if (submit1 == "View Template")
            {
                string dataPath = ResolveApplicationDataPath("MultiColumnReports.pdf");
                Stream file2    = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.Read);

                //Load the template document
                PdfLoadedDocument ldoc = new PdfLoadedDocument(file2);
                return(ldoc.ExportAsActionResult("InputTemplate.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open));
            }

            //Loaded input pdf file.
            loadedDocument = new PdfLoadedDocument(ResolveApplicationDataPath("MultiColumnReports.pdf"));

            //Exporting specify page index as image.
            Bitmap image = loadedDocument.ExportAsImage(0);

            //Stream the output to the browser.
            if (Browser == "Browser")
            {
                ExportAsImage(image, "sample.jpeg", ImageFormat.Jpeg, HttpContext.ApplicationInstance.Response, HttpReadType.Open);
            }

            else
            {
                ExportAsImage(image, "sample.jpeg", ImageFormat.Jpeg, HttpContext.ApplicationInstance.Response, HttpReadType.Save);
            }
            return(View());
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Clear text box
            txtResult.Text = string.Empty;

            string[] words     = txtImageFile.Text.Split('.');
            string   extension = words[words.Length - 1];

            if (extension.ToLower() == "png" || extension.ToLower() == "bmp" || extension.ToLower() == "jpg")
            {
                if (txtImageFile.Text != "" && txtImageFile.Text != string.Empty)
                {
                    if (checkBox1.Checked)
                    {
                        //Read multiple barcodes
                        BarcodeResultCollection result = BarcodeReader.ScanMultipleBarcode(txtImageFile.Text);
                        AddText(result);
                    }
                    else
                    {
                        //Read barcode
                        BarcodeResult result = BarcodeReader.ScanBarcode(txtImageFile.Text);
                        AddText(result);
                    }
                }
            }
            else if (extension.ToLower() == "pdf")
            {
                Bitmap[] images;

                //Load existing document
                PdfLoadedDocument ldoc = new PdfLoadedDocument(txtImageFile.Text);

                //Export the document as images
                images = ldoc.ExportAsImage(0, ldoc.Pages.Count - 1);

                //Scan barcodes
                foreach (Bitmap img in images)
                {
                    BarcodeReader reader = new BarcodeReader(img);
                    if (checkBox1.Checked)
                    {
                        reader.Settings.TryHarder = true;
                        BarcodeResultCollection results = reader.ScanMultipleBarcode();
                        AddText(results);
                    }
                    else
                    {
                        BarcodeResult result = reader.ScanBarcode();
                        AddText(result);
                    }
                }
            }

            else
            {
                MessageBox.Show("Please select a barcode image or PDF", "Barcode Reader", MessageBoxButtons.OK);
            }
        }
Пример #5
0
 public string PdfImageToBase64(HttpPostedFileBase FileUpload1)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         PdfLoadedDocument loadedDocument = new PdfLoadedDocument(FileUpload1.InputStream);
         Bitmap            image          = loadedDocument.ExportAsImage(0);
         image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
         byte[] imageBytes   = ms.ToArray();
         string base64String = Convert.ToBase64String(imageBytes);
         return(base64String);
     }
 }
Пример #6
0
        static void Main(string[] args)
        {
            var app = new CommandLineApplication();

            app.HelpOption("--help");

            var inputArg     = app.Argument("[input pdf]", "");
            var formatOption = app.Option("--format", "output file format", CommandOptionType.SingleValue);
            var outputOption = app.Option("--output", "output folder", CommandOptionType.SingleValue);

            app.OnExecute(() =>
            {
                var input  = inputArg.Value;
                var output = outputOption.Value();

                var fileName         = Path.GetFileNameWithoutExtension(input);
                var fileNameTemplate = formatOption.HasValue() ? formatOption.Value() : "{filename} ({page}).{extension}";
                if (string.IsNullOrEmpty(output))
                {
                    output = Path.GetDirectoryName(input);
                }

                using (var doc = new PdfLoadedDocument(input))
                {
                    for (var i = 0; i < doc.Pages.Count; i++)
                    {
                        using (var bitmap = doc.ExportAsImage(i))
                        {
                            var outputFileName = fileNameTemplate
                                                 .Replace("{filename}", fileName)
                                                 .Replace("{page}", i.ToString())
                                                 .Replace("{pagecount}", doc.Pages.Count.ToString())
                                                 .Replace("{extension}", "png");

                            var outputPath = Path.Combine(output, outputFileName);
                            bitmap.Save(outputPath, ImageFormat.Png);
                        }
                    }
                }

                return(0);
            });

            app.Execute(args);
        }
Пример #7
0
        /// <summary>
        /// Convert base64 PDF string to any type of image
        /// </summary>
        /// <param name="PDFBase64">PDF in base64 string</param>
        /// <param name="Format">Format of the image conversion</param>
        /// <returns>Operation result that has the status and information of the conversion</returns>
        public static OperationResult PdfToImageConvert(byte[] file, string DestinyPath, string Format)
        {
            OperationResult result = new OperationResult();

            try
            {
                PdfLoadedDocument loadedDocument = new PdfLoadedDocument(file, true);
                Bitmap            image          = loadedDocument.ExportAsImage(0);
                image.Save(DestinyPath, IdentifyImageFormat(Format));
                loadedDocument.Close(true);
            }
            catch (Exception es)
            {
                result.WasSuccesful = false;
                result.Message      = es.Message;
                result.Error        = es;
            }

            return(result);
        }
Пример #8
0
        /// <summary>
        /// Convert PDF from path to any type of image
        /// </summary>
        /// <param name="PDFfilePath">Path of the file ubication</param>
        /// <param name="DestinyPath">Path to store the document</param>
        /// <param name="Format">Format of the image conversion</param>
        /// <returns>Operation result that has the status and information of the conversion</returns>
        public static OperationResult PdfToImageConvert(string PDFfilePath, string DestinyPath, string Format)
        {
            OperationResult result   = new OperationResult();
            var             fileName = Path.GetFileNameWithoutExtension(PDFfilePath);

            try
            {
                PdfLoadedDocument loadedDocument = new PdfLoadedDocument(PDFfilePath);
                Bitmap            image          = loadedDocument.ExportAsImage(0);
                image.Save(DestinyPath, IdentifyImageFormat(Format));
                loadedDocument.Close(true);
            }
            catch (Exception es)
            {
                result.WasSuccesful = false;
                result.Message      = es.Message;
                result.Error        = es;
            }

            return(result);
        }
Пример #9
0
        private void PDFtoJPG(string filePath, int saveType = 0)
        {
            // saveType of 1 = png, all other cases are jpg.

            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(filePath);
            string            _baseName      = Path.GetFileNameWithoutExtension(filePath);
            string            _parentFolder  = Path.GetDirectoryName(filePath);
            int count = loadedDocument.Pages.Count;

            for (int i = 0; i < count; i++)
            {
                Image image = loadedDocument.ExportAsImage(i);
                if (saveType == 1)
                {
                    image.Save(_parentFolder + "\\" + _baseName + " " + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
                }
                else
                {
                    image.Save(_parentFolder + "\\" + _baseName + " " + i + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
            loadedDocument.Close(true);
        }
Пример #10
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = "";
                string fileName = "";

                string myFilePath = openFileDialog.FileName;
                string ext        = Path.GetExtension(myFilePath);
                switch (ext)
                {
                case ".jpg":
                    ProcessImage((Bitmap)Bitmap.FromFile(openFileDialog.FileName));
                    break;

                case ".png":
                    ProcessImage((Bitmap)Bitmap.FromFile(openFileDialog.FileName));
                    break;

                case ".xlsx":
                    Workbook workbook = new Workbook(openFileDialog.FileName);
                    //Get the first worksheet.
                    Worksheet sheet = workbook.Worksheets[0];

                    //Define ImageOrPrintOptions
                    ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
                    //Specify the image format
                    imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
                    //Only one page for the whole sheet would be rendered
                    imgOptions.OnePagePerSheet = true;

                    //Render the sheet with respect to specified image/print options
                    SheetRender sr = new SheetRender(sheet, imgOptions);
                    //Render the image for the sheet
                    Bitmap bitmap = sr.ToImage(0);

                    //Save the image file specifying its image format.
                    //bitmap.Save("excel.jpg");
                    ProcessImage(bitmap);

                    break;

                case ".doc":
                    filePath = Path.GetDirectoryName(openFileDialog.FileName);
                    fileName = Path.GetFileNameWithoutExtension(openFileDialog.FileName) + ".doc";
                    WordToJpg(filePath, fileName);
                    break;

                case ".docx":
                    filePath = Path.GetDirectoryName(openFileDialog.FileName);
                    fileName = Path.GetFileNameWithoutExtension(openFileDialog.FileName) + ".docx";
                    WordToJpg(filePath, fileName);
                    break;

                case ".pdf":
                    PdfLoadedDocument loadedDocument = new PdfLoadedDocument(openFileDialog.FileName);
                    Bitmap            image          = loadedDocument.ExportAsImage(0);
                    //image.Save("Image.jpg", ImageFormat.Jpeg);
                    ProcessImage(image);
                    loadedDocument.Close(true);
                    break;
                }
            }
        }