10

我在 VB.NET 中有这个代码段:

CType(pbImageHolder.Image, Bitmap).SetPixel(curPoint.X, curPoint.Y, Color.Purple)

C# 中的适当代码是什么?

4

4 回答 4

24

在 VB.NetCType(object, type)中,将对象转换为特定类型。

在 C# 中有两种方法可以实现这一点:

Bitmap image = pbImageHolder.Image as Bitmap;
image.SetPixel(curPoint.X, curPoint.Y, Color.Purple);

或者

Bitmap image = (Bitmap)(pbImageHolder.Image);
image.SetPixel(curPoint.X, curPoint.Y, Color.Purple);
于 2012-09-03T14:13:50.783 回答
9
((Bitmap)pbImageHolder.Image).SetPixel(curPoint.X, curPoint.Y, Color.Purple)
于 2012-09-03T14:13:42.717 回答
2

嗨,这是将 VB 转换为 C# 代码后的代码:

((Bitmap)pbImageHolder.Image).SetPixel(curPoint.X, curPoint.Y, Color.Purple);

如果你想从 VB 到 C# 的代码转换,反之亦然,请通过以下链接: http: //www.developerfusion.com/tools/convert/vb-to-csharp/

于 2012-09-11T10:40:51.897 回答
-1

我很惊讶关于这个主题的答案都不正确,所以我决定在这里发帖。您可以通过反编译 VB.NET 程序来验证发生了什么。答案取决于要转换为的类型。

对于参考类型:

Dim value = CType(x, ReferenceType)

var value = (ReferenceType)x;

对于结构:

Dim value = CType(x, StructType)

var value = (x != null) ? ((StructType)x) : default(StructType);

对于预定义的演员表:

Dim value = CDbl(x)

var value = Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(x)

看起来像这样:

internal static double ToDouble(object Value, NumberFormatInfo NumberFormat)
{
    if (Value == null)
    {
        return 0.0;
    }
    IConvertible convertible = Value as IConvertible;
    if (convertible != null)
    {
        switch (convertible.GetTypeCode())
        {
        case TypeCode.Boolean:
            if (Value is bool)
            {
                return 0 - (((bool)Value) ? 1 : 0);
            }
            return 0 - (convertible.ToBoolean(null) ? 1 : 0);
        case TypeCode.SByte:
            if (Value is sbyte)
            {
                return (sbyte)Value;
            }
            return convertible.ToSByte(null);
        case TypeCode.Byte:
            if (Value is byte)
            {
                return (int)(byte)Value;
            }
            return (int)convertible.ToByte(null);
        case TypeCode.Int16:
            if (Value is short)
            {
                return (short)Value;
            }
            return convertible.ToInt16(null);
        case TypeCode.UInt16:
            if (Value is ushort)
            {
                return (int)(ushort)Value;
            }
            return (int)convertible.ToUInt16(null);
        case TypeCode.Int32:
            if (Value is int)
            {
                return (int)Value;
            }
            return convertible.ToInt32(null);
        case TypeCode.UInt32:
            if (!(Value is uint))
            {
                return convertible.ToUInt32(null);
            }
            return (uint)Value;
        case TypeCode.Int64:
            if (Value is long)
            {
                return (long)Value;
            }
            return convertible.ToInt64(null);
        case TypeCode.UInt64:
            if (!(Value is ulong))
            {
                return convertible.ToUInt64(null);
            }
            return (ulong)Value;
        case TypeCode.Decimal:
            if (Value is decimal)
            {
                return convertible.ToDouble(null);
            }
            return Convert.ToDouble(convertible.ToDecimal(null));
        case TypeCode.Single:
            if (Value is float)
            {
                return (float)Value;
            }
            return convertible.ToSingle(null);
        case TypeCode.Double:
            if (Value is double)
            {
                return (double)Value;
            }
            return convertible.ToDouble(null);
        case TypeCode.String:
            return ToDouble(convertible.ToString(null), NumberFormat);
        }
    }
    throw new InvalidCastException(Utils.GetResourceString("InvalidCast_FromTo", Utils.VBFriendlyName(Value), "Double"));
}
于 2020-10-01T07:30:11.117 回答