Returns the color at a particular point on the canvas.
Syntax

[C#]

XColor GetColor(int inX, int inY);
XColor GetColor(XPoint inPoint);

[Visual Basic]

Function GetColor(inX As Integer, inY As Integer) As XColor
Function GetColor(inPoint As XPoint) As XColor
Params
Name Description
inX The horizontal coordinate at which you wish to sample the image color.
inY The vertical coordinate at which you wish to sample the image color.
inPoint The point at which you wish to sample the image color.
return The color at the point.
Notes

You may wish to find the color of the image at a particular point. Pass in a point and this function will return the color.

This operation is only valid for raster images. If the canvases are not raster based they will be rasterized.

See Also

XColor

Specifying Colors.

Example

[C#]

Canvas canvas = new Canvas(200, 200);
DrawOptions drawOpts = new DrawOptions(DrawOptions.ShapeDrawingType.Both);
drawOpts.PenColor = new XColor(Color.Red);
drawOpts.PaintColor = new XColor(Color.Red);
canvas.DrawRect(new XRect(0, 0, 100, 100), drawOpts);
drawOpts.PenColor = new XColor(Color.Green);
drawOpts.PaintColor = new XColor(Color.Green);
canvas.DrawRect(new XRect(100, 0, 100, 100), drawOpts);
drawOpts.PenColor = new XColor(Color.Blue);
drawOpts.PaintColor = new XColor(Color.Blue);
canvas.DrawRect(new XRect(0, 100, 100, 100), drawOpts);
drawOpts.PenColor = new XColor(Color.Yellow);
drawOpts.PaintColor = new XColor(Color.Yellow);
canvas.DrawRect(new XRect(100, 100, 100, 100), drawOpts);
canvas.SaveAs(Server.MapPath("getcolor.gif"));
Canvas theImage = new Canvas(Server.MapPath("getcolor.gif"));
XColor theColor = new XColor();
theColor = theImage.GetColor(new XPoint(50, 50));
Response.Write("Top Left Color is " + theColor.String + "<br>\r\n");
theColor = theImage.GetColor(new XPoint(150, 50));
Response.Write("Top Right Color is " + theColor.String + "<br>\r\n");
theColor = theImage.GetColor(new XPoint(50, 150));
Response.Write("Bottom Left Color is " + theColor.String + "<br>\r\n");
theColor = theImage.GetColor(new XPoint(150, 150));
Response.Write("Bottom Left Color is " + theColor.String + "<br>\r\n");


[Visual Basic]

Dim canvas As New Canvas(200, 200)
Dim drawOpts As New DrawOptions(DrawOptions.ShapeDrawingType.Both)
drawOpts.PenColor = New XColor(Color.Red)
drawOpts.PaintColor = New XColor(Color.Red)
canvas.DrawRect(New XRect(0, 0, 100, 100), drawOpts)
drawOpts.PenColor = New XColor(Color.Green)
drawOpts.PaintColor = New XColor(Color.Green)
canvas.DrawRect(New XRect(100, 0, 100, 100), drawOpts)
drawOpts.PenColor = New XColor(Color.Blue)
drawOpts.PaintColor = New XColor(Color.Blue)
canvas.DrawRect(New XRect(0, 100, 100, 100), drawOpts)
drawOpts.PenColor = New XColor(Color.Yellow)
drawOpts.PaintColor = New XColor(Color.Yellow)
canvas.DrawRect(New XRect(100, 100, 100, 100), drawOpts)
canvas.SaveAs(Server.MapPath("getcolor.gif"))
Dim theImage As New Canvas(Server.MapPath("getcolor.gif"))
Dim theColor As New XColor()
theColor = theImage.GetColor(New XPoint(50, 50))
Response.Write("Top Left Color is " & theColor.[String] & "<br>" & vbCr & vbLf)
theColor = theImage.GetColor(New XPoint(150, 50))
Response.Write("Top Right Color is " & theColor.[String] & "<br>" & vbCr & vbLf)
theColor = theImage.GetColor(New XPoint(50, 150))
Response.Write("Bottom Left Color is " & theColor.[String] & "<br>" & vbCr & vbLf)
theColor = theImage.GetColor(New XPoint(150, 150))
Response.Write("Bottom Left Color is " & theColor.[String] & "<br>" & vbCr & vbLf)


The graphics file and text output that this produces are shown below. Note that the names of colors map onto what might be described as a typical value rather than a 100% saturated value. For instance green is not a 100% saturated green with no other components (this looks rather garish) but a rather more typical green color.

Top Left Color is 86.7,3.1,2.4,0.0
Top Right Color is 0.0,39.2,6.7,0.0
Bottom Left Color is 0.0,0.0,83.1,0.0
Bottom Left Color is 98.8,95.3,2.0,0.0



getcolor.gif