473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

image buttons

Is there a way to display images (imageButtons or linkbuttons for instance)
as a max size (200px by 50px) and not have it stretch the image?

What I want to be able to do is limit the real estate an image can take up
but not stretch the image if it happens to be 150px by 40.

I am doing commands such as:

<asp:ImageButton ID="CompanyLogo" onClick="Image_Click" Width="200px"
Height="50px" runat="server"/>

Thanks,

Tom

Feb 7 '07 #1
9 4395
No. To avoid stretching you would have to resize the image proportionally
through code. In other words, open the image through the System.Drawing
classes and check the width and height and then setting the
image/imagebutton's width and height to be a new height and width
proportional to the old. HTML doesn't make any allowances for doing
proportional changes itself or for percentage dimensions.
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"tshad" <t@home.comwrote in message
news:uB**************@TK2MSFTNGP05.phx.gbl...
Is there a way to display images (imageButtons or linkbuttons for
instance) as a max size (200px by 50px) and not have it stretch the image?

What I want to be able to do is limit the real estate an image can take up
but not stretch the image if it happens to be 150px by 40.

I am doing commands such as:

<asp:ImageButton ID="CompanyLogo" onClick="Image_Click" Width="200px"
Height="50px" runat="server"/>

Thanks,

Tom

Feb 8 '07 #2
Yes it possible in JavaScript. There are two cases, both with same aspect
ratio (aspect ratio = width / height or height / width). First, image should
not be clipped, and resized to fit predefined rectangle. In this case, image
is shown entirely. Second case (I’m guessing that’s the case you’re working
on) is simply fitting and clipping together, so image can fit (partially) to
predefined rectangle. Apart from that, there are some script related tricks
you have to apply, but don’t worry I prepared fully working example (example
supports both cases, please read comments to find out what needs to be
changed):

-- begin aspx code --

<script type="text/javascript">
//<!--
function ResizeImage(img, maxWidth, maxHeight)
{
if (!img)
return;

var height = img.height;
var width = img.width;

// exit if image fits the allowed rect
if ((height <= maxHeight) && (width <= maxWidth))
return;

var ratio = width / height;

// for non clipping resizing (first case) change it to:
// if (height >= width)
if (height <= width)
{
height = maxHeight;
width = parseInt(maxHeight * ratio);
}
else
{
height = parseInt(maxWidth / ratio);
width = maxWidth;
}

// remove these two lines if you want to use first case resizing (non
clipping)
img.style.left = parseInt((maxWidth - width) / 2) + 'px';
img.style.top = parseInt((maxHeight - height) / 2) + 'px';

img.style.width = width.toString() + 'px';
img.style.height = height.toString() + 'px';
}
//-->
</script>

<!--i used div with hidden overflow to avoid resizing effect while image is
loaded -->
<div style="width: 100px; height: 100px; overflow:hidden">
<asp:ImageButton runat="server" ID="imageButton" BorderWidth="0"
Style="position:relative;"/>
</div>

<script type="text/javascript">
// this trick is to make sure onload event is fired (if i used onload
attribute,
// it wouldn't work if image is cached on client's machine)
var <%=imageButton.ClientID %>_image =
document.getElementById('<%=imageButton.ClientID %>');
<%=imageButton.ClientID %>_image.onload = function() {
ResizeImage(<%=imageButton.ClientID %>_image, 100, 100);
}
// it doesn't work with some binary providers
<%=imageButton.ClientID %>_image.src = '2.jpg';
</script>

some very interesting content that won't be moved when image is loading
-- end aspx code --

Hope this helps

Milosz
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"tshad" <t@home.comwrote in message
news:uB**************@TK2MSFTNGP05.phx.gbl...
Is there a way to display images (imageButtons or linkbuttons for
instance) as a max size (200px by 50px) and not have it stretch the image?

What I want to be able to do is limit the real estate an image can take up
but not stretch the image if it happens to be 150px by 40.

I am doing commands such as:

<asp:ImageButton ID="CompanyLogo" onClick="Image_Click" Width="200px"
Height="50px" runat="server"/>

Thanks,

Tom



Feb 8 '07 #3
Looks pretty good. I am going to play with this today.

Is there a way to make sure the width and heights are proportional to the
old sizes? For example: give it a new height and have the width
automatically calculated based on the old proportions?

