Tuesday, May 20, 2008

Set Auto Image Size Thumbnail and image resize functions are described below

how we have to call these functions:-

We must give the read write premmison to folder and we select right click and remove readonly property of folder.

1) Set Auto Image Size :-
MyobjectofClassWhereIputMyFunction.AutoImageSize(Request.PhysicalApplicationPath + "UsersImage\\Thumbnail\\" +

objPicName.ToString(), Request.PhysicalApplicationPath + "UsersImage\\Thumbnail\\" + "PlaceGoing2_" + e.Item.ItemIndex +

objPicName.ToString(), 51);






using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Collections;

///
/// This function used for setting the size of image at run time
///

/// orgpath where the image is exist or stroe, modpath where we want to store the image, size it is

size of image
///
public void AutoImageSize(string orgpath, string modpath, int size)
{
//int maxDimension = 153;//this value can be any thing......
int maxDimension = size;
Bitmap bm = new Bitmap(orgpath);
double num = ((float)bm.Width) / ((float)bm.Height);
if (System.IO.File.Exists(modpath))
{
System.IO.File.Delete(modpath);
}
if ((num > 1.0) && (bm.Width > maxDimension))
{

System.Drawing.Image image = new Bitmap(maxDimension, (int)(((double)maxDimension) / num));
Graphics graphics = Graphics.FromImage(image);
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.DrawImage(bm, 0, 0, maxDimension, (int)(((double)maxDimension) / num));
image.Save(modpath);
image.Dispose();
}
else if (bm.Height > maxDimension)
{
System.Drawing.Image image = new Bitmap((int)(maxDimension * num), maxDimension);
Graphics graphics = Graphics.FromImage(image);
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.DrawImage(bm, 0, 0, (int)(maxDimension * num), maxDimension);
image.Save(modpath);
image.Dispose();
}
else
{
System.Drawing.Image image = new Bitmap(bm);
image.Save(modpath);
image.Dispose();
}

bm.Dispose();
}


public void Resize(string sPhysicalPath, string sOrgFileName, string sThumbNailFileName, ImageFormat oFormat, int

intMaxSize)
{

try
{
FileInfo objFileInfo = new FileInfo(sPhysicalPath + sThumbNailFileName);
if (objFileInfo.Exists)
{
objFileInfo.Delete();
}

if (sOrgFileName.IndexOf(".bmp") > 0)
{
Size ThumbNailSize = new Size();
Bitmap oImg = new Bitmap(sPhysicalPath + sOrgFileName);

if (oImg.Width > intMaxSize || oImg.Height > intMaxSize)
{
if (oImg.Width > oImg.Height)
{
ThumbNailSize.Height = (int)((intMaxSize * 1.0F / oImg.Width * 1.0F) * oImg.Height * 1.0F);
ThumbNailSize.Width = intMaxSize;
}
else
{
ThumbNailSize.Width = (int)((intMaxSize * 1.0F / oImg.Height * 1.0F) * oImg.Width * 1.0F);
ThumbNailSize.Height = intMaxSize;
}
}
else
{
ThumbNailSize.Height = oImg.Height;
ThumbNailSize.Width = oImg.Width;
}

// System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.

System.Drawing.Image.GetThumbnailImageAbort myCallback = new

System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

System.Drawing.Image myThumbnail = oImg.GetThumbnailImage(ThumbNailSize.Width, ThumbNailSize.Height,

myCallback, IntPtr.Zero);
myThumbnail.Save(sPhysicalPath + @"\" + sThumbNailFileName);
oImg.Dispose();
}
else
{
Size ThumbNailSize = new Size();
System.Drawing.Image oImg = System.Drawing.Image.FromFile(sPhysicalPath + sOrgFileName);

if (oImg.Width > intMaxSize || oImg.Height > intMaxSize)
{
if (oImg.Width > oImg.Height)
{
ThumbNailSize.Height = (int)((intMaxSize * 1.0F / oImg.Width * 1.0F) * oImg.Height * 1.0F);
ThumbNailSize.Width = intMaxSize;
}
else
{
ThumbNailSize.Width = (int)((intMaxSize * 1.0F / oImg.Height * 1.0F) * oImg.Width * 1.0F);
ThumbNailSize.Height = intMaxSize;
}
}
else
{
ThumbNailSize.Height = oImg.Height;
ThumbNailSize.Width = oImg.Width;
}

System.Drawing.Image oThumbNail = new Bitmap(ThumbNailSize.Width, ThumbNailSize.Height);
Graphics oGraphic = Graphics.FromImage(oThumbNail);

oGraphic.CompositingQuality = CompositingQuality.HighQuality;
oGraphic.SmoothingMode = SmoothingMode.HighQuality;
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle oRectangle = new Rectangle(0, 0, ThumbNailSize.Width, ThumbNailSize.Height);
oGraphic.DrawImage(oImg, oRectangle);
oThumbNail.Save(sPhysicalPath + sThumbNailFileName, oFormat);

oImg.Dispose();
}

objFileInfo = null;
}
catch (Exception ex) { throw ex; }
}

1 comment:

ABBY said...

Wow i was searching for this for a long time in an easiest way and from here i got it

Thanks to Mr. mahesh for this