# How to find image sizes ?

• ### Question

• User-539484920 posted

Hi Everybody,<o:p>
</o:p>

How to find image size for width and height in asp.net with c# code?<o:p>
</o:p>

<o:p></o:p>

<o:p> </o:p>1. I need to find the image sizes in asp.net with c# source.<o:p>
</o:p>

2. in my code behind: I need to fix the image size.<o:p>
</o:p>

3. I need to check uploaded image size and fixed size.<o:p>
</o:p>

4. If it will be match both the fixed size and uploaded image sizes. The uploaded image must store in database.<o:p>
</o:p>

Pls give some idea and sample source for how to set width and height value.(fixed value for image)<o:p></o:p>

<o:p> </o:p>

Thanks.

Tuesday, November 4, 2008 5:44 AM

• User-670144018 posted

I cant get the logic of what you want to accomplish but i suppose the following code will be usefull to you.

private static Image resizeImage(Image img, Size size)
{
int sourceWidth = img.Width;
int sourceHeight = img.Height;

float nPercent = 0;
float nPercentW = ((float)size.Width / (float)sourceWidth);
float nPercentH = ((float)size.Height / (float)sourceHeight);

if (nPercentH <nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;

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

Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, 0, 0, destWidth, destHeight);
g.Dispose();

return (Image)b;
}

http://blog.paranoidferret.com/?p=11

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Tuesday, November 4, 2008 8:17 AM