TML即超文本標記語言,是目前應用最為廣泛的語言之一,是組成一個網頁的主要語言。在現今這個HTML5華麗麗地占領了整個互聯網的時候,如果想要通過網頁抓住瀏覽者的眼球光靠因循守舊是不行的,程序猿們需要掌握一些必須知道的HTML常用代碼,因為只有熟悉掌握了常用的HTML代碼,程序猿們在編寫網頁的時候才可以做到流暢美觀,用細膩的思維和創意的細節效果打動并留住網頁瀏覽者。
html+css代碼
文本設置
1、font-size: 字號參數
2、font-style: 字體格式
3、font-weight: 字體粗細
4、顏色屬性
color: 參數
注意使用網頁安全色
超鏈接設置
text-decoration: 參數
主要用途是改變瀏覽器顯示文字鏈接時的下劃線。
參數取值范圍:
underline:為文字加下劃線
overline:為文字加上劃線
line-through:為文字加刪除線
blink:使文字閃爍
none:不顯示上述任何效果
背景
1、背景顏色
background-color: 參數
2、背景圖片
background-image: url(URL)
URL就是背景圖片的存放路徑,none表示無。
3、背景圖片重復
background-repeat: 參數
參數取值范圍 :
no-repeat:不重復平鋪背景圖片
repeat-x:使圖片只在水平方向上平鋪
repeat-y:使圖片只在垂直方向上平鋪
如果不指定背景圖片重復屬性,瀏覽器默認的是背景圖片向水平、垂直兩個方向上平鋪。
4、背景圖片固定
背景圖片固定控制背景圖片是否隨網頁的滾動而滾動。如果不設置背景圖片固定屬性,瀏覽器默認背景圖片隨網頁的滾動而滾動。為了避免過于花哨的背景圖片在滾動時轉移瀏覽者的注意力,一般都設為固定
background-attachment: 參數
參數取值范圍:
fixed:網頁滾動時,背景圖片相對于瀏覽器的窗口而言,固定不動
scroll:網頁滾動時,背景圖片相對于瀏覽器的窗口而言,一起滾動
區塊
1、單詞間距
word-spacing: 間隔距離
2、字母間距
letter-spacing: 字母間距
3、文本對齊
text-align: 參數
參數的取值:
left:左對齊
right:右對齊
center:居中對齊
justify:相對左右對齊
4、垂直對齊
vertical-align: 參數
top:頂對齊
bottom:底對齊
text-top:相對文本頂對齊
text-bottom:相對文本底對齊
baseline:基準線對齊
middle:中心對齊
sub:以下標的形式顯示
super:以上標的形式顯示
5、文本縮進
text-indent: 縮進距離
12px相當于一個文字距離
6、空格
white-space: 參數
normal 正常
pre 保留
nowrap 不換行
7、顯示樣式
display: 參數
參數取值范圍:
block:塊級元素,在對象前后都換行
inline:在對象前后都不換行
list-item:在對象前后都換行,增加了項目符號
none:無顯示
方框
1、height 高度
2、width 寬度
3、padding 內邊距
4、margin 外邊距
5、float(浮動):可以讓塊級元素在一行中排列,例如橫向菜單。
6、clear 清除浮動
邊框
1、樣式
border style 參數
邊框樣式的參數:
none:無邊框
dotted:邊框為點線
dashed:邊框為長短線
solid:邊框為實線
double:邊框為雙線
2、寬度
border width 參數
3、顏色
border color 參數
列表
list-style-type 列表樣式
不同瀏覽器的列表符可能不相同,可能會影響到網頁,所以網頁中的列表大多都是由背景圖片顯示。
控制用戶界面的樣式
鼠標
cursor:鼠標形狀參數
CSS鼠標形狀參數表:
鼠標形狀:CSS代碼
style="cursor:hand" 手形
style="cursor:crosshair" 十字形
style="cursor:text" 文本形
style="cursor:wait" 沙漏形
style="cursor:move" 十字箭頭形:
style="cursor:help" 問號形
style="cursor:e-resize" 右箭頭形
style="cursor:n-resize" 上箭頭形
style="cursor:nw-resize" 左上箭頭形
style="cursor:w-resize" 左箭頭形
style="cursor:s-resize" 下箭頭形
style="cursor:se-resize" 右下箭頭形
style="cursor:sw-resize" 左下箭頭形
HTML常用代碼之:結構性定義
文件類型<HTML></HTML> (放在檔案的開頭與結尾)
文件主題<TITLE></TITLE> (必須放在「文頭」區塊內)
文頭<HEAD></HEAD> (描述性資料,像是「主題」)
文體<BODY></BODY> (文件本體)
標題<H?></H?> (從1到6,有六層選擇)
標題的對齊 <H?ALIGN=LEFT|CENTER|RIGHT></H?>
區分<DIV></DIV>
區分的對齊 <DIVALIGN=LEFT|RIGHT|CENTER|JUSTIFY></DIV>
引文區塊<BLOCKQUOTE></BLOCKQUOTE> (通常會內縮)
強調<EM></EM> (通常會以斜體顯示)
特別強調<STRONG></STRONG> (通常會以加粗顯示)
引文<CITE></CITE> (通常會以斜體顯示)
碼<CODE></CODE> (顯示原始碼之用)
樣本<SAMP></SAMP>
鍵盤輸入<KBD></KBD>
變數<VAR></VAR>
定義<DFN></DFN> (有些瀏覽器不提供)
地址 <ADDRESS></ADDRESS>
大字<BIG></BIG>
小字<SMALL></SMALL>
與外觀相關的標簽(作者自訂的表現方式)
加粗<B></B>
斜體<I></I>
底線<U></U> (尚有些瀏覽器不提供)
刪除線<S></S> (尚有些瀏覽器不提供)
下標<SUB></SUB>
上標<SUP></SUP>
打字機體<TT></TT> (用單空格字型顯示)
預定格式<PRE></PRE> (保留文件中空格的大小)
預定格式的寬度<PRE WIDTH=?></PRE>(以字元計算)
向中看齊<CENTER></CENTER> (文字與圖片都可以)
閃耀<BLINK></BLINK> (有史以來最被嘲弄的標簽)
字體大小 <FONTSIZE=?></FONT>(從1到7)
改變字體大小 <FONTSIZE=+|-?></FONT>
基本字體大小 <BASEFONTSIZE=?> (從1到7; 內定為3)
字體顏色 <FONTCOLOR="#$$"></FONT>($$為顏色代碼)
HTML常用代碼之:修改頁面的實用性HTML代碼
貼圖:<img src="圖片地址">
加入連接:<a href="所要連接的相關地址">寫上你想寫的字</a>
在新窗口打開連接:<a href="相關地址" target="_blank">寫上要寫的字</a>
移動字體(走馬燈):<marquee>寫上你想寫的字</marquee>
字體加粗:<b>寫上你想寫的字</b>
字體斜體:<i>寫上你想寫的字</i>
字體下劃線: <u>寫上你想寫的字</u>
字體刪除線: <s>寫上你想寫的字</s>
字體加大: <big>寫上你想寫的字</big>
字體控制大小:<h1>寫上你想寫的字</h1> (其中字體大小可從h1-h5,h1最大,h5最小)
更改字體顏色:<font color="#value">寫上你想寫的字</font>(其中value值在000000與ffffff(16位進制)之間
消除連接的下劃線:<a href="相關地址" style="text-decoration:none">寫上你想寫的字</a>
貼音樂:<embed src="音樂地址" width="寬度" height="高度" autostart=false>
貼flash: <embed src="flash地址" width="寬度" height="高度">
貼影視文件:<img dynsrc="文件地址" width="寬度" height="高度" start=mouseover>
換行:<br>
段落:<p>段落</p>
原始文字樣式:<pre>正文</pre>
換帖子背景:<body background="背景圖片地址">
固定帖子背景不隨滾動條滾動:<body background="背景圖片地址" body bgproperties=fixed>
定制帖子背景顏色:<body bgcolor="#value">(value值見10)
帖子背景音樂:<bgsound="背景音樂地址" loop=infinite>
貼網頁:<iframe. src="相關地址" width="寬度" height="高度"></iframe>
HTML常用代碼之:常常會遇到的問題
點擊關閉窗口
<a href="javascript.:top.window.close();">點擊關閉窗口</a>!
請問如何去掉主頁右面的滾動條?
<body scroll="no">
<body style="overflow-y:hidden">
如何做到讓一個網頁自動關閉.
<html>
<head>
<OBJECT id=closes type="application/x-oleobject" classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">
<param name="Command" value="Close">
</object>
</head>
<body >
這個窗口會在10秒過后自動關閉,而且不會出現提示. </body>
如何在不刷新頁面的情況下刷新css?
<style>
button{ color:#000000;}
</style>
<button nclick=document.styleSheets[0].rules[0].style.color=‘‘‘‘red‘‘‘‘>點擊按鈕直接修改style標簽里button選擇符使按鈕改為 紅色</button>
請問如何讓網頁自動刷新?
在head部記入<META. HTTP-EQUIV="Refresh" c>其中20為20秒后自動刷新,你可以更改為任意值。
如何讓頁面自動刷新?
方法一,用refresh
HTML 代碼片段如下:
<head>
<meta. http-equiv="refresh" c>
</head>
5表示刷新時間
[Ctrl+A 全部選擇 提示:你可先修改部分代碼,再按運行]
方法二,使用setTimeout控制
<img src=/logo.gif>
<script>
function rl(){
document.location.reload()
}
setTimeout(rl,2000)
</SCRIPT>
如何讓超鏈接沒有下劃線
在源代碼中的<HEAD>…</HEAD>之間輸入如下代碼:
<style. type="text/css"> <!--
a { text-decoration: none}
--> </style>
請問如何去掉IE的上下滾動條?
<body style=‘‘‘‘overflow:scroll;overflow-y:hidden‘‘‘‘>
</body>
怎樣才能把RealPlayer文件在網頁做一個試聽連接?
<embed height=25 src=51js.rm type=audio/x-pn-realaudio-plugin width=50 autostart="false" c>
如何用html實現瀏覽器上后退按鈕的功能?
<a href="java script.:history.go(-1)">點擊后退</a>
或者
<script> history.back() </SCRIPT>
HTML常用代碼之:你不一定知道的技巧
ncontextmenu="window.event.returnValue=false" 將徹底屏蔽鼠標右鍵
<table borderncontextmenu=return(false)><td>no</table> 可用于Table
<body nselectstart="return false"> 取消選取、防止復制
onpaste="return false" 不準粘貼
oncopy="return false;" ncut="return false;" 防止復制
<link rel="Shortcut Icon"href="favicon.ico"> IE地址欄前換成自己的圖標
<link rel="Bookmark"href="favicon.ico"> 可以在收藏夾中顯示出你的圖標
<inputstyle="ime-mode:disabled"> 關閉輸入法
永遠都會帶著框架
<script. language="JavaScript"><!--
if (window==top)top.location.href="frames.htm"; //frames.htm為框架網頁
// --></script>
防止被人frame.
<SCRIPT. LANGUAGE=JAVASCRIPT><!--
if (top.location !=self.location)top.location=self.location;
// --></SCRIPT>
網頁將不能被另存為
<noscript><iframe.src=*.html></iframe></noscript>
查看網頁源代碼
<input type=button value=查看網頁源代碼
onclick="window.location="view-source:"+"http://www.w3cschool.cn"">
刪除時確認
<a href="javascript:if(confirm("確實要刪除嗎?"))location="boos.asp? &areyou=刪除&page=1"">刪除</a>
屏蔽功能鍵Shift,Alt,Ctrl
<script>
function look(){
if(event.shiftKey)
alert("禁止按Shift鍵!");//可以換成ALT CTRL
}
document.onkeydown=look;
</script>
網頁不會被緩存
<META. HTTP-EQUIV="pragma" CONTENT="no-cache">
<META. HTTP-EQUIV="Cache-Control"CONTENT="no-cache, must-revalidate">
<META. HTTP-EQUIV="expires"CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
或者<META. HTTP-EQUIV="expires"CONTENT="0">
怎樣讓表單沒有凹凸感?
<input type=text style="border:1 solid #000000">
<input type=text style="border-left:none;border-right:none; border -top:none; border-bottom: 1 solid#000000"></textarea>
不要滾動條?
讓豎條沒有:
<body style="overflow:scroll;overflow-y:hidden">
</body>
讓橫條沒有:
<body style="overflow:scroll;overflow-x:hidden">
</body>
兩個都去掉?更簡單了
<body scroll="no">
</body>
怎樣去掉圖片鏈接點擊后,圖片周圍的虛線?
<a href="#"nFocus="this.blur()"><img src="logo.jpg"border=0></a>
電子郵件處理提交表單
<form. name="form1"method="post" action="mailt****@***.com"enctype="text/plain">
<input type=submit>
</form>
在打開的子窗口刷新父窗口的代碼里如何寫?
window.opener.location.reload()
如何設定打開頁面的大小
<body nload="top.resizeTo(300,200);">
打開頁面的位置<bodynload="top.moveBy(300,200);">
在頁面中如何加入不是滿鋪的背景圖片,拉動頁面時背景圖不動
<STYLE>
body
{background-image:url(logo.gif); background-repeat:no-repeat;
background-position:center;background-attachment: fixed}
</STYLE>
檢查一段字符串是否全由數字組成
<script. language="Javascript"><!--
function checkNum(str){return str.match(//D/)==null}
alert(checkNum("1232142141"))
alert(checkNum("123214214a1"))
// --></script>
獲得一個窗口的大小
document.body.clientWidth; document.body.clientHeight
怎么判斷是否是字符
if (/[^/x00-/xff]/g.test(s)) alert("含有漢字");
else alert("全是字符");
TEXTAREA自適應文字行數的多少
<textarea rows=1 name=s1 cols=27npropertychange="this.style.posHeight=this.scrollHeight">
</textarea>
日期減去天數等于第二個日期
<script. language=Javascript>
function cc(dd,dadd)
{
//可以加上錯誤處理
var a=new Date(dd)
a=a.valueOf()
a=a - dadd * 24 * 60 * 60 * 1000
a=new Date(a)
alert(a.getFullYear() + "年" + (a.getMonth() +1) + "月" + a.getDate() + "日")
} cc("12/23/2002",2)
</script>
選擇了哪一個Radio
<HTML><script. language="vbscript">
function checkme()
for each ob in radio1
if ob.checked then window.alert ob.value
next
end function
</script><BODY>
<INPUT name="radio1" type="radio"value="style" checked>Style.
<INPUT name="radio1" type="radio"value="barcode">Barcode
<INPUT type="button" value="check"nclick="checkme()">
</BODY></HTML>
1、CSS通常稱為CSS樣式或層疊樣式表,主要用于設置HTML頁面中的文本內容(字體、大小、 對其方式等)
2、圖片的外形(高寬、邊框樣式、邊距等)以及版面的布局等外觀顯示樣式
3、CSS可以是HTML頁面更好看,CSS色系的搭配可以讓用戶更舒服
4、CSS+DIV布局更佳靈活,更 容易繪制出用戶需要的結構
1、使用HTML時,需要遵從一定的規范,CSS也是如此有一定規范
2、語法解釋
"選擇器"用于指定CSS樣式作用的HTML對象
括號內是對該對象設 置的具體樣式
屬性和屬性值以鍵值對方式呈現,英文冒號":"分隔
多個屬性之間使用英文 分號";"
1、"選擇器"嚴格區分大小寫
2、"屬性"和"屬性值”不區分大小寫
3、多個屬性之間使用英文的分號隔開,最后一個屬性的分號可以省略(建議寫上便于閱讀)
4、為了提高CSS樣式閱讀加上注釋與CSS樣式的縮近排版
1、CSS使用非常靈活,可嵌入在HTML中
2、也可是一個單獨的文件,如果是單獨的文件, 必須.css為擴展名
1、行內樣式
1、行內樣式,是通過標簽的style屬性來設置元素的樣式
2、行內樣式通過標簽的屬性來控制樣式,沒有做到結構與表現(HTML結構、CSS顯示 )分離(少使用)
2、內部樣式(內嵌式)
1、CSS代碼集中卸載HTML文檔的<head>標簽體中,并且使用 <style>定義給當前html多個標簽設置樣式
2、內嵌式CSS樣式只對所在的HTML頁面有效,設計一個網頁使用時不錯的選擇,但是設計一個網站就不合適了,因為不能同時充分的發揮CSS代碼的重用型。
3、外部樣式
1、外部樣式又稱為鏈入式
2、將所有的樣式放在一個或多的外部樣式表文件中
3、通過<link>標簽將樣式連接到HTML檔中
4、優先級
1、行內樣式>內部樣式(內嵌式)>外部樣式
最近再做一個需求,需要對網頁生成預覽圖,如下圖
但是網頁千千萬,總不能一個個打開,截圖吧;于是想著能不能使用代碼來實現網頁的截圖。其實要實現這個功能,無非就是要么實現一個仿真瀏覽器,要么調用系統瀏覽器,再進行截圖操作。
void startPrintScreen(ScreenShotParam requestParam)
{
Thread thread=new Thread(new ParameterizedThreadStart(do_PrintScreen));
thread.SetApartmentState(ApartmentState.STA);
thread.Start(requestParam);
if (requestParam.Wait)
{
thread.Join();
FileInfo result=new FileInfo(requestParam.SavePath);
long minSize=1 * 1024;// 太小可能是空白圖,重抓
int maxRepeat=2;
while ((!result.Exists || result.Length <=minSize) && maxRepeat > 0)
{
thread=new Thread(new ParameterizedThreadStart(do_PrintScreen));
thread.SetApartmentState(ApartmentState.STA);
thread.Start(requestParam);
thread.Join();
maxRepeat--;
}
}
}
2、模擬瀏覽器WebBrowser
void do_PrintScreen(object param)
{
try
{
ScreenShotParam screenShotParam=(ScreenShotParam)param;
string requestUrl=screenShotParam.Url;
string savePath=screenShotParam.SavePath;
WebBrowser wb=new WebBrowser();
wb.ScrollBarsEnabled=false;
wb.ScriptErrorsSuppressed=true;
wb.Navigate(requestUrl);
logger.Debug("wb.Navigate");
DateTime startTime=DateTime.Now;
TimeSpan waitTime=new TimeSpan(0, 0, 0, 10, 0);// 10 second
while (wb.ReadyState !=WebBrowserReadyState.Complete)
{
Application.DoEvents();
if (DateTime.Now - startTime > waitTime)
{
wb.Dispose();
logger.Debug("wb.Dispose() timeout");
return;
}
}
wb.Width=screenShotParam.Left + screenShotParam.Width + screenShotParam.Left; // wb.Document.Body.ScrollRectangle.Width (避掉左右側的邊線);
wb.Height=screenShotParam.Top + screenShotParam.Height; // wb.Document.Body.ScrollRectangle.Height;
wb.ScrollBarsEnabled=false;
wb.Document.Body.Style="overflow:hidden";//hide scroll bar
var doc=(wb.Document.DomDocument) as mshtml.IHTMLDocument2;
var style=doc.createStyleSheet("", 0);
style.cssText=@"img { border-style: none; }";
Bitmap bitmap=new Bitmap(wb.Width, wb.Height);
wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
wb.Dispose();
logger.Debug("wb.Dispose()");
bitmap=CutImage(bitmap, new Rectangle(screenShotParam.Left, screenShotParam.Top, screenShotParam.Width, screenShotParam.Height));
bool needResize=screenShotParam.Width > screenShotParam.ResizeMaxWidth || screenShotParam.Height > screenShotParam.ResizeMaxWidth;
if (needResize)
{
double greaterLength=bitmap.Width > bitmap.Height ? bitmap.Width : bitmap.Height;
double ratio=screenShotParam.ResizeMaxWidth / greaterLength;
bitmap=Resize(bitmap, ratio);
}
bitmap.Save(savePath, System.Drawing.Imaging.ImageFormat.Gif);
bitmap.Dispose();
logger.Debug("bitmap.Dispose();");
logger.Debug("finish");
}
catch (Exception ex)
{
logger.Info($"exception: {ex.Message}");
}
}
3、截圖操作
private static Bitmap CutImage(Bitmap source, Rectangle section)
{
// An empty bitmap which will hold the cropped image
Bitmap bmp=new Bitmap(section.Width, section.Height);
//using (Bitmap bmp=new Bitmap(section.Width, section.Height))
{
Graphics g=Graphics.FromImage(bmp);
// Draw the given area (section) of the source image
// at location 0,0 on the empty bitmap (bmp)
g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
return bmp;
}
}
private static Bitmap Resize(Bitmap originImage, Double times)
{
int width=Convert.ToInt32(originImage.Width * times);
int height=Convert.ToInt32(originImage.Height * times);
return ResizeProcess(originImage, originImage.Width, originImage.Height, width, height);
}
public static string ScreenShotAndSaveAmazonS3(string account, string locale, Guid rule_ID, Guid template_ID)
{
//新的Template
var url=string.Format("https://xxxx/public/previewtemplate?showTemplateName=0&locale={0}&inputTemplateId={1}&inputThemeId=&Account={2}",
locale,
template_ID,
account
);
var tempPath=Tools.GetAppSetting("TempPath");
//路徑準備
var userPath=AmazonS3.GetS3UploadDirectory(account, locale, AmazonS3.S3SubFolder.Template);
var fileName=string.Format("{0}.gif", template_ID);
var fullFilePath=Path.Combine(userPath.LocalDirectoryPath, fileName);
logger.Debug("userPath: {0}, fileName: {1}, fullFilePath: {2}, url:{3}", userPath, fileName, fullFilePath, url);
//開始截圖,並暫存在本機
var screen=new Screen();
screen.ScreenShot(url, fullFilePath);
//將截圖,儲存到 Amazon S3
//var previewImageUrl=AmazonS3.UploadFile(fullFilePath, userPath.RemotePath + fileName);
return string.Empty;
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PrintScreen.Common
{
public class Screen
{
protected static NLog.Logger logger=NLog.LogManager.GetCurrentClassLogger();
public void ScreenShot(string url, string path
, int width=400, int height=300
, int left=50, int top=50
, int resizeMaxWidth=200, int wait=1)
{
if (!string.IsOrEmpty(url) && !string.IsOrEmpty(path))
{
ScreenShotParam requestParam=new ScreenShotParam
{
Url=url,
SavePath=path,
Width=width,
Height=height,
Left=left,
Top=top,
ResizeMaxWidth=resizeMaxWidth,
Wait=wait !=0
};
startPrintScreen(requestParam);
}
}
void startPrintScreen(ScreenShotParam requestParam)
{
Thread thread=new Thread(new ParameterizedThreadStart(do_PrintScreen));
thread.SetApartmentState(ApartmentState.STA);
thread.Start(requestParam);
if (requestParam.Wait)
{
thread.Join();
FileInfo result=new FileInfo(requestParam.SavePath);
long minSize=1 * 1024;// 太小可能是空白圖,重抓
int maxRepeat=2;
while ((!result.Exists || result.Length <=minSize) && maxRepeat > 0)
{
thread=new Thread(new ParameterizedThreadStart(do_PrintScreen));
thread.SetApartmentState(ApartmentState.STA);
thread.Start(requestParam);
thread.Join();
maxRepeat--;
}
}
}
void do_PrintScreen(object param)
{
try
{
ScreenShotParam screenShotParam=(ScreenShotParam)param;
string requestUrl=screenShotParam.Url;
string savePath=screenShotParam.SavePath;
WebBrowser wb=new WebBrowser();
wb.ScrollBarsEnabled=false;
wb.ScriptErrorsSuppressed=true;
wb.Navigate(requestUrl);
logger.Debug("wb.Navigate");
DateTime startTime=DateTime.Now;
TimeSpan waitTime=new TimeSpan(0, 0, 0, 10, 0);// 10 second
while (wb.ReadyState !=WebBrowserReadyState.Complete)
{
Application.DoEvents();
if (DateTime.Now - startTime > waitTime)
{
wb.Dispose();
logger.Debug("wb.Dispose() timeout");
return;
}
}
wb.Width=screenShotParam.Left + screenShotParam.Width + screenShotParam.Left; // wb.Document.Body.ScrollRectangle.Width (避掉左右側的邊線);
wb.Height=screenShotParam.Top + screenShotParam.Height; // wb.Document.Body.ScrollRectangle.Height;
wb.ScrollBarsEnabled=false;
wb.Document.Body.Style="overflow:hidden";//hide scroll bar
var doc=(wb.Document.DomDocument) as mshtml.IHTMLDocument2;
var style=doc.createStyleSheet("", 0);
style.cssText=@"img { border-style: none; }";
Bitmap bitmap=new Bitmap(wb.Width, wb.Height);
wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
wb.Dispose();
logger.Debug("wb.Dispose()");
bitmap=CutImage(bitmap, new Rectangle(screenShotParam.Left, screenShotParam.Top, screenShotParam.Width, screenShotParam.Height));
bool needResize=screenShotParam.Width > screenShotParam.ResizeMaxWidth || screenShotParam.Height > screenShotParam.ResizeMaxWidth;
if (needResize)
{
double greaterLength=bitmap.Width > bitmap.Height ? bitmap.Width : bitmap.Height;
double ratio=screenShotParam.ResizeMaxWidth / greaterLength;
bitmap=Resize(bitmap, ratio);
}
bitmap.Save(savePath, System.Drawing.Imaging.ImageFormat.Gif);
bitmap.Dispose();
logger.Debug("bitmap.Dispose();");
logger.Debug("finish");
}
catch (Exception ex)
{
logger.Info($"exception: {ex.Message}");
}
}
private static Bitmap CutImage(Bitmap source, Rectangle section)
{
// An empty bitmap which will hold the cropped image
Bitmap bmp=new Bitmap(section.Width, section.Height);
//using (Bitmap bmp=new Bitmap(section.Width, section.Height))
{
Graphics g=Graphics.FromImage(bmp);
// Draw the given area (section) of the source image
// at location 0,0 on the empty bitmap (bmp)
g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
return bmp;
}
}
private static Bitmap Resize(Bitmap originImage, Double times)
{
int width=Convert.ToInt32(originImage.Width * times);
int height=Convert.ToInt32(originImage.Height * times);
return ResizeProcess(originImage, originImage.Width, originImage.Height, width, height);
}
private static Bitmap ResizeProcess(Bitmap originImage, int oriwidth, int oriheight, int width, int height)
{
Bitmap resizedbitmap=new Bitmap(width, height);
//using (Bitmap resizedbitmap=new Bitmap(width, height))
{
Graphics g=Graphics.FromImage(resizedbitmap);
g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
g.DrawImage(originImage, new Rectangle(0, 0, width, height), new Rectangle(0, 0, oriwidth, oriheight), GraphicsUnit.Pixel);
return resizedbitmap;
}
}
}
class ScreenShotParam
{
public string Url { get; set; }
public string SavePath { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public int Left { get; set; }
public int Top { get; set; }
/// <summary>
/// 長邊縮到指定長度
/// </summary>
public int ResizeMaxWidth { get; set; }
public bool Wait { get; set; }
}
}
完成,達到預期的效果。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。