S
Sinan Alkan
Hi All,
I have a method to resize any image file to the specific dimensions and
save this new image to a path.
I found it on a web page(By Joel Neubeck) and i changed it for my project.
The code is as follows;
---------------------------------
// usings part
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
// The method
/// <summary>
///
/// </summary>
/// <param name="imgPhoto">Original Image</param>
/// <param name="Width">Width of the new image</param>
/// <param name="Height">Height of the new image</param>
/// <param name="Path">The path that new image will be saved</param>
/// <param name="name">The File Name of the original file without
extension</param>
/// <param name="Subfix">a string to add the end of the file name when
saving as new image file</param>
/// <param name="EXT">Extension of the original file</param>
/// <returns></returns>
public static bool CreateFixedSize(Image imgPhoto, int Width, int Height,
string Path,
string name, string Subfix, string EXT)
{
ImageFormat kk = imgPhoto.RawFormat;
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 = nPercentH;
destX = System.Convert.ToInt16((Width -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((Height -
(sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width, Height,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
try
{
bmPhoto.Save(Path + @"\" + name + Subfix + EXT, kk);
bmPhoto.Dispose();
return true;
}
catch
{
bmPhoto.Dispose();
return false;
}
}
---------------------------------
sample usage is ;
clsImage.CreateFixedSize(imgOrg, 480, 480, txtPath, txtFileName, "_L",
".jpg")
It works and creates new one but the problem is that the quality of new
image become bad then original file.
How can i get same quality as original file.
Anybody can give me a hand for this ?
Thanks in advance.
ALKAN
I have a method to resize any image file to the specific dimensions and
save this new image to a path.
I found it on a web page(By Joel Neubeck) and i changed it for my project.
The code is as follows;
---------------------------------
// usings part
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
// The method
/// <summary>
///
/// </summary>
/// <param name="imgPhoto">Original Image</param>
/// <param name="Width">Width of the new image</param>
/// <param name="Height">Height of the new image</param>
/// <param name="Path">The path that new image will be saved</param>
/// <param name="name">The File Name of the original file without
extension</param>
/// <param name="Subfix">a string to add the end of the file name when
saving as new image file</param>
/// <param name="EXT">Extension of the original file</param>
/// <returns></returns>
public static bool CreateFixedSize(Image imgPhoto, int Width, int Height,
string Path,
string name, string Subfix, string EXT)
{
ImageFormat kk = imgPhoto.RawFormat;
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 = nPercentH;
destX = System.Convert.ToInt16((Width -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((Height -
(sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width, Height,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
try
{
bmPhoto.Save(Path + @"\" + name + Subfix + EXT, kk);
bmPhoto.Dispose();
return true;
}
catch
{
bmPhoto.Dispose();
return false;
}
}
---------------------------------
sample usage is ;
clsImage.CreateFixedSize(imgOrg, 480, 480, txtPath, txtFileName, "_L",
".jpg")
It works and creates new one but the problem is that the quality of new
image become bad then original file.
How can i get same quality as original file.
Anybody can give me a hand for this ?
Thanks in advance.
ALKAN