/// <summary>
/// 獲取一個圖片按等比例縮小後的大小
/// </summary>
/// <param name="maxWidth">需要縮小到的寬度</param>
/// <param name="maxHeight">需要縮小到的高度</param>
/// <param name="imageOriginalWidth">圖片的原始寬度</param>
/// <param name="imageOriginalHeight">圖片的原始高度</param>
/// <returns>返回圖片按等比例縮小後的實際大小</returns>
public static Size GetNewSize(int maxWidth
{
double w =
double h =
double sw = Convert
double sh = Convert
double mw = Convert
double mh = Convert
if (sw < mw && sh < mh)
{
w = sw;
h = sh;
}
else if ((sw / sh) > (mw / mh))
{
w = maxWidth;
h = (w * sh) / sw;
}
else
{
h = maxHeight;
w = (h * sw) / sh;
}
return new Size(Convert
}
/// <summary>
/// 對給定的一個圖片(Image對象)生成一個指定大小的縮略圖
/// </summary>
/// <param name="originalImage">原始圖片</param>
/// <param name="thumMaxWidth">縮略圖的寬度</param>
/// <param name="thumMaxHeight">縮略圖的高度</param>
/// <returns>返回縮略圖的Image對象</returns>
public static System
{
Size thumRealSize = Size
System
Graphics graphics = null;
try
{
thumRealSize = GetNewSize(thumMaxWidth
newImage = new Bitmap(thumRealSize
graphics = Graphics
graphics
graphics
graphics
graphics
graphics
}
catch { }
finally
{
if (graphics != null)
{
graphics
graphics = null;
}
}
return newImage;
}
/// <summary>
/// 對給定的一個圖片文件生成一個指定大小的縮略圖
/// </summary>
/// <param name="originalImage">圖片的物理文件地址</param>
/// <param name="thumMaxWidth">縮略圖的寬度</param>
/// <param name="thumMaxHeight">縮略圖的高度</param>
/// <returns>返回縮略圖的Image對象</returns>
public static System
{
System
System
try
{
originalImage = System
newImage = GetThumbNailImage(originalImage
}
catch { }
finally
{
if (originalImage != null)
{
originalImage
originalImage = null;
}
}
return newImage;
}
/// <summary>
/// 對給定的一個圖片文件生成一個指定大小的縮略圖
/// </summary>
/// <param name="originalImageFile">圖片的物理文件地址</param>
/// <param name="thumbNailImageFile">縮略圖的物理文件地址</param>
/// <param name="thumMaxWidth">縮略圖的寬度</param>
/// <param name="thumMaxHeight">縮略圖的高度</param>
public static void MakeThumbNail(string originalImageFile
{
System
try
{
newImage
}
catch
{ }
finally
{
newImage
newImage = null;
}
}
/// <summary>
/// 將一個圖片的內存流調整為指定大小
/// </summary>
/// <param name="originalImageStream">原始圖片的內存流</param>
/// <param name="newWidth">新圖片的寬度</param>
/// <param name="newHeight">新圖片的高度</param>
/// <returns>返回調整後的圖片的內存流</returns>
public static MemoryStream ResizeImage(Stream originalImageStream
{
MemoryStream newImageStream = null;
System
if (newImage != null)
{
newImageStream = new MemoryStream();
newImage
}
return newImageStream;
}
/// <summary>
/// 將一個內存流保存為磁盤文件
/// </summary>
/// <param name="stream">內存流</param>
/// <param name="newFile">目標磁盤文件地址</param>
public static void SaveStreamToFile(Stream stream
{
if (stream == null || stream
{
return;
}
byte[] buffer = new byte[stream
stream
stream
FileStream fileStream = new FileStream(newFile
fileStream
fileStream
fileStream
fileStream
}
/// <summary>
/// 對一個指定的圖片加上圖片水印效果
/// </summary>
/// <param name="imageFile">圖片文件地址</param>
/// <param name="waterImage">水印圖片(Image對象)</param>
public static void CreateImageWaterMark(string imageFile
{
if (string
{
return;
}
System
if (originalImage
{
return;
}
Graphics graphics = Graphics
int x = originalImage
int y = originalImage
int width = waterImage
int height = waterImage
graphics
graphics
MemoryStream stream = new MemoryStream();
originalImage
originalImage
System
imageWithWater
imageWithWater
}
/// <summary>
/// 對一個指定的圖片加上文字水印效果
/// </summary>
/// <param name="imageFile">圖片文件地址</param>
/// <param name="waterText">水印文字內容</param>
public static void CreateTextWaterMark(string imageFile
{
if (string
{
return;
}
System
Graphics graphics = Graphics
graphics
graphics
graphics
graphics
SolidBrush brush = new SolidBrush(Color
Font waterTextFont = new Font("Arial"
SizeF waterTextSize = graphics
float x = (float)originalImage
float y = (float)originalImage
graphics
graphics
brush
MemoryStream stream = new MemoryStream();
originalImage
originalImage
System
imageWithWater
imageWithWater
}
/// <summary>
/// 判斷上傳組件是否包含內容
/// </summary>
/// <param name="fileUpload">ASP
/// <returns>如果數據有效
public static bool IsAttachmentValid(FileUpload fileUpload)
{
if (fileUpload != null &&
fileUpload
!string
fileUpload
{
return true;
}
return false;
}
From:http://tw.wingwit.com/Article/program/net/201311/14084.html