If you want a specific feature which is not avaliable now to be supported by our product, please post it here. We’ll seriously consider adding it as a new feature in our future upgrades.

Thu Sep 12, 2019 2:31 pm

Hi,
We have performed URL to Image convertion using Spire.PDF. We are having several( circular, Datatable, etc) widgets in our URL. I need to get a particular widget from the page and i need to convert it to an seperate image in png format.

prism
 
Posts: 20
Joined: Thu Aug 15, 2019 11:16 am

Fri Sep 13, 2019 4:29 am

Hi,

Thanks for your inquiry.
Generally, our Spire.Pdf's image conversion function just can convert each page to an image. But I'd like to do further investigation for your requirement if you could share your url and your expected result. Look forward to your reply.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2445
Joined: Wed Jun 27, 2012 8:50 am

Fri Sep 13, 2019 6:12 am

Hi,
I am sharing my code in this post and Actual result, Expected Result also attached in this post. If it is possible or else any other possible way please let me know.

Code: Select all
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Spire.Pdf;
using Spire.Pdf.HtmlConverter.Qt;

namespace CoreApplication
{
    public class UrltoImage
    {
        public void Image()
        {
            List<string> Url = new List<string>();
            List<MemoryStream> image = new List<MemoryStream>();
            // we load more URL ( based on different widgets)
            Url.Add("https://rarunkumar454.github.io/sample_grid/");
            Url.Add("https://rarunkumar454.github.io/CircularWidget/");
            int i = 0;

            foreach (var item in Url)
            {
                MemoryStream ms = new MemoryStream();
                HtmlConverter.Convert(Url[i].ToString(), ms);

                string date = DateTime.Now.ToString("MM_dd_yyyy") + "Time_" + DateTime.Now.ToString("HHmmss");

                PdfDocument doc = new PdfDocument();
                doc.LoadFromStream(ms);
                Image bmp = doc.SaveAsImage(0);
                Image zoomImg = new Bitmap((int)(bmp.Size.Width * 2), (int)(bmp.Size.Height * 1.5));

                using (Graphics g = Graphics.FromImage(zoomImg))
                {
                    g.ScaleTransform(2.0f, 2.0f);
                    g.DrawImage(bmp, new Rectangle(new Point(0, 0), bmp.Size), new Rectangle(new Point(0, 0), bmp.Size), GraphicsUnit.Pixel);
                }
                //Image thumbnail = zoomImg.GetThumbnailImage(700, 400, null, IntPtr.Zero);
                zoomImg.Save($@"C:\Users\Downloads\Download\New folder\ResultImgThumb{date}[{i}].png", ImageFormat.Png);
                i++;
            }
        }
    }
}


Thanks

With Regards
Arunkumar R
You do not have the required permissions to view the files attached to this post.

prism
 
Posts: 20
Joined: Thu Aug 15, 2019 11:16 am

Fri Sep 13, 2019 9:26 am

Hi Arunkumar R,

Thanks for your file sharing.
After investigating, I regret to tell you that our Spire.Pdf can't extract the widgets in your url so it cannot convert the widgets to images.
But I tried a way to remove the white edges of the image. I shared my code and output image files here.
Code: Select all
 
// Calls CutImageWhitePart methond to remove the white edges of the image
 CutImageWhitePart(@"C:\Users\Downloads\Download\New folder\ResultImgThumb{date}[{i}].png", 0);
 public static void CutImageWhitePart(string FilePath, int WhiteBarRate)
        {
            Bitmap bmp = new Bitmap(FilePath);
            int top = 0, left = 0;
            int right = bmp.Width, bottom = bmp.Height;
            Color white = Color.White;
         
            for (int i = 0; i < bmp.Height; i++)
            {
                bool find = false;
                for (int j = 0; j < bmp.Width; j++)
                {
                    Color c = bmp.GetPixel(j, i);
                    if (IsWhite(c))
                    {
                        top = i;
                        find = true;
                        break;
                    }
                }
                if (find) break;
            }
       
            for (int i = 0; i < bmp.Width; i++)
            {
                bool find = false;
                for (int j = top; j < bmp.Height; j++)
                {
                    Color c = bmp.GetPixel(i, j);
                    if (IsWhite(c))
                    {
                        left = i;
                        find = true;
                        break;
                    }
                }
                if (find) break; ;
            }
           
            for (int i = bmp.Height - 1; i >= 0; i--)
            {
                bool find = false;
                for (int j = left; j < bmp.Width; j++)
                {
                    Color c = bmp.GetPixel(j, i);
                    if (IsWhite(c))
                    {
                        bottom = i;
                        find = true;
                        break;
                    }
                }
                if (find) break;
            }

            for (int i = bmp.Width - 1; i >= 0; i--)
            {
                bool find = false;
                for (int j = 0; j <= bottom; j++)
                {
                    Color c = bmp.GetPixel(i, j);
                    if (IsWhite(c))
                    {
                        right = i;
                        find = true;
                        break;
                    }
                }
                if (find) break;
            }
            int iWidth = right - left;
            int iHeight = bottom - left;
            int blockWidth = Convert.ToInt32(iWidth * WhiteBarRate / 100);
            bmp = Cut(bmp, left - blockWidth, top - blockWidth, right - left + 2 * blockWidth, bottom - top + 2 * blockWidth);
            if (bmp != null)
            {
                bmp.Save(bmp.GetHashCode().ToString() + ".jpg", ImageFormat.Jpeg);
            }
        }
        public static Bitmap Cut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
        {
            if (b == null)
            {
                return null;
            }
            int w = b.Width;
            int h = b.Height;
            if (StartX >= w || StartY >= h)
            {
                return null;
            }
            if (StartX + iWidth > w)
            {
                iWidth = w - StartX;
            }
            if (StartY + iHeight > h)
            {
                iHeight = h - StartY;
            }
            try
            {
                Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
                Graphics g = Graphics.FromImage(bmpOut);
                g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
                g.Dispose();
                return bmpOut;
            }
            catch
            {
                return null;
            }
        }
 
       
        public static bool IsWhite(Color c)
        {
            if (c.R < 245 || c.G < 245 || c.B < 245)
                return true;
            else return false;
        }


Sincerely,
Amy
E-iceblue support team
You do not have the required permissions to view the files attached to this post.
User avatar

amy.zhao
 
Posts: 2445
Joined: Wed Jun 27, 2012 8:50 am

Fri Sep 13, 2019 1:12 pm

Hi,
Thanks it's working perfectly.

With Regards
Arunkumar R

prism
 
Posts: 20
Joined: Thu Aug 15, 2019 11:16 am

Mon Sep 16, 2019 4:12 am

Hi,

Thanks for your feedback.
Welcome to write to us again if any issues.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2445
Joined: Wed Jun 27, 2012 8:50 am

Return to New Feature Requests