0

I'm having trouble consistently setting the width and height of an img tag. The img src is pulled dynamically from a database. The images being pulled from the database can have varying widths and heights so I need to set the img control. Sometimes the width and height are set properly and sometimes they aren't. I can't figure out why. Any help would be greatly appreciated. Thanks!

Here is what I'm doing now.

try
    {
        byte[] byteArray = GetImage();
        using (Bitmap bmp = ByteArrayToBitmap(byteArray))
        {
            //image is the img tag.
            image.Style["width"] = bmp.Width.ToString();
            image.Style["height"] = bmp.Height.ToString();
        }
    }
    catch (Exception ex)
    { }

EDIT: This appears to be a problem only in IE. Chrome and Firefox seem to be working fine. Also, when IE7 doesn't display the image with the proper height/width if the browser is refreshed it then displays properly. Sometimes...

9
  • Are the width and height values sometimes missing from the html? Or are then sometimes wrong? Commented Feb 7, 2009 at 16:21
  • Try putting some logging code in your catch block to see if you're getting an exception Commented Feb 7, 2009 at 16:23
  • Tighe-height and width values are set in the img tag properly but the image's height and width don't seem to actually be set to these values. Example, i have an image that is supposed to be wider than it is tall and this is in the html style="width:450;height:240;" but it's taller than it is long. Commented Feb 7, 2009 at 16:45
  • What's interesting about my previous comment is the last image loaded was taller than it was long... Commented Feb 7, 2009 at 16:46
  • Cal - Thanks for the suggestion. I've walked through the executing code many times and haven't hit the catch block yet but that might still be a good idea. Thanks. Commented Feb 7, 2009 at 16:47

2 Answers 2

2

This is because you need to remove the width and height attributes of the tag. Simply changing the CSS image width and height values will not work.

Try something like so, this is in Javascript;


imageTag.removeAttribute('width');
imageTag.removeAttribute('height');
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for trying but it didn't work. I did as you said in codebehind in Page_PreInit().
Did you try doing it everytime a new image is loaded? Try debugging and work out exactly what the database is returning, and if there is any pattern to when it doesn't work.
2

Looking at your code, I would suggest another approach; since you already have a copy of the image in your byte array, why not have your application resize the image and return a predictable size to the client? You would gain the benefit of not worrying about how different browsers resize and possibly reduce the quality of the image.

Example here... http://www.west-wind.com/Weblog/posts/283.aspx

4 Comments

Good idea but the problem is the images have different height to width ratios. If I resize to a height and width I choose I run the risk of distorting the image. There is no universal size I can resize them to.
That's fine though - what you can do is define a maximum width OR height, and then calculate the maximum size that you can make it until it meets the height or width requirement. Then just find the % between the original and your end #, and size the other dimension down. Want a code sample?
+1 - It will save you bandwidth and produce better images at lower sizes than the original. The browser doesn't do any anisotropic filtering or any other technique to resize an image, so it will look crappy. It is trivial to create an HttpHandler that can resize an image & maintain aspect ratio.
IE7 can do anisotropic resizing if you tell it to: code.flickr.com/blog/2008/11/12/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.