Thanks,

Tom

"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:11**********************************@microsof t.com...
Yes it possible in JavaScript. There are two cases, both with same aspect
ratio (aspect ratio = width / height or height / width). First, image
should
not be clipped, and resized to fit predefined rectangle. In this case,
image
is shown entirely. Second case (I'm guessing that's the case you're
working
on) is simply fitting and clipping together, so image can fit (partially)
to
predefined rectangle. Apart from that, there are some script related
tricks
you have to apply, but don't worry I prepared fully working example
(example
supports both cases, please read comments to find out what needs to be
changed):

-- begin aspx code --

<script type="text/javascript">
//<!--
function ResizeImage(img, maxWidth, maxHeight)
{
if (!img)
return;

var height = img.height;
var width = img.width;

// exit if image fits the allowed rect
if ((height <= maxHeight) && (width <= maxWidth))
return;

var ratio = width / height;

// for non clipping resizing (first case) change it to:
// if (height >= width)
if (height <= width)
{
height = maxHeight;
width = parseInt(maxHeight * ratio);
}
else
{
height = parseInt(maxWidth / ratio);
width = maxWidth;
}

// remove these two lines if you want to use first case resizing (non
clipping)
img.style.left = parseInt((maxWidth - width) / 2) + 'px';
img.style.top = parseInt((maxHeight - height) / 2) + 'px';

img.style.width = width.toString() + 'px';
img.style.height = height.toString() + 'px';
}
//-->
</script>

<!--i used div with hidden overflow to avoid resizing effect while image
is
loaded -->
<div style="width: 100px; height: 100px; overflow:hidden">
<asp:ImageButton runat="server" ID="imageButton" BorderWidth="0"
Style="position:relative;"/>
</div>

<script type="text/javascript">
// this trick is to make sure onload event is fired (if i used onload
attribute,
// it wouldn't work if image is cached on client's machine)
var <%=imageButton.ClientID %>_image =
document.getElementById('<%=imageButton.ClientID %>');
<%=imageButton.ClientID %>_image.onload = function() {
ResizeImage(<%=imageButton.ClientID %>_image, 100, 100);
}
// it doesn't work with some binary providers
<%=imageButton.ClientID %>_image.src = '2.jpg';
</script>

some very interesting content that won't be moved when image is loading
-- end aspx code --

Hope this helps

Milosz
>--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"tshad" <t@home.comwrote in message
news:uB**************@TK2MSFTNGP05.phx.gbl...
Is there a way to display images (imageButtons or linkbuttons for
instance) as a max size (200px by 50px) and not have it stretch the
image?

What I want to be able to do is limit the real estate an image can take
up
but not stretch the image if it happens to be 150px by 40.

I am doing commands such as:

<asp:ImageButton ID="CompanyLogo" onClick="Image_Click" Width="200px"
Height="50px" runat="server"/>

Thanks,

Tom



Feb 8 '07 #4

"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:OP**************@TK2MSFTNGP03.phx.gbl...
No. To avoid stretching you would have to resize the image proportionally
through code. In other words, open the image through the System.Drawing
classes and check the width and height and then setting the
image/imagebutton's width and height to be a new height and width
proportional to the old. HTML doesn't make any allowances for doing
proportional changes itself or for percentage dimensions.
How do you figure out the the proportional sizes are?

Thanks,

Tom
>

--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"tshad" <t@home.comwrote in message
news:uB**************@TK2MSFTNGP05.phx.gbl...
>Is there a way to display images (imageButtons or linkbuttons for
instance) as a max size (200px by 50px) and not have it stretch the
image?

What I want to be able to do is limit the real estate an image can take
up but not stretch the image if it happens to be 150px by 40.

I am doing commands such as:

<asp:ImageButton ID="CompanyLogo" onClick="Image_Click" Width="200px"
Height="50px" runat="server"/>

Thanks,

Tom


Feb 8 '07 #5
Howdy,

Read it again :) same aspect ratio = proportional old size.
--
Milosz
"tshad" wrote:
Looks pretty good. I am going to play with this today.

Is there a way to make sure the width and heights are proportional to the
old sizes? For example: give it a new height and have the width
automatically calculated based on the old proportions?

Thanks,

Tom

