Sitecore Image Parameters

Found a very strange behaviour with Sitecore image control,  take the following code

<sc:Image runat="server" ID="img" Height="200" Width="300"/>

i would expect that would render the img tag with the correct height and width, but instead it renders out this

<img src="/~/media/Images/websitename/Images/1.ashx?h=120&amp;w=160" alt="basics" width="160" height="120">

it overrides the height and width that i set and take the image height and width.. odd indeed

luckily we have a way to overcome this without having to use custom control or having to resort to image control – we would like the support the page editor functionality.

Set the parameters explicitly

That’s right,  we need to set the parameters explicitly so that it renders out the correct value.

First, remove the Height and Width attribute from the markup

<sc:Image runat="server" ID="img"/>

then in the code behind

img.Item = item;
img.Field = "Big Image";
img.Parameters = "w=200&h=300";

and here’s the result

<img src="/~/media/Images/Website/Images/1.ashx?h=300&amp;w=200" alt="basics" width="200" height="300">

Just like what i wanted.

If you’re wondering what other parameters are supported by the image control, you could find that in the Sitecore SDN


2 thoughts on “Sitecore Image Parameters

  1. Pingback: Images not Resizing in Sitecore 7.5, Sitecore 8.0 | Sean Holmesby

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s