By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,948 Members | 1,975 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,948 IT Pros & Developers. It's quick & easy.

RE: Unable to cast object of type 'System.Web.UI.WebControls.Unit' to

P: n/a
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies

P: n/a
Thanks so much for your reply... However, I tried it and I still can't make
it work. Instead of returning the Width or the Height is simply returns a
0. I also notice that when I hover the .Value in the selection box that
displays when I am typing the command... ie objImage.Height.Value it
describes Value as "Getting the length of the System.UI.Webcontrols.Unit".

I have included the code that I generated based on your response to my
question. As a newbie it may be that I don't understand exactly how to use
it. Further direction would be helpful.

Code Snippet..

// create new Image() object for each Photo
Image objImage = new Image();

objImage.ImageUrl = strWebPath + "/" +

int imgWidth =
objImage.Width = Convert.ToInt32(imgWidth * 0.10);
// scale the width

int imgHeight =
objImage.Height = Convert.ToInt32(imgHeight * 0.10);
// scale the height

"Jduan" <Jd*** in message
Jun 27 '08 #2

P: n/a
System.Web.UI.WebControls.Image is just a ASP.NET server-side control used to
render an image. It does not actually load the image on the server,
therefore, by default, objImage.Height.IsEmpty is always true and the
objImage.Height.Value is 0.

There are a lot of ways to get the actual image size on the server side, all
of them will involve reading the actual image file. The simplest way would be
like this:

// create image web control and set the image Url
System.Web.UI.WebControls.Image objImage = new
objImage.ImageUrl = "......"; // put image url here

// set the size of the image control
using (System.Drawing.Image img =
System.Drawing.Image.FromFile(Server.MapPath(objIm age.ImageUrl)))
objImage.Width = new Unit(img.Width * 0.1);
objImage.Height = new Unit(img.Height * 0.1);
Hope this works.

Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.