html5绘制标尺,html5画布创建标尺

我只是需要做同样的事情网页和管理的最终设计出一个简单的算法来实现任务:

代码背后:

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

{

protected void Page_Load(object sender, EventArgs e)

{

int RulerLengthMM = Int32.Parse(Request.QueryString["LenMM"]);

int RulerLengthPixels = Int32.Parse(Request.QueryString["LenPx"]);

int RulerHeightPixels = Int32.Parse(Request.QueryString["HeightPx"]);

string sBC = Request.QueryString["BColour"];

string sFC = Request.QueryString["FColour"];

double dConversionFactor = (float)RulerLengthPixels/(float)RulerLengthMM;

double dTolerance = 0.5;

int iTextCount = 0;

Bitmap b = new Bitmap(RulerLengthPixels, RulerHeightPixels);

// Set to background colour

for (int x = 0; x < RulerLengthPixels; x++)

{

for (int y = 0; y < RulerHeightPixels; y++)

{

b.SetPixel(x, y, Color.FromName(sBC));

}

}

for (int x = 0; x < RulerLengthPixels; x++)

{

b.SetPixel(x, 0, Color.FromName(sFC));

if (x % (dConversionFactor * 10) <= dTolerance || x % (dConversionFactor * 10) >= (dConversionFactor * 10) - dTolerance)

{

// Do this every 10 mm

for (int y = 0; y < RulerHeightPixels*0.7; y++)

{

b.SetPixel(x, y, Color.FromName(sFC));

}

// Add text

string sText = iTextCount++.ToString();

var gr = Graphics.FromImage(b);

gr.DrawString(sText, SystemFonts.DefaultFont, Brushes.Black, new Point(x, (int)(RulerHeightPixels * 0.5)));

}

if (x % (dConversionFactor * 10) == (dConversionFactor * 5) || (x % (dConversionFactor * 10) >= (dConversionFactor * 5) - dTolerance && x % (dConversionFactor * 10) <= (dConversionFactor * 5) + dTolerance))

{

// Do this every 5 mm

for (int y = 0; y < RulerHeightPixels/2; y++)

{

b.SetPixel(x, y, Color.FromName(sFC));

}

}

if (x % (dConversionFactor) <= dTolerance || x % (dConversionFactor) >= dConversionFactor-dTolerance)

{

// Do this every 1 mm

for (int y = 0; y < RulerHeightPixels/10; y++)

{

b.SetPixel(x, y, Color.FromName(sFC));

}

}

}

// Convert the image to byte[]

System.IO.MemoryStream stream = new System.IO.MemoryStream();

b.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);

byte[] imageBytes = stream.ToArray();

// Convert byte[] to Base64 String

string base64String = Convert.ToBase64String(imageBytes);

// set img tag src

imgTest.Src = "data:image/gif;base64," +base64String;

imgTest.Width = RulerLengthPixels;

imgTest.Height = RulerHeightPixels;}}

.aspx的代码:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值