Found a very strange behaviour with Sitecore image control, take the following code
1 |
<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
1 |
<img src="/~/media/Images/websitename/Images/1.ashx?h=120&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 asp.net 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
1 |
<sc:Image runat="server" ID="img"/> |
then in the code behind
1 2 3 |
img.Item = item; img.Field = <span class="str">"Big Image"</span>; img.Parameters = <span class="str">"w=200&h=300"</span>; |
and here’s the result
1 |
<img src="/~/media/Images/Website/Images/1.ashx?h=300&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
unfortunately the version of sitecore that I’m using at that time was 7.2 so the security feature that was implemented for Sitecore 7.5 and above wasn’t the cause of it