html JavaScript html5 easeljs bitmap 图片宽高设置

JavaScript015

html JavaScript html5 easeljs bitmap 图片宽高设置,第1张

中文API没有,自己去官网看英文的吧,英文也不难看懂。

设置宽高用scaleX,scaleY属性。

==================================

img.getBounds()获取img的外界矩形,相对于img的本地坐标(不是img的父容器坐标,这点和flash中不一样)。

img.setbounds(0,0,100,100)4个参数分别是x/y/width/height

其实setbounds不应该这么用,官方API中说的清楚,这个方法是为那些无法getBounds的对象用的,比如Shape(矢量图,用了graphics)/Text等。

举个例子:

var circle=new createjs.Shape()

 circle.graphics.beginFill("#00ff00")

    circle.graphics.drawCircle(0,0,40)

    stage.addChild(circle)

    console.log(circle.getBounds())

上面这段代码console.log(circle.getBounds())会输出null。也就是说createjs引擎没有实现Shape计算外接矩形的方法。(事实上这个方法实现起来确实很麻烦,也很影响效率)。这个方法有时候很有用,所以createjs提供了一个setbounds()方法,让用户自己设置原始的外接矩形。

======================================

总结:

1、  createjs中,设置宽高应该用scaleX /scaleY属性。

2、如果用到了graphics或Text,还要用getBounds(),画完矢量图,或者设置完文字内容,需要自己调用TextField对象或者使用了的对象的setBounds()方法,手动设置对象的外接矩形。

===================================

题外话:

ActionScript3.0中设置width和height属性,其实最终也是设置的scaleX和ScaleY,只是引擎进行了封装而已。

一、添加引用

在解决方案上单击右键,选择“Add Reference...”,添加“System.Windows.Forms”,添加完后,Web.Config 中应该有类似下面的内容:

<system.web>

<compilation debug="true">

<assemblies>

<add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

二、 设置 STA 模式

在 @ Page 指令中加上 AspCompat="true",以强制该网页在 STA(单线程单元) 模式下执行。结果类似如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" AspCompat="true" %>

三、写截图代码

using System

using System.Collections

using System.Configuration

using System.Data

using System.Web

using System.Web.Security

using System.Web.UI

using System.Web.UI.HtmlControls

using System.Web.UI.WebControls

using System.Web.UI.WebControls.WebParts

using System.Drawing

using System.Drawing.Imaging

using System.Windows.Forms

public partial class _Default : System.Web.UI.Page

{

private WebBrowser _webBrowser

protected void Page_Load(object sender, EventArgs e)

{

string url = Request.QueryString["url"]

if (string.IsNullOrEmpty(url))

{

url = "http://www.cftea.com/"

}

_webBrowser = new WebBrowser()

_webBrowser.ScrollBarsEnabled = false//不显示滚动条

_webBrowser.Navigate(url)

_webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(Completed)

while (_webBrowser.ReadyState != WebBrowserReadyState.Complete)

{

System.Windows.Forms.Application.DoEvents()//避免假死,若去掉则可能无法触发 DocumentCompleted 事件。

}

}

public void Completed(object sender, WebBrowserDocumentCompletedEventArgs e)

{

//设置浏览器宽度、高度为文档宽度、高度,以便截取整个网页。

_webBrowser.Width = _webBrowser.Document.Body.ScrollRectangle.Width

_webBrowser.Height = _webBrowser.Document.Body.ScrollRectangle.Height

using (Bitmap bmp = new Bitmap(_webBrowser.Width, _webBrowser.Height))

{

_webBrowser.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height))

bmp.Save("C:\\Capture.png", ImageFormat.Png)

}

}

}