public static string SaveUploadeFile(AsyncFileUpload fup, string SaveFolder, UploadFileType filetype, UploadFileSizeLimitation filesizelimit, UploadImageSize? Imagesize = null) { string FileExtension = Path.GetExtension(fup.FileName); if (!IsValidFileType(FileExtension, filetype)) { MessageBoxHelper.ShowMessageBox(fup.Page, "نوع فایل نامعتبر است", "", MessageBoxType.Information); return null; } if (!IsValidFileSize(fup.FileBytes.Length.ToLong(), filesizelimit)) { MessageBoxHelper.ShowMessageBox(fup.Page, "حجم فایل نامعتبر است", "", MessageBoxType.Information); return null; } MemoryStream memStream = Imagesize.HasValue ? ResizeImage(fup.FileBytes, Imagesize.Value, FileExtension) : new MemoryStream(fup.FileBytes); string FileName = Path.GetFileNameWithoutExtension(fup.FileName); if (FileName != null) FileName = FileName.Replace(" ", "_"); FileName = FileName + "__" + Common.CreateTemporaryPassword(8) + FileExtension; String AttchFilePath = HttpContext.Current.Server.MapPath(SaveFolder + FileName); File.WriteAllBytes(AttchFilePath, memStream.ToArray()); return FileName; }
private static Size GetSize(UploadImageSize Imagesize) { switch (Imagesize) { case UploadImageSize._100_130: return new Size(100, 130); case UploadImageSize._1024_768: return new Size(1024, 768); default: throw new ArgumentOutOfRangeException("Imagesize"); } }
private static MemoryStream ResizeImage(byte[] filebytes, UploadImageSize Imagesize, string FileExtension) { Image original = Image.FromStream(new MemoryStream(filebytes)); Size s = GetSize(Imagesize); Image resized = ResizeImage(original, s); MemoryStream memStream = new MemoryStream(); if (FileExtension.ToUpper() == ".PNG") resized.Save(memStream, ImageFormat.Png); else if (FileExtension.ToUpper() == ".JPG" || FileExtension.ToUpper() == ".JPEG") resized.Save(memStream, ImageFormat.Jpeg); return memStream; }