校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識(shí)庫 > c#生成圖片縮略圖的類(2種實(shí)現(xiàn)思路)

c#生成圖片縮略圖的類(2種實(shí)現(xiàn)思路)

熱門標(biāo)簽:電銷語音自動(dòng)機(jī)器人 五常地圖標(biāo)注 戶外地圖標(biāo)注軟件手機(jī)哪個(gè)好用 凱立德導(dǎo)航官網(wǎng)地圖標(biāo)注 長(zhǎng)春呼叫中心外呼系統(tǒng)哪家好 萊蕪?fù)夂綦婁N機(jī)器人價(jià)格 智能電話營(yíng)銷外呼系統(tǒng) 鄭州400電話辦理 聯(lián)通 地圖標(biāo)注和認(rèn)領(lǐng)
第一種
復(fù)制代碼 代碼如下:

/**//// summary>
/// 生成縮略圖
/// /summary>
/// param name="originalImagePath">源圖路徑(物理路徑)/param>
/// param name="thumbnailPath">縮略圖路徑(物理路徑)/param>
/// param name="width">縮略圖寬度/param>
/// param name="height">縮略圖高度/param>
/// param name="mode">生成縮略圖的方式/param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
Image originalImage = Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高寬縮放(可能變形)
break;
case "W"://指定寬,高按比例
toheight = originalImage.Height * width/originalImage.Width;
break;
case "H"://指定高,寬按比例
towidth = originalImage.Width * height/originalImage.Height;
break;
case "Cut"://指定高寬裁減(不變形)
if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height*towidth/toheight;
y = 0;
x = (originalImage.Width - ow)/2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width*height/towidth;
x = 0;
y = (originalImage.Height - oh)/2;
}
break;
default :
break;
}
//新建一個(gè)bmp圖片
Image bitmap = new System.Drawing.Bitmap(towidth,toheight);
//新建一個(gè)畫板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//設(shè)置高質(zhì)量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空畫布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小繪制原圖片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow,oh),
GraphicsUnit.Pixel);
try
{
//以jpg格式保存縮略圖
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}