"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:11**********************************@microsof t.com...
Yes it possible in JavaScript. There are two cases, both with same aspect
ratio (aspect ratio = width / height or height / width). First, image
should
not be clipped, and resized to fit predefined rectangle. In this case,
image
is shown entirely. Second case (I'm guessing that's the case you're
working
on) is simply fitting and clipping together, so image can fit (partially)
to
predefined rectangle. Apart from that, there are some script related
tricks
you have to apply, but don't worry I prepared fully working example
(example
supports both cases, please read comments to find out what needs to be
changed):

-- begin aspx code --

<script type="text/javascript">
//<!--
function ResizeImage(img, maxWidth, maxHeight)
{
if (!img)
return;

var height = img.height;
var width = img.width;

// exit if image fits the allowed rect
if ((height <= maxHeight) && (width <= maxWidth))
return;

var ratio = width / height;

// for non clipping resizing (first case) change it to:
// if (height >= width)
if (height <= width)
{
height = maxHeight;
width = parseInt(maxHeight * ratio);
}
else
{
height = parseInt(maxWidth / ratio);
width = maxWidth;
}

// remove these two lines if you want to use first case resizing (non
clipping)
img.style.left = parseInt((maxWidth - width) / 2) + 'px';
img.style.top = parseInt((maxHeight - height) / 2) + 'px';

img.style.width = width.toString() + 'px';
img.style.height = height.toString() + 'px';
}
//-->
</script>

<!--i used div with hidden overflow to avoid resizing effect while image
is
loaded -->
<div style="width: 100px; height: 100px; overflow:hidden">
<asp:ImageButton runat="server" ID="imageButton" BorderWidth="0"
Style="position:relative;"/>
</div>

<script type="text/javascript">
// this trick is to make sure onload event is fired (if i used onload
attribute,
// it wouldn't work if image is cached on client's machine)
var <%=imageButton.ClientID %>_image =
document.getElementById('<%=imageButton.ClientID %>');
<%=imageButton.ClientID %>_image.onload = function() {
ResizeImage(<%=imageButton.ClientID %>_image, 100, 100);
}
// it doesn't work with some binary providers
<%=imageButton.ClientID %>_image.src = '2.jpg';
</script>

some very interesting content that won't be moved when image is loading
-- end aspx code --

Hope this helps

Milosz
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"tshad" <t@home.comwrote in message
news:uB**************@TK2MSFTNGP05.phx.gbl...
Is there a way to display images (imageButtons or linkbuttons for
instance) as a max size (200px by 50px) and not have it stretch the
image?

What I want to be able to do is limit the real estate an image can take
up
but not stretch the image if it happens to be 150px by 40.

I am doing commands such as:

<asp:ImageButton ID="CompanyLogo" onClick="Image_Click" Width="200px"
Height="50px" runat="server"/>

Thanks,

Tom



Feb 8 '07 #6
Hi there,

Simple formula:

R = w\h

where: R - aspect ratio, h - original image height, w - original image
width, In addition to that let's introduce Wmax - constraining (maximum)
width, Hmax - constraining maximum height.

if you use height as referential size:
newHeight = Hmax
newWidth = Hmax * R

if you use width as referential size:
newHeight = Wmax / R
newWidth = Wmax

--
Milosz
"tshad" wrote:
>
"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:OP**************@TK2MSFTNGP03.phx.gbl...
No. To avoid stretching you would have to resize the image proportionally
through code. In other words, open the image through the System.Drawing
classes and check the width and height and then setting the
image/imagebutton's width and height to be a new height and width
proportional to the old. HTML doesn't make any allowances for doing
proportional changes itself or for percentage dimensions.

How do you figure out the the proportional sizes are?

Thanks,

Tom


--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"tshad" <t@home.comwrote in message
news:uB**************@TK2MSFTNGP05.phx.gbl...
Is there a way to display images (imageButtons or linkbuttons for
instance) as a max size (200px by 50px) and not have it stretch the
image?

What I want to be able to do is limit the real estate an image can take
up but not stretch the image if it happens to be 150px by 40.

I am doing commands such as:

<asp:ImageButton ID="CompanyLogo" onClick="Image_Click" Width="200px"
Height="50px" runat="server"/>

Thanks,

Tom


