473,466 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Create 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 3847
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)sourceWidth);
nPercentH = ((float)Height/(float)sourceHeight);

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

int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap bmPhoto = new Bitmap(Width,
Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb) ;
bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
imgPhoto.VerticalResolution);

Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(System.Drawing.Color.White);
grPhoto.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQua lityBicubic;

grPhoto.DrawImage(imgPhoto,
new Rectangle(destX,destY,destWidth,destHeight),
new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight ),
GraphicsUnit.Pixel);

grPhoto.Dispose();

// delete the previous user's avatar, both *.jpg and *.gif
if( File.Exists( Page.Server.MapPath(Page.Request.ApplicationPath) +
"UserIcons\\" + EditUser.Username + ".jpg") ){
System.IO.File.Delete( Page.Server.MapPath(Page.Request.ApplicationPath)
+ "UserIcons\\" + EditUser.Username + ".jpg" );
}
if( File.Exists( Page.Server.MapPath(Page.Request.ApplicationPath) +
"UserIcons\\" + EditUser.Username + ".gif") ){
System.IO.File.Delete( Page.Server.MapPath(Page.Request.ApplicationPath)
+ "UserIcons\\" + EditUser.Username + ".gif" );
}
return bmPhoto;
}

--
Alex Mueller
"Arthur Hsu" <ah**@vimatech.com> wrote in message
news:%2****************@TK2MSFTNGP09.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(site) 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="my400x200Image.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(site) 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.microsoft.com> wrote in message
news:B$**************@cpmsftngxa10.phx.gbl...
Hi Arthur,

Thanks for your posting. If the external image is in your own
application(site) 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
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...
2
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...
2
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...
0
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...
1
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...
5
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...
0
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...
0
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...
2
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 =...
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:
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
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...
0
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.