{
string res = null;
string photoPath = null;
string PhotothumbPath = null;
string newfilename = null;
// int newfileSize = null;
string fileName = Path.GetFileNameWithoutExtension(newfilename);
string fileExt = Path.GetExtension(newfilename);
string thumbName = null;
string fullName = fileName + fileExt;
string uploadDir = Server.MapPath("../uploadImage/");
try
{
if (!Directory.Exists(uploadDir))
Directory.CreateDirectory(uploadDir);
/* for category */
photoPath = uploadDir + "/";
if (!Directory.Exists(photoPath))
Directory.CreateDirectory(photoPath);
PhotothumbPath = photoPath + "Thumb/";
if (!Directory.Exists(PhotothumbPath))
Directory.CreateDirectory(PhotothumbPath);
int nFileLen = myFile.ContentLength;
if (nFileLen == 0)
{
res = "E#There wasn't any file uploaded.";
return res;
}
/* Check file extension (must be JPG) */
if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg")
{
res = "E#The file must have an extension of JPG";
return res;
}
////////************************//*/*/*/
Graphics oGraphics = null;
Bitmap originalBMP = new Bitmap(myFile.InputStream);
Bitmap newBMP = null;
int newWidth = 0, newHeight = 0;
/* Calculate the new image dimensions */
float origWidth = originalBMP.Width;
float origHeight = originalBMP.Height;
if (origWidth > 800)
{
float sngRatio = origWidth / origHeight;
newWidth = int.Parse(ConfigurationManager.AppSettings.Get("ImageWidth").ToString()); //50;
int s = (int)sngRatio;
float newHeight1 = newWidth / sngRatio;
newHeight = (int)newHeight1;
// Create a new bitmap which will hold the previous resized bitmap
newBMP = new Bitmap(originalBMP, newWidth, newHeight); // Create a graphic based on the new bitmap
newBMP.SetResolution(50, 40);
//newBMP.SetResolution(int.Parse(ConfigurationManager.AppSettings.Get("quesImageWidth").ToString()), int.Parse(ConfigurationManager.AppSettings.Get("quesImageHeight").ToString()));
oGraphics = Graphics.FromImage(newBMP);
//newfileSize = newBMP;
}
else
{
newWidth = (int)origWidth;
newHeight = (int)origHeight;
oGraphics = Graphics.FromImage(originalBMP);
originalBMP.SetResolution(50, 40);
//originalBMP.SetResolution(int.Parse(ConfigurationManager.AppSettings.Get("quesImageWidth").ToString()), int.Parse(ConfigurationManager.AppSettings.Get("quesImageHeight").ToString()));
newBMP = originalBMP;
}
// Set the properties for the new graphic file
oGraphics.SmoothingMode = SmoothingMode.AntiAlias;
oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; // Draw the new graphic based on the resized bitmap
oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
oGraphics.CompositingQuality = CompositingQuality.HighQuality;
newfilename = myFile.FileName;
fileName = Path.GetFileNameWithoutExtension(newfilename);
fileExt = Path.GetExtension(newfilename);
fileName = fileName; //+ "_" + txt_Name.Text;
fullName = fileName + fileExt;
int i = 0;
while(File.Exists(photoPath + fullName))
{
i++;
fullName = fileName + "_" + i + fileExt;
}
/* Thumb Image */
System.Drawing.Image.GetThumbnailImageAbort myCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
int thumbHeight = int.Parse(ConfigurationManager.AppSettings["thumbHeight"].ToString());
int thumbWidth = int.Parse(ConfigurationManager.AppSettings["thumbWidth"].ToString());
System.Drawing.Image myThumbnail = newBMP.GetThumbnailImage(thumbWidth, thumbHeight, myCallBack, IntPtr.Zero);
thumbName = fileName + "_Thumb" + fileExt;
myThumbnail.Save(PhotothumbPath + thumbName, ImageFormat.Jpeg);
/* Save Original image */
Bitmap wmBitMap = new Bitmap(newBMP);
wmBitMap.Save(photoPath + fullName, ImageFormat.Jpeg);
UploadFiles.AddFiles(fullName, thumbName, fullName, 0, newWidth, newHeight, "I");
originalBMP.Dispose();
newBMP.Dispose();
oGraphics.Dispose();
}
catch (ArgumentException errArgument)
{
// The file wasn't a valid jpg file
res = "E#The file wasn't a valid jpg file.";
System.IO.File.Delete(photoPath + fullName);
}
res = fullName;
return res;
}
No comments:
Post a Comment