Feb 8 '07 #7
"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:OP**************@TK2MSFTNGP03.phx.gbl...
No. To avoid stretching you would have to resize the image proportionally
through code. In other words, open the image through the System.Drawing
classes and check the width and height and then setting the
image/imagebutton's width and height to be a new height and width
proportional to the old. HTML doesn't make any allowances for doing
proportional changes itself or for percentage dimensions.
I tried that but I can't seem to get the oThumbnail.Save to work. I get the
following error:

System.Runtime.InteropServices.ExternalException: A generic error occurred
in GDI+.

theFileName = New FileInfo(strFileName)
fileOut = fileIn.Substring(0,fileIn.LastIndexOf(".")) &
session("User").CompanyID & fileIn.Substring(fileIn.LastIndexOf("."))
pathFileOut = Server.MapPath("\Uploads") & "\" &
fileIn.Substring(0,fileIn.LastIndexOf(".")) & "Temp" &
fileIn.Substring(fileIn.LastIndexOf("."))
MyFile2.PostedFile.SaveAs(PathFileOut)

' Write out a temporary file that will be read back in for thumbnail
Dim oImage As System.Drawing.Image
oImage = oImage.FromFile(pathFileOut)

' Read back in File, create thumbnail and save it
Dim oThumbnail as System.Drawing.Image
oThumbnail = oImage.GetThumbnailImage(100,200,Nothing,Nothing)
thumbnailFileOut = Server.MapPath("\Uploads") & "\" &
fileIn.Substring(0,fileIn.LastIndexOf(".")) &
fileIn.Substring(fileIn.LastIndexOf("."))
oThumbnail.Save(Server.MapPath("uploads") & "\" & fileOut,
ImageFormat.Jpeg)

I am getting the error on the last line.

The path and filename are valid. It is the same as when I saved the
original file (MyFile2.PostedFile.SaveAs(PathFileOut)).

Thanks,

Tom
>
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"tshad" <t@home.comwrote in message
news:uB**************@TK2MSFTNGP05.phx.gbl...
>Is there a way to display images (imageButtons or linkbuttons for
instance) as a max size (200px by 50px) and not have it stretch the
image?

What I want to be able to do is limit the real estate an image can take
up but not stretch the image if it happens to be 150px by 40.

I am doing commands such as:

<asp:ImageButton ID="CompanyLogo" onClick="Image_Click" Width="200px"
Height="50px" runat="server"/>

Thanks,

Tom


Feb 8 '07 #8
"tshad" <t@home.comwrote in message
news:eQ**************@TK2MSFTNGP03.phx.gbl...
"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:OP**************@TK2MSFTNGP03.phx.gbl...
>No. To avoid stretching you would have to resize the image proportionally
through code. In other words, open the image through the System.Drawing
classes and check the width and height and then setting the
image/imagebutton's width and height to be a new height and width
proportional to the old. HTML doesn't make any allowances for doing
proportional changes itself or for percentage dimensions.

I tried that but I can't seem to get the oThumbnail.Save to work. I get
the following error:

System.Runtime.InteropServices.ExternalException: A generic error occurred
in GDI+.

theFileName = New FileInfo(strFileName)
fileOut = fileIn.Substring(0,fileIn.LastIndexOf(".")) &
session("User").CompanyID & fileIn.Substring(fileIn.LastIndexOf("."))
pathFileOut = Server.MapPath("\Uploads") & "\" &
fileIn.Substring(0,fileIn.LastIndexOf(".")) & "Temp" &
fileIn.Substring(fileIn.LastIndexOf("."))
MyFile2.PostedFile.SaveAs(PathFileOut)

' Write out a temporary file that will be read back in for thumbnail
Dim oImage As System.Drawing.Image
oImage = oImage.FromFile(pathFileOut)

' Read back in File, create thumbnail and save it
Dim oThumbnail as System.Drawing.Image
oThumbnail = oImage.GetThumbnailImage(100,200,Nothing,Nothing)
thumbnailFileOut = Server.MapPath("\Uploads") & "\" &
fileIn.Substring(0,fileIn.LastIndexOf(".")) &
fileIn.Substring(fileIn.LastIndexOf("."))
oThumbnail.Save(Server.MapPath("uploads") & "\" & fileOut,
ImageFormat.Jpeg)

I am getting the error on the last line.
Someone pointed out the problem with this one. The last line should be:

oThumbnail.Save(thumbnailFileOut ,ImageFormat.Jpeg)

