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

G

Garro

In order to correctly scale existing photos (keeping the existing aspect
ratio) of different sizes I need to be able to use the height and width of
an image. While I have been successful in accessing those values as type
unit I have been unable to use them in an "if" statement or to do
calculations using them and I have been working on trying to convert them to
a type (int,decimal) that is compatible with these functions. To that end I
have been trying to come up with a way to convert an object of type
System.Web.UI.WebControls.Unit to an object of type int, or decimal, and I
haven't been able to make it happen. Each time I try the error message in
the subject of this post is returned. Any help would be greatly
appreciated. I have snipped a couple of my attempt and placed them
below.....

// create new Image() object for each Photo
Image objImage = new Image();
Int32 imgHeight = Convert.ToInt32(objImage.Height);

// create new Image() object for each Photo
Image objImage = new Image();
Int32 imgHeight = objImage.Height;
 
G

Garro

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 + "/" +
objPhoto.Name;

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

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

Jduan

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
System.Web.UI.WebControls.Image();
objImage.ImageUrl = "......"; // put image url here

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


Hope this works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top