.Net的PDF轉圖片

來源:互聯網
上載者:User

標籤:

 

用的是破解版的 O2S.Components.PDFRender4NET.dll 外掛程式, 簡單引用即可

    public static class PdfToImage    {        public static MemoryStream GetPdfImagePageStream(string pdfInputPath, int pageIndex, ImageFormat format, int width = 1600, int height = 2560, int quality = 10)        {            try            {                //pdf處理外掛程式                PDFFile pdfFile = PDFFile.Open(pdfInputPath);                int total = pdfFile.PageCount;                #region 防止異常參數                if (pageIndex < 0)                {                    pageIndex = 0;                }                if (pageIndex > total)                {                    pageIndex = total - 1;                }                if (quality < 1)                {                    quality = 1;                }                if (quality > 10)                {                    quality = 10;                }                if (width <= 0)                {                    width = 1;                }                if (height <= 0)                {                    height = 1;                }                #endregion                //pdf轉換圖片                SizeF pageSize = pdfFile.GetPageSize(pageIndex);                Bitmap pageImage = pdfFile.GetPageImage(pageIndex, 56 * quality);                MemoryStream ms = new MemoryStream();                pageImage.Save(ms, format);                //原圖                Image img = Image.FromStream(ms, true);                double ratio = (double)width / (double)height;                double oRatio = (double)img.Width / (double)img.Height;                int sbWidth = 0;                int sbHeight = 0;                int outX = 0;                int outY = 0;                if (oRatio < ratio)                {                    sbWidth = (int)(img.Width * ((double)height / (double)(img.Height)));                    sbHeight = height;                    outX = (width - sbWidth) / 2;                }                else                {                    sbHeight = (int)(img.Height * ((double)width / (double)(img.Width)));                    sbWidth = width;                    outY = (height - sbHeight) / 2;                }                //縮放                Image sbImg = new Bitmap(sbWidth, sbHeight);                Graphics sbGra = Graphics.FromImage(sbImg);                sbGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;                sbGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;                sbGra.Clear(Color.White);                sbGra.DrawImage(img, new System.Drawing.Rectangle(0, 0, sbWidth, sbHeight), new System.Drawing.Rectangle(0, 0, img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel);                //補白                Image outImg = new System.Drawing.Bitmap(width, height);                Graphics outGra = System.Drawing.Graphics.FromImage(outImg);                outGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;                outGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;                outGra.Clear(Color.White);                outGra.DrawImage(sbImg, new System.Drawing.Rectangle(outX, outY, sbWidth, sbHeight), new System.Drawing.Rectangle(0, 0, sbWidth, sbHeight), System.Drawing.GraphicsUnit.Pixel);                MemoryStream outMs = new MemoryStream();                outImg.Save(outMs, format);                sbImg.Dispose();                outImg.Dispose();                img.Dispose();                return outMs;            }            catch (Exception ex)            {            }            return new MemoryStream();        }        public static MemoryStream GetPdfImagePageStream(Stream stream, int pageIndex, ImageFormat format, int width = 1600, int height = 2560, int quality = 10)        {            try            {                //pdf處理外掛程式                PDFFile pdfFile = PDFFile.Open(stream);                int total = pdfFile.PageCount;                #region 防止異常參數                if (pageIndex < 0)                {                    pageIndex = 0;                }                if (pageIndex > total)                {                    pageIndex = total - 1;                }                if (quality < 1)                {                    quality = 1;                }                if (quality > 10)                {                    quality = 10;                }                if (width <= 0)                {                    width = 1;                }                if (height <= 0)                {                    height = 1;                }                #endregion                //pdf轉換圖片                SizeF pageSize = pdfFile.GetPageSize(pageIndex);                Bitmap pageImage = pdfFile.GetPageImage(pageIndex, 56 * quality);                MemoryStream ms = new MemoryStream();                pageImage.Save(ms, format);                //原圖                Image img = Image.FromStream(ms, true);                double ratio = (double)width / (double)height;                double oRatio = (double)img.Width / (double)img.Height;                int sbWidth = 0;                int sbHeight = 0;                int outX = 0;                int outY = 0;                if (oRatio < ratio)                {                    sbWidth = (int)(img.Width * ((double)height / (double)(img.Height)));                    sbHeight = height;                    outX = (width - sbWidth) / 2;                }                else                {                    sbHeight = (int)(img.Height * ((double)width / (double)(img.Width)));                    sbWidth = width;                    outY = (height - sbHeight) / 2;                }                //縮放                Image sbImg = new Bitmap(sbWidth, sbHeight);                Graphics sbGra = Graphics.FromImage(sbImg);                sbGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;                sbGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;                sbGra.Clear(Color.White);                sbGra.DrawImage(img, new System.Drawing.Rectangle(0, 0, sbWidth, sbHeight), new System.Drawing.Rectangle(0, 0, img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel);                //補白                Image outImg = new System.Drawing.Bitmap(width, height);                Graphics outGra = System.Drawing.Graphics.FromImage(outImg);                outGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;                outGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;                outGra.Clear(Color.White);                outGra.DrawImage(sbImg, new System.Drawing.Rectangle(outX, outY, sbWidth, sbHeight), new System.Drawing.Rectangle(0, 0, sbWidth, sbHeight), System.Drawing.GraphicsUnit.Pixel);                MemoryStream outMs = new MemoryStream();                outImg.Save(outMs, format);                sbImg.Dispose();                outImg.Dispose();                img.Dispose();                return outMs;            }            catch (Exception ex)            {            }            return new MemoryStream();        }    }
View Code

 

.Net的PDF轉圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.