關(guān)鍵方法Graphics.DrawImage見ms-help://MS.NETFrameworkSDKv1.1.CHS/cpref/html/frlrfsystemdrawinggraphicsclassdrawimagetopic11.htm
第二種
4個(gè)重載方法,有直接返回Image對(duì)象的,有生成縮略圖,并且保存到指定目錄的!
復(fù)制代碼 代碼如下:

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
/// summary>
/// 圖片處理類
/// 1、生成縮略圖片或按照比例改變圖片的大小和畫質(zhì)
/// 2、將生成的縮略圖放到指定的目錄下
/// /summary>
public class ImageClass
{
public Image ResourceImage;
private int ImageWidth;
private int ImageHeight;
public string ErrMessage;
/// summary>
/// 類的構(gòu)造函數(shù)
/// /summary>
/// param name="ImageFileName">圖片文件的全路徑名稱/param>
public ImageClass(string ImageFileName)
{
ResourceImage=Image.FromFile(ImageFileName);
ErrMessage="";
}
public bool ThumbnailCallback()
{
return false;
}
/// summary>
/// 生成縮略圖重載方法1,返回縮略圖的Image對(duì)象
/// /summary>
/// param name="Width">縮略圖的寬度/param>
/// param name="Height">縮略圖的高度/param>
/// returns>縮略圖的Image對(duì)象/returns>
public Image GetReducedImage(int Width,int Height)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback);
ReducedImage=ResourceImage.GetThumbnailImage(Width,Height,callb,IntPtr.Zero);
return ReducedImage;
}
catch(Exception e)
{
ErrMessage=e.Message;
return null;
}
}
/// summary>
/// 生成縮略圖重載方法2,將縮略圖文件保存到指定的路徑
/// /summary>
/// param name="Width">縮略圖的寬度/param>
/// param name="Height">縮略圖的高度/param>
/// param name="targetFilePath">縮略圖保存的全文件名,(帶路徑),參數(shù)格式:D:Images ilename.jpg/param>
/// returns>成功返回true,否則返回false/returns>
public bool GetReducedImage(int Width,int Height,string targetFilePath)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback);
ReducedImage=ResourceImage.GetThumbnailImage(Width,Height,callb,IntPtr.Zero);
ReducedImage.Save(@targetFilePath,ImageFormat.Jpeg);
ReducedImage.Dispose();
return true;
}
catch(Exception e)
{
ErrMessage=e.Message;
return false;
}
}
/// summary>
/// 生成縮略圖重載方法3,返回縮略圖的Image對(duì)象
/// /summary>
/// param name="Percent">縮略圖的寬度百分比 如:需要百分之80,就填0.8/param>
/// returns>縮略圖的Image對(duì)象/returns>
public Image GetReducedImage(double Percent)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback);
ImageWidth=Convert.ToInt32(ResourceImage.Width*Percent);
ImageHeight=Convert.ToInt32(ResourceImage.Width*Percent);
ReducedImage=ResourceImage.GetThumbnailImage(ImageWidth,ImageHeight,callb,IntPtr.Zero);
return ReducedImage;
}
catch(Exception e)
{
ErrMessage=e.Message;
return null;
}
}
/// summary>
/// 生成縮略圖重載方法4,返回縮略圖的Image對(duì)象
/// /summary>
/// param name="Percent">縮略圖的寬度百分比 如:需要百分之80,就填0.8/param>
/// param name="targetFilePath">縮略圖保存的全文件名,(帶路徑),參數(shù)格式:D:Images ilename.jpg/param>
/// returns>成功返回true,否則返回false/returns>
public bool GetReducedImage(double Percent,string targetFilePath)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback);
ImageWidth=Convert.ToInt32(ResourceImage.Width*Percent);
ImageHeight=Convert.ToInt32(ResourceImage.Width*Percent);
ReducedImage=ResourceImage.GetThumbnailImage(ImageWidth,ImageHeight,callb,IntPtr.Zero);
ReducedImage.Save(@targetFilePath,ImageFormat.Jpeg);
ReducedImage.Dispose();
return true;
}
catch(Exception e)
{
ErrMessage=e.Message;
return false;
}
}
}
您可能感興趣的文章:
  • C#(.net)水印圖片的生成完整實(shí)例
  • asp.net(C#)使用QRCode生成圖片中心加Logo或圖像的二維碼實(shí)例
  • c# .net 生成圖片驗(yàn)證碼的代碼
  • C#動(dòng)態(tài)生成PictureBox并指定圖片的方法
  • C#生成條形碼圖片的簡(jiǎn)單方法
  • c#多圖片上傳并生成縮略圖的實(shí)例代碼
  • C#實(shí)現(xiàn)的pdf生成圖片文字水印類實(shí)例
  • c#生成自定義圖片方法代碼實(shí)例
  • C#實(shí)現(xiàn)在底圖上動(dòng)態(tài)生成文字和圖片

標(biāo)簽:西寧 紅河 岳陽 福州 湖州 衢州 西藏 宣城

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《c#生成圖片縮略圖的類(2種實(shí)現(xiàn)思路)》,本文關(guān)鍵詞  生成,圖片,縮,略圖,的,類,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《c#生成圖片縮略圖的類(2種實(shí)現(xiàn)思路)》相關(guān)的同類信息!
  • 本頁收集關(guān)于c#生成圖片縮略圖的類(2種實(shí)現(xiàn)思路)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 门头沟区| 台州市| 麦盖提县| 拜泉县| 彰化县| 彭山县| 南召县| 渭南市| 镶黄旗| 泰兴市| 慈溪市| 华蓥市| 武川县| 百色市| 都江堰市| 上犹县| 南漳县| 西吉县| 林周县| 白河县| 诸城市| 汾阳市| 筠连县| 石阡县| 新蔡县| 富顺县| 新沂市| 兴业县| 平顶山市| 宁明县| 浙江省| 井陉县| 英吉沙县| 鄂托克前旗| 襄垣县| 彭州市| 双柏县| 册亨县| 西乌| 灵石县| 翁牛特旗|