I've uploaded a few images of kittens, teddy bears, flowers, and other cute things to a folder within the Sitecore Media Library. I now need to access them using the ItemService API and display them in my ASP.NET page.
When I access these images by their ItemID, I get 200 (OK) responses, which JSON string containing all the data pertaining to these images. But I'm unable to figure out a way how to display these images within my page. Some of the code snippets I am trying out are the following:
To access the ItemService API:
var client = new WebClient();
client.Headers.Add(HttpRequestHeader.ContentType, "image/jpeg");
// I am able to get an HTTP 200 response with the proper JSON with the following line
client.DownloadFile((@"https://<mysite>.azurewebsites.net/sitecore/api/ssc/item/<my item guid>", @"cutePicture.json");
I even try the DownloadData
method of the WebClient
class and attempt to convert the returned byte array to and Image using multiple ways, but each time I get errors saying 'Invalid Parameter' and others.
var client = new WebClient();
client.Headers.Add(HttpRequestHeader.ContentType, "image/jpeg");
var bytes = client.DownloadData(@"https://<mysite>.azurewebsites.net/sitecore/api/ssc/item/<my item guid>");
using (MemoryStream ms = new MemoryStream(bytes))
{
Image myImage = Image.FromStream(ms); // Fails here
myImage.Save(@"MyCuteImage.jpg");
}
I replace the using
block with the following snippet, but that too doesn't let me convert the byte array to an image.
ImageConverter converter = new ImageConverter();
Image myImage = (Image)converter.ConvertFrom(bytes); // throws an error saying the parameter is invalid
myImage.Save(@"MyCuteImage.jpg");
Could someone please help me out here? Are there any settings/decorations I need to give my page to make Sitecore know that my page is accessing data from it? And how do I achieve my desired result, i.e., displaying a picture got from the Sitecore media library using the ItemService API onto my page? What am I missing here?
Any help with the above would be greatly appreciated!
Joonu