Search code examples
c#.netvb.netcasting

How do I translate VB.NET's CType() to C#?


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#?


Solution

  • 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);