using System.IO; using System.Drawing; namespace WebApplication1 { //Asp.net将图片转为Base64编码 public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { System.Drawing.Image img = new Bitmap(Server.MapPath("~/test.jpg")); Response.Write("<img src='data:image/jpeg;base64," + ImageToBase(img, System.Drawing.Imaging.ImageFormat.Jpeg) + "' />"); } public string ImageToBase(System.Drawing.Image image, System.Drawing.Imaging.ImageFormat format) { using (MemoryStream ms = new MemoryStream()) { // Convert Image to byte[] image.Save(ms, format); byte[] imageBytes = ms.ToArray();
// Convert byte[] to Base64 String string base64String = Convert.ToBase64String(imageBytes); return base64String; } } } }
using System.IO; namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { protected List<FileNode> FileNodes = new List<FileNode>(); protected void Page_Load(object sender, EventArgs e) { string BootPath = Server.MapPath("~/"); if (!string.IsNullOrWhiteSpace(Request.QueryString["path"])) { BootPath = Request.QueryString["path"].ToString(); } FileNode LsFileNode = null; ; string[] getAll = Directory.GetDirectories(BootPath); if (getAll != null && getAll.Length > 0) { foreach (string path in getAll) { LsFileNode = new FileNode();
DirectoryInfo di = new DirectoryInfo(path); LsFileNode.Name = di.Name; LsFileNode.Path = Server.UrlEncode(path); LsFileNode.IsFile = false; FileNodes.Add(LsFileNode); }
} getAll = Directory.GetFiles(BootPath); if (getAll != null && getAll.Length > 0) { foreach (string path in getAll) { LsFileNode = new FileNode(); FileInfo fi = new FileInfo(path); LsFileNode.Name = fi.Name; LsFileNode.Path = Server.UrlEncode(path); LsFileNode.IsFile = true; FileNodes.Add(LsFileNode); }
} }
public class FileNode { public string Name { get; set; } public string Path { get; set; } public bool IsFile { get; set; } } } }
using HtmlAgilityPack; using System.Text; using System.IO; using System.Text.RegularExpressions; using System.Net; namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { HtmlWeb htmlWeb = new HtmlWeb(); htmlWeb.OverrideEncoding = Encoding.UTF8; HtmlAgilityPack.HtmlDocument doc = htmlWeb.Load(@"https://www.cnblogs.com/lori/p/4045633.html"); File.WriteAllText(Server.MapPath("~/Company/index.html"), doc.DocumentNode.OuterHtml); if (!Directory.Exists(Server.MapPath("~/Company/css/"))) { Directory.CreateDirectory(Server.MapPath("~/Company/css/")); } var links = doc.DocumentNode.SelectNodes("//link"); foreach (var link in links.ToArray()) { string cssfile = link.GetAttributeValue("href", ""); if (cssfile != "" && cssfile.Contains(".css")) { string fname = string.Empty; // string fontpath = string.Empty; if (cssfile.Contains("/")) { fontpath = cssfile.Substring(0, cssfile.LastIndexOf("/") + 1); } else { fontpath = cssfile; } WebClient wc = new WebClient(); string oo = wc.DownloadString(cssfile); if (cssfile.Contains("/")) { fname = cssfile.Substring(cssfile.LastIndexOf("/") + 1, cssfile.Length - cssfile.LastIndexOf("/") - 1); } else { fname = cssfile; } File.WriteAllText(Server.MapPath("~/Company/css/") + fname, oo);
string[] ss = oo.Split('}'); foreach (string item in ss) { if (item.Contains("@font-face")) { Regex rex = new Regex(@"url\((.+?)\)"); MatchCollection ms = rex.Matches(item); foreach (Match it in ms) {
String str1 = it.Groups[1].ToString(); if (str1.Contains("/")) { fname = str1.Substring(str1.LastIndexOf("/") + 1, str1.Length - str1.LastIndexOf("/") - 1); } else { fname = str1; } string f = fontpath + str1; wc = new WebClient(); wc.DownloadFile(f, Server.MapPath("~/Company/css/") + fname); } } } } }
} } }
using mshtml; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) {
} string pathstring = "0"; int level = 0; //该过程将被递归调用 //dom_node是当前的HTML DOM节点 //tree_node是当前插入树的结点 private void InsertDOMNodes(IHTMLDOMNode parentnode, TreeNode tree_node) {
int sibing = 0;//当前结点在兄弟结点之间的顺序,所有的结点之间通过","隔开 if (parentnode.hasChildNodes()) { //level++; //pathstring = pathstring +","+ level; IHTMLDOMChildrenCollection allchild = (IHTMLDOMChildrenCollection)parentnode.childNodes; int length = allchild.length; for (int i = 0; i < length; i++) { string instring = pathstring; instring = instring + "," + sibing++; IHTMLDOMNode child_node = (IHTMLDOMNode)allchild.item(i); TreeNode tempnode = tree_node.Nodes.Add(child_node.nodeName + "_" + instring); //string tmp = InsertDOMNodes(child_node, tempnode); pathstring = instring; } } }
private void eVIPSBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { pathstring = "0"; level = 0; DOMTreeView.Nodes.Clear(); IHTMLDocument3 HTMLDocument = (IHTMLDocument3)eVIPSBrowser.Document.DomDocument; IHTMLDOMNode rootDomNode = (IHTMLDOMNode)HTMLDocument.documentElement;
TreeNode root = DOMTreeView.Nodes.Add("HTML" + "_" + pathstring); InsertDOMNodes(rootDomNode, root); }
private void button1_Click(object sender, EventArgs e) { eVIPSBrowser.Navigate(this.textBox1.Text); }
} }