物件導向 實驗與問題 圖片上傳
檔案名稱 AppCode/GetFile.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing.Imaging;
using System.Drawing;
using System.Drawing.Drawing2D;
/// <summary>
/// 功能說明
/// 一、檔案上傳
/// 二、圖片上傳
/// 三、圖片縮圖處理
/// 四、副檔名檢查
/// </summary>
namespace Sumi.FileProcessing
{
public class GetFile
{
public string sSavePath;
//*檔案上傳================================================
private static bool Upload(FileUpload myFileUpload, string sFilePath,string sFileName)
{
//設定回傳預設值,作為判斷是否上傳成功
bool Result = false;
try
{
//取得上載檔案之延伸檔名,並轉換成小寫字母再進行比對
string sFileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();
myFileUpload.SaveAs(sFilePath + sFileName + sFileExtension);
Result = true;
}
catch (Exception ex)
{
Result = false;
}
return Result;
}
}
public class PhotoImpact
{
//*圖像縮圖 與 上傳========================
public string NarrowUpload(FileUpload fileFromPhoto, string sToPath,string sToName, int iWidth, int iHeight)
{
string sResult = "";
try
{
if (fileFromPhoto.HasFile)
{
//儲存暫存圖片檔案
string sTempName = HttpContext.Current.Request.MapPath("~/temp/") + fileFromPhoto.FileName;
fileFromPhoto.SaveAs(sTempName);
Bitmap image = new Bitmap(sTempName);
System.Drawing.Image smallImage = image;
// 產生縮圖
Bitmap thumbnailBitmap = new Bitmap(iWidth, iHeight);
Graphics thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
//將各像成像品質設定為HighQuality
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
//設定重畫縮圖範圍
Rectangle imageRectangle = new Rectangle(0, 0, iWidth, iHeight);
//重畫縮圖
thumbnailGraph.DrawImage(image, imageRectangle);
//設定呈現方式
//Response.ContentType = "image/jpeg";
//輸出縮圖
thumbnailBitmap.Save(sToPath + sToName);
thumbnailGraph.Dispose();
thumbnailBitmap.Dispose();
image.Dispose();
sResult = sToName;
}
}
catch (Exception ex)
{
sResult = Convert.ToString(ex);
}
return sResult;
}
//*檢查副檔名=========
private static bool CheckExtensions(FileUpload myFileUpload)
{
//設定回傳預設值,作為判斷是否為規定的圖片類型
bool bResult = false;
string[] allowExtensions = { ".jpg", ".gif", ".png" };
//取得上載檔案之延伸檔名,並轉換成小寫字母再進行比對
string fileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();
System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();
for (int i = 0; i < allowExtensions.Length; i++)
{
if (fileExtension == allowExtensions[i])
{
bResult = true;
}
}
return bResult;
}
//*計算維持比例的縮圖大小=========
private int[] getThumbnailImageScale(int maxWidth, int maxHeight, int oldWidth, int oldHeight)
{
int[] iResult = new int[] { 0, 0 };
float widthDividend, heightDividend, commonDividend;
widthDividend = (float)oldWidth / (float)maxWidth;
heightDividend = (float)oldHeight / (float)maxHeight;
commonDividend = (heightDividend > widthDividend) ? heightDividend : widthDividend;
iResult[0] = (int)(oldWidth / commonDividend);
iResult[1] = (int)(oldHeight / commonDividend);
return iResult;
}
private bool ThumbnailCallback()
{
return false;
}
}
}
沒有留言:
張貼留言