here i am processing the image loading it from file and overwriting the destination image converting the lowercase name to uppercase but here its resizing using a fixed size, i need to resize with height 260 pixels and maintaining its aspect ratio not setting the image width
//--------------------------------------------------------------------------------//
private void ProcessImage()
{
if (File.Exists(pictureBox1.ImageLocation))
{
string SourceImagePath = pictureBox1.ImageLocation;
string ImageName = Path.GetFileName(SourceImagePath).ToUpper();
string TargetImagePath = Properties.Settings.Default.ImageTargetDirectory + "\\" + ImageName;
//Set the image to uppercase and save as uppercase
if (SourceImagePath.ToUpper() != TargetImagePath.ToUpper())
{
using (Image Temp = Image.FromFile(SourceImagePath))
{
// my problem is here, i need to resize only by height
// and maintain aspect ratio
Bitmap ResizedBitmap = resizeImage(Temp, new Size(175, 260));
//ResizedBitmap.Save(@TargetImagePath);
ResizedBitmap.Save(@TargetImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
pictureBox1.ImageLocation = @TargetImagePath;
File.Delete(SourceImagePath);
}
}
}
//--------------------------------------------------------------------------------//
private static Bitmap resizeImage(Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
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(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return b;
}
//--------------------------------------------------------------------------------//