protected void btnUpdate_Click(object sender, EventArgs e)
{
int SPhotoMaxSize = 39;
int BPhotoMaxSize = 160;
Graphics g = null;
System.Drawing.Image upimage = null;
System.Drawing.Image thumimg = null;
System.Drawing.Image simage = null;
System.Drawing.Image bimage = null;
Bitmap outputfile = null;
string photoPath = "/mocktrade/userprofile/images/";
string fileName = Guid.NewGuid().ToString();
string extension = Path.GetExtension(FileUpload1.PostedFile.FileName).ToUpper();
//原圖路徑
string bPath = photoPath + fileName + "_b" + extension;
string sPath = photoPath + fileName + "_s" + extension;
Regex re = new Regex(@"^(\.jpeg|\.jpg|\.gif|\.png|\.bmp)$", RegexOptions.IgnoreCase);
if (re.IsMatch(extension))
{
try
{
//BlogControl.DAL.BlogDataSetTwo.MT_USERPROFILERow userRow = _blogBll.GetMTUserProfileByUserName(OwnerUserName);
//if (userRow != null)
//{
int width, height, newwidth, newheight;
int bWidth, bHeight, bNewWidth, bNewHeight;
System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); ;
//創建目錄
if (!Directory.Exists(resourcesPath + photoPath))
Directory.CreateDirectory(resourcesPath + photoPath);
//string simagefile = Server.MapPath("a8logo.jpg"); //要加水印的文件
upimage = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream); //上传的图片
width = upimage.Width;
height = upimage.Height;
bWidth = upimage.Width;
bHeight = upimage.Height;
//缩略图
if (width > height && width >= SPhotoMaxSize)
{
newwidth = SPhotoMaxSize;
newheight = (int)((double)height / (double)width * (double)newwidth);
}
else if (width < height && height >= SPhotoMaxSize)
{
newheight = SPhotoMaxSize;
newwidth = (int)((double)width / (double)height * (double)newheight);
}
else if (width == height && height >= SPhotoMaxSize)
{
newheight = SPhotoMaxSize;
newwidth = SPhotoMaxSize;
}
else
{
newwidth = width;
newheight = height;
}
//大图
if (bWidth > bHeight && bWidth >= BPhotoMaxSize)
{
bNewWidth = BPhotoMaxSize;
bNewHeight = (int)((double)bHeight / (double)bWidth * (double)bNewWidth);
}
else if (bWidth < bHeight && bHeight >= BPhotoMaxSize)
{
bNewHeight = BPhotoMaxSize;
bNewWidth = (int)((double)bWidth / (double)bHeight * (double)bNewHeight);
}
else if (bWidth == bHeight && bHeight >= BPhotoMaxSize)
{
bNewHeight = BPhotoMaxSize;
bNewWidth = BPhotoMaxSize;
}
else
{
bNewWidth = bWidth;
bNewHeight = bHeight;
}
thumimg = upimage.GetThumbnailImage(newwidth, newheight, callb, IntPtr.Zero);
bimage = upimage.GetThumbnailImage(bNewWidth, bNewHeight, callb, IntPtr.Zero);
outputfile = new Bitmap(upimage);
bimage.Save(resourcesPath + bPath);
thumimg.Save(resourcesPath + sPath);
outputfile.Dispose();
//刪除舊圖
Common.FileSystem.deleteFile(resourcesPath + BlogProfile.PHOTOPATH);
Common.FileSystem.deleteFile(resourcesPath + BlogProfile.THUMBNAILPATH);
//更新
_blogBll.UpdatePhoto(bPath, sPath, OwnerUserName);
//_UserProfileBLL.UpdateUserProfile(OwnerUserName, bPath, sPath, Common.CRGlobal.toDecimal(userRow.VOCATION), Common.CRGlobal.toDecimal(userRow.INVESTEXPERIENCE),
// Common.CRGlobal.toDecimal(userRow.INTERESTITEM), Common.CRGlobal.toDecimal(userRow.INTERESTMARKET), Common.CRGlobal.toDecimal(userRow.STATUS));
//}
ShowDialog("提示", "上傳成功");
return;
}
catch
{
ShowDialog("提示", "對不起!上傳失敗");
return;
}
finally
{
if (g != null)
g.Dispose();
if (thumimg != null)
thumimg.Dispose();
if (upimage != null)
upimage.Dispose();
if (simage != null)
simage.Dispose();
FileUpload1.Dispose();
//重新绑定数据
BlogDataSet.BLOG_PROFILERow blogRow = this.BlogProfile;
//BlogControl.DAL.BlogDataSetTwo.MT_USERPROFILERow userRow = _blogBll.GetMTUserProfileByUserName(OwnerUserName);
//if (userRow != null)
//{
//string resourcesPath = Server.MapPath(Common.ResourcesPath.RootPath);
//Image1.ImageUrl = Common.ResourcesPath.RootPath + userRow.THUMBNAILPATH;
//Image1.ImageUrl = GetUrl(userRow.THUMBNAILPATH);
Image1.ImageUrl = GetUrl(BlogProfile.THUMBNAILPATH);
//}
}
}
else
{
ShowDialog("提示", "對不起!只能上傳jpeg,jpg,gif,png,bmp圖片");
return;
}
}