Powered By Blogger

Friday, January 1, 2010

save imges in folder. resizing images asp.net c#

resizing images using Class

public class ImageManipulator
{
public Bitmap ScaleByPercent(System.Drawing.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);
bmPhoto.SetResolution(72, 72);

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 bool ThumbnailCallback()
{
return true;
}
}


Upload Images in folder

page load:

profileImageFolderPath = Server.MapPath("ProfileImages");

private void UploadPhoto()
{
Bitmap bitmap = new Bitmap(FileUpload1.FileContent);
float percent = 100;
if (bitmap.Width > bitmap.Height)
{
if (bitmap.Width > 500)
{
float width = (float)bitmap.Width / 500;
percent = percent / width;
}
}
else
{
if (bitmap.Height > 500)
{
float heigth = (float)bitmap.Height / 500;
percent = percent / heigth;
}
}
ImageManipulator imageManipulator = new ImageManipulator();
System.Drawing.Image image = imageManipulator.ScaleByPercent(bitmap, (int)percent);
string imagePath = profileImageFolderPath + "\\" + txtEmail.Text.Trim() + ".jpg";
image.Save(imagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}