I missed that from all my changing code to try to get it to work.

Thanks,

Tom
>
The path and filename are valid. It is the same as when I saved the
original file (MyFile2.PostedFile.SaveAs(PathFileOut)).

Thanks,

Tom
>>
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"tshad" <t@home.comwrote in message
news:uB**************@TK2MSFTNGP05.phx.gbl...
>>Is there a way to display images (imageButtons or linkbuttons for
instance) as a max size (200px by 50px) and not have it stretch the
image?

What I want to be able to do is limit the real estate an image can take
up but not stretch the image if it happens to be 150px by 40.

I am doing commands such as:

<asp:ImageButton ID="CompanyLogo" onClick="Image_Click" Width="200px"
Height="50px" runat="server"/>

Thanks,

Tom



Feb 8 '07 #9
Worked great.

It turns out that if you are going to use width as the referential size you
need to change the Ration formula to: R = h\w.

Thanks,

Tom

"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:2F**********************************@microsof t.com...
Hi there,

Simple formula:

R = w\h

where: R - aspect ratio, h - original image height, w - original image
width, In addition to that let's introduce Wmax - constraining (maximum)
width, Hmax - constraining maximum height.

if you use height as referential size:
newHeight = Hmax
newWidth = Hmax * R

if you use width as referential size:
newHeight = Wmax / R
newWidth = Wmax

--
Milosz
"tshad" wrote:
>>
"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:OP**************@TK2MSFTNGP03.phx.gbl...
No. To avoid stretching you would have to resize the image
proportionally
through code. In other words, open the image through the System.Drawing
classes and check the width and height and then setting the
image/imagebutton's width and height to be a new height and width
proportional to the old. HTML doesn't make any allowances for doing
proportional changes itself or for percentage dimensions.

How do you figure out the the proportional sizes are?

Thanks,

Tom
>

--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"tshad" <t@home.comwrote in message
news:uB**************@TK2MSFTNGP05.phx.gbl...
Is there a way to display images (imageButtons or linkbuttons for
instance) as a max size (200px by 50px) and not have it stretch the
image?

What I want to be able to do is limit the real estate an image can
take
up but not stretch the image if it happens to be 150px by 40.

I am doing commands such as:

<asp:ImageButton ID="CompanyLogo" onClick="Image_Click" Width="200px"
Height="50px" runat="server"/>

Thanks,

Tom





Feb 9 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: nath | last post by:
Does anybody know how to make an image a "submit"-button WITHOUT using JavaScript ? means i have three image buttons in my first.jsp. if i press first image it has to go back.jsp when i...
2
by: itsme | last post by:
Hello, I am using HTML image buttons in my web application and when I run it, all my image buttons are showing up with a border. I tried using border="0" with the input tag, but it is...
4
by: CGuy | last post by:
Hi Gurus, I have a rather unique requirements in my ASP.NET application - I need to create image buttons on the fly while rendering a page. The scenario is like this - the user can enter some...
1
by: JoeClark | last post by:
I am using .Net Framework 1.1. I have used Image buttons (Server side) in the application. Image buttons are not getting displayed on the screen sometimes. Why it could be. The client browser is...
6
by: pmud | last post by:
Hi, I have created a very simple ASP.NET application which has a couple of ImageButtons which go to different SQL reports on clicking them. I have used Response.Redirect to send the user to the ...
0
by: Stephen Skinner | last post by:
Ok, I've banged my head on this as long as I care to. I'm trying to use image buttons to add nice graphical edit butons to my datagrid. The link buttons trigger the datagrid commands, but the image...
16
by: browntown | last post by:
so I have this application I'm nearly finished with. The only thing the client has requested is the ability to submit the form by pressing "enter". I didn't think this would be a huge pain in the...
3
by: Andreas Wöckl | last post by:
HI Group! I have to programmatically create a user input form with various Checkbox and RadioButton lists - Beside every List I have to place an image button that is able to reset the...
4
by: vamshiv | last post by:
hi...im working on asp.net with VB i have created a database which hav fields id,name,image path in web form i hav written code for creation of image buttons dynamically i hav connected the form...
4
by: rajsrmc | last post by:
In my form i have three radio buttons and nine image buttons, three image buttons per radio buttons If the user check one radio button, the respective three image buttons only has to be displayed....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.