问题描述
在我的项目中,我将一些vb.net转换为c#,我来到了这一行:
int thisdigit = Fix(countervalue / (Math.Pow(10,(numdigits - j - 1)))) - Fix(countervalue / (Math.Pow(10,(numdigits - j)))) * 10;
但是我得到了错误:
The name 'Fix' does not exist in the current context
我该如何解决?我不明白为什么Fix()不存在。
但是,如果我改用Math.Truncate()
,那是行不通的,因为thisdigit
是int
。
我该如何更改?
这是我原始的vb.net代码:
dim dg as int
dg = Fix(value / (10 ^ (digits - j - 1))) - Fix(value / (10 ^ (digits - j))) * 10
这里是我要转换的内容的链接: https://www.developerfusion.com/code/3734/aspnet-graphical-page-hit-counter/
该代码在我的vb.net项目中有效。我已经通过调试器运行了转换后的代码,唯一能看到任何问题的地方就是此行。
我也是这样想的:
double thisdigit = Math.Truncate((double)(countervalue / (10 ^ (numdigits - j - 1)))) - Math.Truncate(((double)(countervalue / (10 ^ (numdigits - j))) * 10));
解决方法
我真的不明白为什么这么复杂。
原始代码似乎要画一个计数器,一次一位:
dim j as Integer,dg as Integer
for j = 0 to (digits-1)
' Extract digit from value
dg = fix(value / (10^(digits - j - 1))) - fix(value / (10^(digits - j)))*10
' Add digit to the output graphic
g.drawimage(i,New rectangle(j*dgwidth,dgwidth,dgheight),New rectangle(dg*dgwidth,GraphicsUnit.Pixel)
next j
但是可以肯定,做这样的事情会更容易:
int pageCounter = 7234283;
string toDraw = pageCounter.ToString();
for(int i = 0; i < toDraw.Length; i++)
someGraphics.DrawString(toDraw.Substring(i,1),someFont,someBrush,new PointF(i * 10.0f,0));
或者也许:
int pageCounter = 7234283;
string toDraw = pageCounter.ToString();
PointF p = new PointF(0.0f,0.0f);
foreach(char c in toDraw){
someGraphics.DrawString(c.ToString(),p);
p.X += 10.0f;
}
,
在C#Discord的Aidy的帮助下,我终于做到了。非常简单干净。
//Get the number of digits to display in the output graphic
//If the countervalue is 16 then "16".ToString("D5") converts it to "00016".
//ToCharArray() turns that into an array of characters ['0','0','1','6'].
//We loop through that list and convert the char back to int and we get 0,1 and 6.
//Thanks to @Aidy in the C# Discord for help on this
int numdigits = Convert.ToInt32(Request.QueryString["digits"]);
var digits = countervalue.ToString("D" + numdigits.ToString()).ToCharArray();
//Create an output object
Bitmap imageoutput = new Bitmap(digitwidth * digits.Length,digitheight,PixelFormat.Format24bppRgb); //should be 5*15 = 75 for digits.gif
Graphics graphic = Graphics.FromImage(imageoutput); //here is our black box
//digits.gif is 150 x 20px;
//So,if our countervalue = 16,and numdigits = 5,we want to display 00016.
for(int j = 0; j < digits.Length; j++) {
//We loop through that digits and convert the char back to int and we get 0,1 and 6.
int thisdigitX = int.Parse(digits[j].ToString());
//add the digit to the output graphic
graphic.DrawImage(digitpix,new Rectangle(j * digitwidth,digitwidth,digitheight),new Rectangle(thisdigitX * digitwidth,GraphicsUnit.Pixel);
}
但是,我还能够获得原始代码,以进行int和double的转换以及导入Visual Basic程序集。
using Microsoft.VisualBasic;
int thisdigitX = Conversion.Fix(countervalue / ((int)Math.Pow(10,(double)(numdigits - j - 1)))) - ( Conversion.Fix(countervalue / ((int)Math.Pow(10,(double)(numdigits - j)))) * 10);
这里是我的github页面的链接,如果有人真的感兴趣,我会在其中张贴entire working project。 (不是在这里自我推广-只是分享;我不在乎是否有人使用它。)