C#實戰:PS圖片上傳圖片 同時產生微縮圖

來源:互聯網
上載者:User

【IT168 技術文檔】本文旨在與各位朋友們分享我是如何在項目中用C# “ps圖片” 為網站產生同比例微縮圖的解決方案。如有不足之處歡迎您指出。

  一、技術概述:

  1.Ajax無重新整理上傳圖片,詳情請閱我的這篇文章。(jquery + c# ashx)

  2.C#位元影像處理 System.Drawing。

  3.最新demo支援IE7,IE8,FireFox。

  二、微縮圖處理方法:

       產生微縮圖的核心方法:

  CreateThumbnailPicture

 

代碼

/// <summary>        
/// 圖片微縮圖處理
/// </summary>
/// <param name="srcPath">源圖片</param>
/// <param name="destPath">靶心圖表片</param>
/// <param name="width">寬度</param>

/// <param name="height">高度</param>
public static void CreateThumbnailPicture(string srcPath, string destPath, int width, int height)
{
//根據圖片的磁碟絕對路徑擷取 源圖片 的Image對象
System.Drawing.Image img = System.Drawing.Image.FromFile(srcPath);

//bmp: 最終要建立的 微縮圖 位元影像對象。 Bitmap bmp = new Bitmap(width, height);
//g: 繪製 bmp Graphics 對象
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Transparent);
//為Graphics g 對象 初始化必要參數,很容易理解。
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//源圖片寬和高
int imgWidth = img.Width;
int imgHeight = img.Height;
//繪製微縮圖
g.DrawImage(img, new System.Drawing.Rectangle(0, 0, width, height), new System.Drawing.Rectangle(0, 0, imgWidth, imgHeight) , GraphicsUnit.Pixel);
ImageFormat format = img.RawFormat;
ImageCodecInfo info = ImageCodecInfo.GetImageEncoders().SingleOrDefault(i => i.FormatID == format.Guid);
EncoderParameter param = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = param;
img.Dispose();
//儲存已產生微縮圖,這裡將GIF格式轉化成png格式。
if (format == ImageFormat.Gif)
{
destPath = destPath.ToLower().Replace(".gif", ".png");
bmp.Save(destPath, ImageFormat.Png);
}
else
{
if (info != null)
{
bmp.Save(destPath, info, parameters);
}
else
{
bmp.Save(destPath, format);
}
}
img.Dispose();
g.Dispose();
bmp.Dispose();
}

 部分代碼已經加入注釋,仔細閱讀代碼應該不難理解。下面介紹ashx中AJAX調用方法,我們在AJAX非同步上傳圖片成功後對源圖片進行"PS"。關鍵程式碼片段如下:

 

代碼

//上傳成功後網站內源圖片相對路徑 
string relativePath = System.Web.HttpContext.Current.Request.ApplicationPath
+ string.Format(@"Content/Upload/Images/{0}", fileName);

/*
比例處理
微縮圖高度(DefaultHeight屬性值為 400)
*/
System.Drawing.Image img = System.Drawing.Image.FromFile(toFile);
int width = img.Width;
int height = img.Height;
float ratio = (float)width / height;

//微縮圖高度和寬度
int newHeight = height <= DefaultHeight ? height : DefaultHeight;
int newWidth = height <= DefaultHeight ? width : Convert.ToInt32(DefaultHeight * ratio);

FileInfo generatedfile = new FileInfo(toFile);
string newFileName = "Thumb_" + generatedfile.Name;
string newFilePath = Path.Combine(generatedfile.DirectoryName, newFileName);

PictureHandler.CreateThumbnailPicture(toFile, newFilePath, newWidth, newHeight);

string thumbRelativePath = System.Web.HttpContext.Current.Request.ApplicationPath
+ string.Format(@"/Content/Upload/Images/{0}", newFileName);

//返回原圖和微縮圖的網站相對路徑
relativePath = string.Format("{0},{1}", relativePath, thumbRelativePath);

return relativePath;

 

 三、程式運行:

  上傳前:


 

  上傳後:


 

  四、 小結:

  我使用該方法主要是為瞭解決列印報表時由於圖片大小沒有合理的比例規範導致報表樣式變形,同樣該方法也適合網站或論壇由使用者上傳源圖片產生微縮頭像等。

  如果您感興趣可以在這裡下載本例DEMO。

  最後希望本文能夠協助您解決開發中的類似問題。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.