大家知道,開發項目除了數據訪問層很重要外,就是Common了,這里就提供了強大且實用的工具。
?
【C#公共幫助類】 Convert幫助類
?
Image類:
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Drawing.Imaging; using System.Drawing; using System.Web; namespace Common {public static class Image{#region 圖片格式轉換/// <summary>/// 圖片格式轉換/// </summary>/// <param name="OriFilename">原始文件相對路徑</param>/// <param name="DesiredFilename">生成目標文件相對路徑</param>/// <returns></returns>/// JPG采用的是有損壓縮所以JPG圖像有可能會降低圖像清晰度,而像素是不會降低的 /// GIF采用的是無損壓縮所以GIF圖像是不會降低原圖圖像清晰度和像素的,但是GIF格式只支持256色圖像。public static bool ConvertImage(string OriFilename, string DesiredFilename){string extname = DesiredFilename.Substring(DesiredFilename.LastIndexOf('.')+1).ToLower();ImageFormat DesiredFormat;//根據擴張名,指定ImageFormatswitch (extname){case "bmp":DesiredFormat = ImageFormat.Bmp;break;case "gif":DesiredFormat = ImageFormat.Gif;break;case "jpeg":DesiredFormat = ImageFormat.Jpeg;break;case "ico":DesiredFormat = ImageFormat.Icon;break;case "png":DesiredFormat = ImageFormat.Png;break;default:DesiredFormat = ImageFormat.Jpeg;break;}try{System.Drawing.Image imgFile = System.Drawing.Image.FromFile(WebPathTran(OriFilename));imgFile.Save(WebPathTran(DesiredFilename), DesiredFormat);return true;}catch {return false;}}#endregion#region 圖片縮放/// <summary>/// 圖片固定大小縮放/// </summary>/// <param name="OriFileName">源文件相對地址</param>/// <param name="DesiredFilename">目標文件相對地址</param>/// <param name="IntWidth">目標文件寬</param>/// <param name="IntHeight">目標文件高</param>/// <param name="imageFormat">圖片文件格式</param>public static bool ChangeImageSize(string OriFileName, string DesiredFilename, int IntWidth, int IntHeight, ImageFormat imageFormat){string SourceFileNameStr =WebPathTran(OriFileName); //來源圖片名稱路徑string TransferFileNameStr = WebPathTran(DesiredFilename); //目的圖片名稱路徑FileStream myOutput =null;try{System.Drawing.Image.GetThumbnailImageAbort myAbort = new System.Drawing.Image.GetThumbnailImageAbort(imageAbort);Image SourceImage = System.Drawing.Image.FromFile(OriFileName);//來源圖片定義Image TargetImage = SourceImage.GetThumbnailImage(IntWidth, IntHeight, myAbort, IntPtr.Zero); //目的圖片定義//將TargetFileNameStr的圖片放寬為IntWidth,高為IntHeight myOutput = new FileStream(TransferFileNameStr, FileMode.Create, FileAccess.Write, FileShare.Write);TargetImage.Save(myOutput, imageFormat);myOutput.Close();return true;}catch {myOutput.Close();return false;}}private static bool imageAbort(){return false;}#endregion#region 文字水印/// <summary>/// 文字水印/// </summary>/// <param name="wtext">水印文字</param>/// <param name="source">原圖片物理文件名</param>/// <param name="target">生成圖片物理文件名</param>public static bool ImageWaterText(string wtext,string source, string target){bool resFlag = false;Image image = Image.FromFile(source);Graphics graphics = Graphics.FromImage(image);try{graphics.DrawImage(image, 0, 0, image.Width, image.Height);Font font = new System.Drawing.Font("Verdana", 60);Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Green);graphics.DrawString(wtext, font, brush, 35, 35);image.Save(target);resFlag = true;}catch (Exception){throw;}finally {graphics.Dispose();image.Dispose();}return resFlag;}#endregion#region 圖片水印/// <summary>/// 在圖片上生成圖片水印/// </summary>/// <param name="Path">原服務器圖片路徑</param>/// <param name="Path_syp">生成的帶圖片水印的圖片路徑</param>/// <param name="Path_sypf">水印圖片路徑</param>public static bool ImageWaterPic(string source, string target, string waterPicSource){bool resFlag = false;Image sourceimage = Image.FromFile(source);Graphics sourcegraphics = Graphics.FromImage(sourceimage);Image waterPicSourceImage = Image.FromFile(waterPicSource);try{sourcegraphics.DrawImage(waterPicSourceImage, new System.Drawing.Rectangle(sourceimage.Width - waterPicSourceImage.Width, sourceimage.Height - waterPicSourceImage.Height, waterPicSourceImage.Width, waterPicSourceImage.Height), 0, 0, waterPicSourceImage.Width, waterPicSourceImage.Height, GraphicsUnit.Pixel);sourceimage.Save(target);}catch (Exception){throw;}finally{sourcegraphics.Dispose();sourceimage.Dispose();waterPicSourceImage.Dispose();}return resFlag;}#endregion/// <summary>/// 路徑轉換(轉換成絕對路徑)/// </summary>/// <param name="path"></param>/// <returns></returns>private static string WebPathTran(string path){try{return HttpContext.Current.Server.MapPath(path);}catch{return path;}}} }
?