Heres a bitmap image saving code snippet in vb.net. Can somebody please help me understand why am I getting an error here stating that i am trying to convert string to double while saving the image. how to correct that?
下图是vb.net中的位图图像保存代码片段。有人可以帮我理解为什么我在这里得到一个错误,说明我在保存图像时尝试将字符串转换为double。怎么纠正?
Private Sub Timer5_Tick(sender As System.Object, e As System.EventArgs) Handles Timer5.Tick
x = MyRandomNumber.Next(1000)
screenWidth = Screen.GetBounds(New Point(0, 0)).Width
screenHeight = Screen.GetBounds(New Point(0, 0)).Height
Dim bmpScreenShot As New Bitmap(screenWidth, screenHeight)
Dim gfx As Graphics = Graphics.FromImage(bmpScreenShot)
gfx.CopyFromScreen(0, 0, 0, 0, New Size(screenWidth, screenHeight))
***bmpScreenShot.Save("D:\\screenshots\\" + x + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)***
End Sub
3 个解决方案
#1
3
"D:\\screenshots\\"
is as string. x
is a double.
“D:\\ screenshots \\”是字符串。 x是双倍的。
You try to add "D:\\screenshots\\"
and x
, and this fails, because "D:\\screenshots\\"
is not a double.
您尝试添加“D:\\ screenshots \\”和x,这会失败,因为“D:\\ screenshots \\”不是双倍的。
That's what the compiler tries to tell you.
这就是编译器试图告诉你的。
Have a look at the documentation of the +
operator:
看一下+运算符的文档:
In general, + performs arithmetic addition when possible, and concatenates only when both expressions are strings.
通常,+尽可能执行算术加法,并且仅当两个表达式都是字符串时才连接。
Data types of expressions:
表达式的数据类型:
Object expression holds a numeric value and the other is of type String
对象表达式包含数值,另一个包含String类型
Action by compiler:
编译器的行动:
If Option Strict is On, then generate a compiler error.
如果Option Strict为On,则生成编译器错误。
If Option Strict is Off, then implicitly convert the String to Double and add.
如果Option Strict为Off,则隐式将String转换为Double并添加。
If the String cannot be converted to Double, then throw an InvalidCastException exception.
如果String无法转换为Double,则抛出InvalidCastException异常。
To concatenate strings use the &
operator:
要连接字符串,请使用&运算符:
Generates a string concatenation of two expressions.
生成两个表达式的字符串连接。
... "D:\\screenshots\\" & x & ".jpg"...
or String.Format
:
String.Format("D:\\screenshots\\{0}.jpg", x)
Lesson learned:
Always use Option Strict On
, and always look up the documentation.
始终使用Option Strict On,并始终查找文档。
#2
1
bmpScreenShot.Save("D:\\screenshots\\" + x.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
#3
1
Use & instead of + to concatenate the path:
使用&而不是+来连接路径:
bmpScreenShot.Save("D:\\screenshots\\" & x & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
#1
3
"D:\\screenshots\\"
is as string. x
is a double.
“D:\\ screenshots \\”是字符串。 x是双倍的。
You try to add "D:\\screenshots\\"
and x
, and this fails, because "D:\\screenshots\\"
is not a double.
您尝试添加“D:\\ screenshots \\”和x,这会失败,因为“D:\\ screenshots \\”不是双倍的。
That's what the compiler tries to tell you.
这就是编译器试图告诉你的。
Have a look at the documentation of the +
operator:
看一下+运算符的文档:
In general, + performs arithmetic addition when possible, and concatenates only when both expressions are strings.
通常,+尽可能执行算术加法,并且仅当两个表达式都是字符串时才连接。
Data types of expressions:
表达式的数据类型:
Object expression holds a numeric value and the other is of type String
对象表达式包含数值,另一个包含String类型
Action by compiler:
编译器的行动:
If Option Strict is On, then generate a compiler error.
如果Option Strict为On,则生成编译器错误。
If Option Strict is Off, then implicitly convert the String to Double and add.
如果Option Strict为Off,则隐式将String转换为Double并添加。
If the String cannot be converted to Double, then throw an InvalidCastException exception.
如果String无法转换为Double,则抛出InvalidCastException异常。
To concatenate strings use the &
operator:
要连接字符串,请使用&运算符:
Generates a string concatenation of two expressions.
生成两个表达式的字符串连接。
... "D:\\screenshots\\" & x & ".jpg"...
or String.Format
:
String.Format("D:\\screenshots\\{0}.jpg", x)
Lesson learned:
Always use Option Strict On
, and always look up the documentation.
始终使用Option Strict On,并始终查找文档。
#2
1
bmpScreenShot.Save("D:\\screenshots\\" + x.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
#3
1
Use & instead of + to concatenate the path:
使用&而不是+来连接路径:
bmpScreenShot.Save("D:\\screenshots\\" & x & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)