欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#生成高清縮略圖的二個示例分享

 更新時間:2014年04月09日 10:49:13   作者:  
這篇文章主要介紹了c#生成高清縮略圖的二個示例,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

/// <summary>
 /// 為圖片生成縮略圖  
 /// </summary>
 /// <param name="phyPath">原圖片的路徑</param>
/// <param name="width">縮略圖寬</param>
 /// <param name="height">縮略圖高</param>
 /// <returns></returns>
public System.Drawing.Image GetThumbnail(System.Drawing.Image image, int width, intheight)
{
Bitmap bmp = newBitmap(width, height);
//從Bitmap創(chuàng)建一個System.Drawing.Graphics
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
//設(shè)置 
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//下面這個也設(shè)成高質(zhì)量
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//下面這個設(shè)成High
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//把原始圖像繪制成上面所設(shè)置寬高的縮小圖
System.Drawing.Rectangle rectDestination = newSystem.Drawing.Rectangle(0, 0, width, height);
gr.DrawImage(image, rectDestination, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
returnbmp;
}
 

調(diào)用方法

復(fù)制代碼 代碼如下:

HttpPostedFile file = photoFile.PostedFile;
if(!file.ContentType.Contains("image"))
{
return"照片格式不合法";
}
stringext = Path.GetExtension(file.FileName).ToLower();
if (ext != ".jpg" && ext != ".gif" && ext != ".png"&& ext != ".jpeg")
{
return"請您上傳jpg、gif、png圖片";
}
if(file.ContentLength > 5 * 1024 * 1024)
{
return"請您上傳512字節(jié)內(nèi)的圖片";
}
stringnewName = Guid.NewGuid().ToString();
stringtempPath = "upload/";
stringimg = tempPath + newName + ext;
stringfilePath = Server.MapPath(img);
if(!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
using(System.Drawing.Image originalImage = System.Drawing.Image.FromStream(file.InputStream))
{
GetThumbnail(originalImage, 504, 374).Save(filePath);

示例2

復(fù)制代碼 代碼如下:

public void  MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height)
{
//獲取原始圖片 
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
//縮略圖畫布寬高 
int towidth = width;
int toheight = height;
//原始圖片寫入畫布坐標(biāo)和寬高(用來設(shè)置裁減溢出部分) 
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
//原始圖片畫布,設(shè)置寫入縮略圖畫布坐標(biāo)和寬高(用來原始圖片整體寬高縮放) 
int bg_x = 0;
int bg_y = 0;
int bg_w = towidth;
int bg_h = toheight;
//倍數(shù)變量 
double multiple = 0;
//獲取寬長的或是高長與縮略圖的倍數(shù) 
if (originalImage.Width >= originalImage.Height)
multiple = (double)originalImage.Width / (double)width;
else
multiple = (double)originalImage.Height / (double)height;
//上傳的圖片的寬和高小等于縮略圖 
if (ow <= width && oh <= height)
{
//縮略圖按原始寬高 
bg_w = originalImage.Width;
bg_h = originalImage.Height;
//空白部分用背景色填充 
bg_x = Convert.ToInt32(((double)towidth - (double)ow) / 2);
bg_y = Convert.ToInt32(((double)toheight - (double)oh) / 2);
}
//上傳的圖片的寬和高大于縮略圖 
else
{
//寬高按比例縮放 
bg_w = Convert.ToInt32((double)originalImage.Width / multiple);
bg_h = Convert.ToInt32((double)originalImage.Height / multiple);
//空白部分用背景色填充 
bg_y = Convert.ToInt32(((double)height - (double)bg_h) / 2);
bg_x = Convert.ToInt32(((double)width - (double)bg_w) / 2);
}
//新建一個bmp圖片,并設(shè)置縮略圖大小. 
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一個畫板 
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//設(shè)置高質(zhì)量插值法 
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
//設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度 
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空畫布并設(shè)置背景色 
g.Clear(System.Drawing.ColorTranslator.FromHtml("#F2F2F2"));
//在指定位置并且按指定大小繪制原圖片的指定部分 
//第一個System.Drawing.Rectangle是原圖片的畫布坐標(biāo)和寬高,第二個是原圖片寫在畫布上的坐標(biāo)和寬高,最后一個參數(shù)是指定數(shù)值單位為像素 
g.DrawImage(originalImage, new System.Drawing.Rectangle(bg_x, bg_y, bg_w, bg_h), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
try
{
//獲取圖片類型 
string fileExtension = System.IO.Path.GetExtension(originalImagePath).ToLower();
//按原圖片類型保存縮略圖片,不按原格式圖片會出現(xiàn)模糊,鋸齒等問題. 
switch (fileExtension)
{
case ".gif": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif); break;
case ".jpg": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); break;
case ".bmp": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp); break;
case ".png": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png); break;
}
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}

相關(guān)文章

  • 基于Aforge攝像頭調(diào)用簡單實例

    基于Aforge攝像頭調(diào)用簡單實例

    這篇文章主要為大家詳細(xì)介紹了基于Aforge攝像頭調(diào)用的簡單實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • C#播放背景音樂的方法小結(jié)

    C#播放背景音樂的方法小結(jié)

    這篇文章主要介紹了C#播放背景音樂的方法,實例總結(jié)了C#播放背景音樂的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • unity android設(shè)備上查看log輸出方式

    unity android設(shè)備上查看log輸出方式

    這篇文章主要介紹了unity android設(shè)備上查看log輸出方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • C#減少垃圾回收壓力的字符串操作詳解

    C#減少垃圾回收壓力的字符串操作詳解

    這篇文章給大家詳細(xì)分析了C#減少垃圾回收壓力的字符串操作的相關(guān)知識點,有興趣的朋友參考學(xué)習(xí)下吧。
    2018-03-03
  • 一道關(guān)于C#參數(shù)傳遞的面試題分析

    一道關(guān)于C#參數(shù)傳遞的面試題分析

    這篇文章主要介紹了一道關(guān)于C#參數(shù)傳遞的面試題,實例分析了C#參數(shù)傳遞的相關(guān)使用技巧,需要的朋友可以參考下
    2015-05-05
  • timespan使用方法詳解

    timespan使用方法詳解

    TimeSpan是用來表示一個時間段的實例,兩個時間的差可以構(gòu)成一個TimeSpan實例,現(xiàn)在就來介紹一下使用方法
    2014-04-04
  • C# Page用于各頁面繼承功能實例

    C# Page用于各頁面繼承功能實例

    這篇文章主要介紹了C# Page用于各頁面繼承功能實例,包含了常見的頁面視圖、數(shù)據(jù)緩存、數(shù)據(jù)庫操作等技巧,需要的朋友可以參考下
    2014-10-10
  • 解決C#調(diào)用dll提示

    解決C#調(diào)用dll提示

    下面小編就為大家分享一篇解決C#調(diào)用dll提示"試圖加載格式不正確的程序"問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • C#實現(xiàn)顯示CPU使用率與內(nèi)存使用率

    C#實現(xiàn)顯示CPU使用率與內(nèi)存使用率

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實現(xiàn)顯示CPU使用率與內(nèi)存使用率,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以了解一下
    2022-12-12
  • VS2010下生成dll的方法

    VS2010下生成dll的方法

    這篇文章主要介紹了VS2010下生成dll的方法,需要的朋友可以參考下
    2018-01-01

最新評論