473,545 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to keep aspect ratio of image inside an ImageButton control?

Hello,

I have an ImageButton that refers to an external image. How can I keep that
image's aspect ratio when I set the ImageButton's size to 120x120?

TIA,

Arthur
Nov 19 '05 #1
5 3855
You could try something like this...

/// <summary>
/// Crop the image if it is too big.
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
protected System.Drawing. Bitmap Crop( System.Drawing. Bitmap bmp ){

System.Drawing. Image imgPhoto = (System.Drawing .Image)bmp;
int Width = 100;
int Height = 100;
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height ;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;

float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;

nPercentW = ((float)Width/(float)sourceWi dth);
nPercentH = ((float)Height/(float)sourceHe ight);

if(nPercentH < nPercentW) {
nPercent = nPercentW;
destY = (int)((Height - (sourceHeight * nPercent))/2);
}
else {
nPercent = nPercentH;
destX = (int)((Width - (sourceWidth * nPercent))/2);
}

int destWidth = (int)(sourceWid th * nPercent);
int destHeight = (int)(sourceHei ght * nPercent);

Bitmap bmPhoto = new Bitmap(Width,
Height, System.Drawing. Imaging.PixelFo rmat.Format24bp pRgb);
bmPhoto.SetReso lution(imgPhoto .HorizontalReso lution,
imgPhoto.Vertic alResolution);

Graphics grPhoto = Graphics.FromIm age(bmPhoto);
grPhoto.Clear(S ystem.Drawing.C olor.White);
grPhoto.Interpo lationMode =
System.Drawing. Drawing2D.Inter polationMode.Hi ghQualityBicubi c;

grPhoto.DrawIma ge(imgPhoto,
new Rectangle(destX ,destY,destWidt h,destHeight),
new Rectangle(sourc eX,sourceY,sour ceWidth,sourceH eight),
GraphicsUnit.Pi xel);

grPhoto.Dispose ();

// delete the previous user's avatar, both *.jpg and *.gif
if( File.Exists( Page.Server.Map Path(Page.Reque st.ApplicationP ath) +
"UserIcons\ \" + EditUser.Userna me + ".jpg") ){
System.IO.File. Delete( Page.Server.Map Path(Page.Reque st.ApplicationP ath)
+ "UserIcons\ \" + EditUser.Userna me + ".jpg" );
}
if( File.Exists( Page.Server.Map Path(Page.Reque st.ApplicationP ath) +
"UserIcons\ \" + EditUser.Userna me + ".gif") ){
System.IO.File. Delete( Page.Server.Map Path(Page.Reque st.ApplicationP ath)
+ "UserIcons\ \" + EditUser.Userna me + ".gif" );
}
return bmPhoto;
}

--
Alex Mueller
"Arthur Hsu" <ah**@vimatech. com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Hello,

I have an ImageButton that refers to an external image. How can I keep that image's aspect ratio when I set the ImageButton's size to 120x120?

TIA,

Arthur

Nov 19 '05 #2
Hi Arthur,

Thanks for your posting. If the external image is in your own
application(sit e) or is dynamically generatecd by your self , you can use
GDI+ to adjust the image. As Alex has provided some code snippet on this.
And I think you can also consider using a custom HTTPHandler which return
the image resource dynamically , here is a good tech article discussing on
this:

#Using ASP.NET HTTP Handlers to create a photo album
http://www.microsoft.com/belux/nl/ms...et/httphandler.
mspx

Otherwise, if the image is on a external remote site which can't be
controled by ourselve , we can't do anything on the image since the image
is acutally loaded by the client user's browser when parsing the page( the
<input type=image...> html element output by the asp.net imagebutton
control).

Please feel free to post here if there is anything unclear. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #3
Hello Steven Cheng[MSFT],

Hrmm... Setting either the height or the width, but not both works for me.
If you want to maintain the aspect ratio, you need to determine which one
is your starting point. Hence, you can not set both of them and expect your
aspect ratio to be maintained.

<img src="my400x200I mage.gif" height="100"> will render the my400x200Image. gif
with a height of 100 and a width of 50 (100/400 = .25 and .25 * 200 = 50).

Going this route saves a bunch of unnecessary code... Let the browser do
the work... :=)

--
Matt Berther
http://www.mattberther.com
Hi Arthur,

Thanks for your posting. If the external image is in your own
application(sit e) or is dynamically generatecd by your self , you can
use GDI+ to adjust the image. As Alex has provided some code snippet
on this. And I think you can also consider using a custom HTTPHandler
which return the image resource dynamically , here is a good tech
article discussing on this:

#Using ASP.NET HTTP Handlers to create a photo album
http://www.microsoft.com/belux/nl/ms.../desmet/httpha
ndler. mspx

Otherwise, if the image is on a external remote site which can't be
controled by ourselve , we can't do anything on the image since the
image is acutally loaded by the client user's browser when parsing the
page( the <input type=image...> html element output by the asp.net
imagebutton control).

Please feel free to post here if there is anything unclear. Thanks.

Regards,

Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #4
Thanks to Alex and Steven. My images are on a remote site :( Maybe I
should think of something else ...

-Arthur

"Steven Cheng[MSFT]" <v-******@online.m icrosoft.com> wrote in message
news:B$******** ******@cpmsftng xa10.phx.gbl...
Hi Arthur,

Thanks for your posting. If the external image is in your own
application(sit e) or is dynamically generatecd by your self , you can use
GDI+ to adjust the image. As Alex has provided some code snippet on this.
And I think you can also consider using a custom HTTPHandler which return
the image resource dynamically , here is a good tech article discussing on
this:

#Using ASP.NET HTTP Handlers to create a photo album
http://www.microsoft.com/belux/nl/ms...et/httphandler.
mspx

Otherwise, if the image is on a external remote site which can't be
controled by ourselve , we can't do anything on the image since the image
is acutally loaded by the client user's browser when parsing the page( the
<input type=image...> html element output by the asp.net imagebutton
control).

Please feel free to post here if there is anything unclear. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #5
You're welcome Arthur,

Always feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #6

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

Similar topics

3
3904
by: jens.buchta | last post by:
Hi! I'm using a DataGrid with a template column to display an Image inside of it. I'm hooking into its OnPrerender-Event to set the ImageURL-Property dynamically. Everything works just fine here, until I thought "It would be cool, if the user could click on that image..". So I replaced the Image-Control with an ImageButton. My Problem...
2
9414
by: Carl Gilbert | last post by:
Hi I am looking for either a component or technique to allow me to do the following: * Provide a panel with a background image * Resize the image to best fit the panel to maintain aspect ratio * Provide white (or other color) borders at the sides or the top/bottom The last point would be used to allow users to resize the panel to any...
2
19347
by: Farce Milverk | last post by:
Hi, I'm looking for an algorithm to resize an image of arbitrary size to a "fixed" / required width and height. For example, my application requires that images be no larger than 440 pixel (height) x 780 (wide) So the "preferred size" would be 440 x 780
0
1984
by: mharness | last post by:
Hello All, Does anyone know how to dynamically change the width and height properties of an image in a datalist in order to maintain the original aspect ratio? I've managed to do this with a single image (see below) but don't have a clue how to apply the same approach in a datalist, perhaps with a custom databinding expression (or what?)....
1
2308
by: trint | last post by:
I have a gridview displaying images from sql server. The images are all different sizes, so I want to display each image at only 70 pixels wide with the right perspective of the height. here is my code that displays them at 50%, which isn't what i want: <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False"...
5
3476
by: =?Utf-8?B?UmljaA==?= | last post by:
Hello, If I create a form in Java with controls like Panels, textboxes... when I stretch/shrink the form, all the controls can grow/shrink - along with the text contained in the textboxes. This is convenient for resizing a form for different screen resolutions. I can't see how to do this /control this in .Net (VS2005). I have a user...
0
1642
by: kjellkp | last post by:
Hi, How do I keep the ratio aspect on images when I try to resize them in a webbrowser controller in my C# .Net 2.0 windows application ?. I use Webbrowser.execCommand("insertImage") to insert an image. In other programs you can hold the shiftkey down when rezising with the mouse. But I can't find a method to keep the ratio aspect in my...
0
2052
by: Sully | last post by:
Hi Guys, I have this about 98% done and I cannot get it work properly. I have a Repeater bound to a MySQL DataSource, inside the Repeater I have an ImageButton, outside the repeater I have a Image, code is below: <asp:Image ID="imgLarge" Name="imgLarge" runat="server" AlternateText="Large Custom Image" Height="300" Width="400" />...
2
5416
by: alag20 | last post by:
Hi, How can I print an while preserving its aspect ratio. Currently I am using the code below. private void PrintClicked(object sender, EventArgs e) { PrintDocument doc = new PrintDocument(); doc.PrintPage += this.Doc_PrintPage; PrintDialog dlgSettings = new PrintDialog();
0
7408
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7661
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7815
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7433
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7763
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5340
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4949
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1891
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
712
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.