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.
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++;
}
}
}
}
// 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;
}