In this paper, we exploit the context information embodied in an image to develop a machine learning method called context ranking machine (CRM). Specifically, we leverage two kinds of context information: identity context and metric context. The identity context of an image patch refers to its origin (e.g., from which image it is cropped), and the metric context refers to its distance to the exact surrounding box of the target object inside the image. We use these context information in two ways. First, for object localization, instead of learning classifiers to separate the whole negative pool from all positives, we separate each positive from its own negatives sharing the same identity context. Second, we rank image patches according to their resemblance to the ground truth by establishing a connection between appearance based features and metric properties of the image. The CRM learns an image-based ranking algorithm via boosting and achieves an improved localization accuracy. We performed tests on echocardiogram images to localize heart chambers, and face images for eye band localization.