Aşağıdaki classı istediğiniz bir kontrollerin içerisine yapıştırın.
İlk önce Usingleri Ekliyoruz.
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
Daha sonra classımızı ekliyoruz.
public static class FileHelper
{
public static void SaveResizedImage(string filePath, string filename, string resizedFilename, int percent)
{
Image origImg = Image.FromFile(Path.Combine(filePath, filename));
Image resizedImg = ScaleByPercent(origImg, percent);
resizedImg.Save(Path.Combine(filePath, resizedFilename), ImageFormat.Jpeg);
resizedImg.Dispose();
origImg.Dispose();
}
public static void SaveResizedImage(string filePath, string filename, string resizedFilename,
int width, int height)
{
Image origImg = Image.FromFile(Path.Combine(filePath, filename));
Image resizedImg = FixedSize(origImg, width, height);
resizedImg.Save(Path.Combine(filePath, resizedFilename), ImageFormat.Jpeg);
resizedImg.Dispose();
origImg.Dispose();
}
public static Image ScaleByPercent(Image imgPhoto, int percent)
{
float nPercent = ((float)percent / 100);
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX,
sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
public static Image FixedSize(Image imgPhoto, int Width, int Height)
{
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();
return bmPhoto;
}
}
Kullanımı :
FileHelper.SaveResizedImage(path, ResimAdi,KucukResimAdi, 300, 230);
FileHelper.SaveResizedImage(path, ResimAdi,KucukResimAdi, 50);