I have this code segment in VB.NET:
CType(pbImageHolder.Image, Bitmap).SetPixel(curPoint.X, curPoint.Y, Color.Purple)
What is appropriate code in C#?
The VB to C# code converter from the SharpDevelop team is now a standalone extension to Visual Studio. Once installed, you can convert an entire VB.NET project to C# by opening the solution, right clicking the solution node in the Solution Explorer and selecting Convert to C#.
Although VB.Net and C# are very close cousins (syntax notwithstanding), there are a few key differences which will cause problems during conversion. These can be dealt with either before conversion or after. (Hello, compiler errors.)
CType is compiled inline, which means that the conversion code is part of the code that evaluates the expression. In some cases, the code runs faster because no procedures are called to perform the conversion.
CType or DirectCast CType Function returns the result of explicitly converting an expression to a specific data type, object, structure, class, or interface. CType(expression, typename)
In VB.Net CType(object, type)
casts an object to a specific type.
There are two ways to accomplish this in C#:
Bitmap image = pbImageHolder.Image as Bitmap;
image.SetPixel(curPoint.X, curPoint.Y, Color.Purple);
or
Bitmap image = (Bitmap)(pbImageHolder.Image);
image.SetPixel(curPoint.X, curPoint.Y, Color.Purple);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With