コード例 #1
0
ファイル: PDFEngine.cs プロジェクト: apitron/MCL-OfficeTools
        private string ProcessPage(PDFFile pdf, string destDir, string guid, int pageIndex, int pageCount, int imgQuality)
        {
            string imgPaths = destDir + guid;//Guid.NewGuid().ToString();

            if (!Directory.Exists(imgPaths))
            {
                Directory.CreateDirectory(imgPaths);
            }

            if (imgQuality == 0)
            {
                imgQuality = 100;
            }

            Bitmap oriBmp = pdf.GetPageImage(pageIndex, 96);

            Bitmap bmp = ImageUtility.CutAsBmp(oriBmp, CutBorderWidth, CutTopHeight, oriBmp.Width - 2 * CutBorderWidth, oriBmp.Height - CutTopHeight - CutBottomHeight);

            string result = string.Format(@"{0}\{1}.jpg", imgPaths, pageIndex);

            if (bmp.Width >= 700)
            {
                _imageHeight = (int)bmp.Height; // / 2;
                _imageWidth  = (int)bmp.Width;  // / 2;
                ImageUtility.ThumbAsJPG(bmp, result, _imageWidth, _imageHeight, imgQuality);

                //tempImg = bmp.GetThumbnailImage((int)bmp.Width / 2, (int)bmp.Height / 2, new Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
                //tempImg.Save(string.Format(@"{0}\{1}-ori.jpg", imgPaths, i), System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            else
            {
                _imageHeight = bmp.Height;
                _imageWidth  = bmp.Width;

                ImageUtility.CompressAsJPG(bmp, result, imgQuality);
            }
            return(result);
            //bmp.Save(string.Format(@"{0}\{1}.jpg",imgPaths,i), System.Drawing.Imaging.ImageFormat.Jpeg);
        }
コード例 #2
0
ファイル: PDFEngine.cs プロジェクト: apitron/MCL-OfficeTools
        /// <summary>
        /// 转换PDF文件
        /// </summary>
        /// <param name="srcPath"></param>
        /// <param name="destDir"></param>
        /// <param name="fileGUID"></param>
        /// <param name="pageIndex">转换的页码,-1表示转换所有页</param>
        /// <param name="imgQuality"></param>
        /// <param name="combineImages"></param>
        /// <returns></returns>
        public string Convert(string srcPath, string destDir, string fileGUID, Boolean combineImages, int pageIndex = -1, int imgQuality = 0)
        {
            string imgPaths = destDir + fileGUID;//Guid.NewGuid().ToString();
            string result   = string.Empty;

            if (pageIndex < 0)
            {
                if (combineImages)
                {
                    result = string.Format(@"{0}\{1}.jpg", imgPaths, fileGUID);
                }
                else
                {
                    result = string.Format(@"{0}\0.jpg", imgPaths);
                }
            }
            else
            {
                result = string.Format(@"{0}\{1}.jpg", imgPaths, pageIndex);
            }
            //如果已经存在,则直接返回
            if (File.Exists(result))
            {
                return(result);
            }


            if (!Directory.Exists(imgPaths))
            {
                Directory.CreateDirectory(imgPaths);
            }


            PDFFile pdf = PDFFile.Open(srcPath);

            try
            {
                int pageCount = pdf.PageCount;
                //根据页索取
                if (pageIndex >= 0 && pageIndex < pageCount)
                {
                    return(ProcessPage(pdf, destDir, fileGUID, pageIndex, pageCount, imgQuality));
                }
                //获取所有页
                else if (pageIndex == -1)
                {
                    string[] ImgPaths = new string[pdf.PageCount];

                    //全部取的情况
                    for (int i = 0; i < pdf.PageCount; i++)
                    {
                        ImgPaths[i] = ProcessPage(pdf, destDir, fileGUID, i, pageCount, imgQuality);
                    }

                    //拼接图片
                    if (combineImages)
                    {
                        int imgCount = ImgPaths.Count();
                        if (imgCount > 0)
                        {
                            Bitmap   resultImg      = new Bitmap(_imageWidth, _imageHeight * imgCount);
                            Graphics resultGraphics = Graphics.FromImage(resultImg);
                            Image    tempImage;
                            for (int i = 0; i < ImgPaths.Length; i++)
                            {
                                tempImage = Image.FromFile(ImgPaths[i]);
                                if (i == 0)
                                {
                                    resultGraphics.DrawImage(tempImage, 0, 0);
                                }
                                else
                                {
                                    resultGraphics.DrawImage(tempImage, 0, _imageHeight * i);
                                }

                                tempImage.Dispose();
                            }

                            ImageUtility.CompressAsJPG(resultImg, result, imgQuality);
                            resultGraphics.Dispose();
                        }
                    }

                    return(result);
                }
                return("");
            }
            finally
            {
                pdf.Dispose();
            }
        }
コード例 #3
0
ファイル: DocEngine.cs プロジェクト: apitron/MCL-OfficeTools
        private string ProcessPage(Document doc, string destDir, string fileGUID, int pageCount, int imgQuality, Boolean combineImages)
        {
            string imgPaths = destDir + fileGUID;//Guid.NewGuid().ToString();

            if (!Directory.Exists(imgPaths))
            {
                Directory.CreateDirectory(imgPaths);
            }

            if (imgQuality == 0)
            {
                imgQuality = 100;
            }

            var result = "";

            ImageOptions imageOptions = new ImageOptions();

            imageOptions.JpegQuality = 100;

            var tifImagePath = string.Format(@"{0}\{1}.tif", imgPaths, fileGUID);

            doc.SaveToImage(0, doc.PageCount, tifImagePath, imageOptions);
            Utils.Tif2Jpeg(tifImagePath, imgQuality, false);

            //拼接图片
            if (combineImages)
            {
                int imgCount = pageCount;
                if (imgCount > 0)
                {
                    Bitmap   resultImg      = null;
                    Graphics resultGraphics = null;
                    Image    tempImage;

                    for (int i = 0; i < imgCount; i++)
                    {
                        var imgFile = imgPaths + "\\" + i + ".jpg";
                        if (File.Exists(imgFile))
                        {
                            tempImage = Image.FromFile(imgFile);

                            if (resultImg == null)
                            {
                                _imageHeight = tempImage.Height;

                                resultImg      = new Bitmap(tempImage.Width, tempImage.Height * imgCount);
                                resultGraphics = Graphics.FromImage(resultImg);
                            }


                            if (i == 0)
                            {
                                resultGraphics.DrawImage(tempImage, 0, 0);
                            }
                            else
                            {
                                resultGraphics.DrawImage(tempImage, 0, _imageHeight * i);
                            }

                            tempImage.Dispose();
                        }
                    }
                    result = string.Format(@"{0}\{1}.jpg", imgPaths, fileGUID);
                    ImageUtility.CompressAsJPG(resultImg, result, imgQuality);
                    resultGraphics.Dispose();
                }
            }
            else
            {
                result = string.Format(@"{0}\{1}.jpg", imgPaths, 0);
            }
            return(result);
        }
コード例 #4
0
ファイル: Utils.cs プロジェクト: apitron/MCL-OfficeTools
        /// <summary>
        /// Tif转换至Jpg
        /// </summary>
        /// <param name="tifFilePath">tif文件路径</param>
        /// <param name="imgQuality"></param>
        /// <param name="combineImages">合并图片标识</param>
        /// <returns>Jpeg文件路径</returns>
        public static string Tif2Jpeg(string tifFilePath, int imgQuality, Boolean combineImages)
        {
            try
            {
                int                  len          = tifFilePath.LastIndexOf(".tif");
                string               fileName2    = tifFilePath.Substring(0, len);
                string               filePath     = fileName2.Substring(0, fileName2.LastIndexOf('\\') + 1);
                FileStream           stream       = File.OpenRead(tifFilePath);
                Bitmap               bmp          = new Bitmap(stream);
                System.Drawing.Image image        = bmp;
                Guid                 objGuid      = image.FrameDimensionsList[0];
                FrameDimension       objDimension = new FrameDimension(objGuid);
                int                  totFrame     = image.GetFrameCount(objDimension);
                int                  count        = totFrame;

                if (count <= 0)
                {
                    bmp.Dispose();
                    return("");
                }

                Bitmap   resultImg      = null;
                Graphics resultGraphics = null;
                int      imageHeight;
                for (int i = 0; i < totFrame; i++)//循环生成多张图片
                {
                    image.SelectActiveFrame(objDimension, i);

                    //合并图片
                    if (combineImages)
                    {
                        if (resultImg == null)
                        {
                            imageHeight = image.Height;

                            //resultImg = new Bitmap(tempImage.Width, tempImage.Height * imgCount);
                            resultImg      = new Bitmap(792, 1120 * totFrame);
                            resultGraphics = Graphics.FromImage(resultImg);
                        }

                        if (i == 0)
                        {
                            resultGraphics.DrawImage(image, 0, 0);
                        }
                        else
                        {
                            resultGraphics.DrawImage(image, 0, 1120 * i);
                            //resultGraphics.DrawImage(tempImage, 0, imageHeight * i);
                        }
                    }

                    //保存图片
                    ImageUtility.CompressAsJPG(new Bitmap(image), filePath + "\\" + i + ".jpg", imgQuality);
                    //image.Save(filePath + "\\" + i + ".jpg", ImageFormat.Jpeg);
                }

                string imgFilePath = string.Empty;

                if (combineImages)
                {
                    imgFilePath = tifFilePath.Replace(".tif", ".jpg");
                    //ImageUtility.ThumbAsJPG(resultImg, result, (int)(resultImg.Width * 0.4), (int)(resultImg.Height * 0.4), quality);
                    ImageUtility.CompressAsJPG(resultImg, imgFilePath, imgQuality);
                    resultGraphics.Dispose();
                }
                else
                {
                    imgFilePath = filePath + "\\0.jpg";
                }
                bmp.Dispose();
                image.Dispose();
                stream.Close();
                //File.Delete(tifFilePath);
                return(imgFilePath);
            }
            catch (Exception e)
            {
                return(e.Message);
            }
        }