最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)生成指定圖片的縮略圖

 更新時(shí)間:2024年04月20日 08:48:17   作者:初九之潛龍勿用  
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)生成指定圖片的縮略圖,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

應(yīng)用場景

我們假設(shè)會有如下場景:

場景1:培訓(xùn)系統(tǒng)中,在上傳課件培訓(xùn)視頻素材的功能,我們會上傳課程封面圖片,將來會在課程詳情內(nèi)容中在指定的位置輸出。

場景2:人才網(wǎng)站中,企業(yè)端管理后臺,會上傳企業(yè)的 LOGO 內(nèi)容圖片,用于企業(yè)介紹頁面或崗位招聘詳情頁面等。

場景3:商城系統(tǒng)中,商品發(fā)布后臺,會上傳商品的主圖宣傳圖片及其它關(guān)鍵介紹性圖片,用于商品詳情頁面中進(jìn)行展示、宣傳。

以上等場景都會使用一個(gè)通用的功能,查詢。查詢的一個(gè)特征點(diǎn),是會顯示如上場景中涉及的課程封面圖、企業(yè)LOGO圖和商品主宣傳圖。通常為了提高查詢性能顯示效率,會在查詢列表中顯示原有圖片的縮略圖,因?yàn)闉榱诉_(dá)到顯示效果,詳情信息里的圖片畢竟質(zhì)量比較高、尺寸比較大。

因此,生成縮略圖主要要達(dá)到以下目的:

1、縮略圖通過壓縮技術(shù)在盡量保證顯示質(zhì)量的情況下,能夠在 Web 瀏覽器中更加迅速地載入數(shù)據(jù)。

2、較小的數(shù)據(jù)量可以節(jié)省流量成本。

3、制作存儲新的縮略圖(僅用于查詢時(shí)顯示)可以更加直觀的吸引用戶,提高系統(tǒng)體驗(yàn)感。

開發(fā)運(yùn)行環(huán)境

操作系統(tǒng): Windows Server 2019 DataCenter

.net版本: .netFramework4.0 或以上

開發(fā)工具:VS2019  C#

方法設(shè)計(jì)

public  Byte[] MakeThumbnail 方法(制作縮略圖)調(diào)用參數(shù)見如下表格:

序號參數(shù)類型說明
1originalImagePathstring物理路徑圖片文件地址,非唯一選項(xiàng) 
2bvalueByte[]Byte[] 類型數(shù)據(jù),非唯一選項(xiàng)
3thumbnailPathstring非必選項(xiàng),方法返回壓縮后的 Byte[]數(shù)組數(shù)據(jù),如果同時(shí)指定輸出文件路徑 thumbnailPath,則同時(shí)生成這個(gè)文
4width=0int指定輸出縮略圖的寬width,默認(rèn)為0,表示為原圖的寬
5height=0int指定輸出縮略圖的高h(yuǎn)eight,默認(rèn)為0,表示為原圖的高
6modestringmode為壓縮方法:"HW":指定高寬縮放(可能變形),"W":指定寬,高按比例 ,"H":指定高,寬按比例 , "Cut":指定高寬裁減(不變形),參數(shù)默認(rèn)="Cut"
7interpolationModeSystem.Drawing.
Drawing2D.
InterpolationMode
指定在縮放或旋轉(zhuǎn)圖像時(shí)使用的算法,默認(rèn)值=System.Drawing.Drawing2D.InterpolationMode.High

物理路徑文件 originalImagePath 或 Byte[]型數(shù)據(jù) bvalue,兩者同時(shí)傳遞以物理路徑文件優(yōu)先。

實(shí)現(xiàn)代碼

方法代碼

//制作縮略圖(壓縮圖),可接收兩種參數(shù),物理路徑文件 originalImagePath 或 Byte[]型數(shù)據(jù) bvalue,兩者同時(shí)傳遞以物理路徑文件優(yōu)先。
//方法返回壓縮后的 Byte[]數(shù)組數(shù)據(jù),如果同時(shí)指定輸出文件路徑thumbnailPath,則同時(shí)生成這個(gè)文件。
//指定輸出縮略圖的寬width和高h(yuǎn)eight,如果為0,則默認(rèn)為原圖的寬或高
//mode為壓縮方法:"HW":指定高寬縮放(可能變形),"W":指定寬,高按比例 ,"H":指定高,寬按比例 , "Cut":指定高寬裁減(不變形)
 
public  Byte[] MakeThumbnail(string originalImagePath, Byte[] bvalue, string thumbnailPath, int width=0, int height=0, string mode="Cut", System.Drawing.Drawing2D.InterpolationMode interpolationMode= System.Drawing.Drawing2D.InterpolationMode.High)
{
                System.Drawing.Image originalImage;
                if (originalImagePath != "")
                {
                    originalImage = System.Drawing.Image.FromFile(originalImagePath);
                }
                else
                {
                    originalImage = System.Drawing.Image.FromStream(new System.IO.MemoryStream(bvalue));
                }
 
                int towidth = width;
                int toheight = height;
 
                int x = 0;
                int y = 0;
                int ow = originalImage.Width;
                int oh = originalImage.Height;
 
                if (towidth == 0)
                {
                    towidth = ow;
                }
                if (toheight == 0)
                {
                    toheight = oh;
                }
 
                switch (mode)
                {
                    case "HW"://指定高寬縮放(可能變形)                 
                        break;
                    case "W"://指定寬,高按比例                     
                        toheight = originalImage.Height * towidth / originalImage.Width;
                        break;
                    case "H"://指定高,寬按比例 
                        towidth = originalImage.Width * toheight / 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 * toheight / towidth;
                            x = 0;
                            y = (originalImage.Height - oh) / 2;
                        }
                        break;
                    default:
                        break;
                }
                //新建一個(gè)bmp圖片 
                System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
 
                //新建一個(gè)畫板 
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
 
                //設(shè)置高質(zhì)量插值法 
                g.InterpolationMode = interpolationMode ;
 
                //設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度 
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
 
                //清空畫布并以透明背景色填充 
                g.Clear(System.Drawing.Color.Transparent);
 
                //在指定位置并且按指定大小繪制原圖片的指定部分 
                g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
                    new System.Drawing.Rectangle(x, y, ow, oh),
                    System.Drawing.GraphicsUnit.Pixel);
 
 
                try
                {
                    //以jpg格式保存縮略圖 
                    System.IO.MemoryStream mstream = new System.IO.MemoryStream();
                    System.Drawing.Imaging.ImageFormat format = originalImage.RawFormat;
                    System.Drawing.Imaging.ImageFormat toFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
                    if (format.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
                    {
                        toFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
                    }
                    else if (format.Equals(System.Drawing.Imaging.ImageFormat.Png))
                    {
                        toFormat = System.Drawing.Imaging.ImageFormat.Png;
                    }
                    else if (format.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
                    {
                        toFormat = System.Drawing.Imaging.ImageFormat.Bmp;
                    }
                    else if (format.Equals(System.Drawing.Imaging.ImageFormat.Gif))
                    {
                        toFormat = System.Drawing.Imaging.ImageFormat.Gif;
                    }
                    else if (format.Equals(System.Drawing.Imaging.ImageFormat.Icon))
                    {
                        toFormat = System.Drawing.Imaging.ImageFormat.Icon;
                    }
                    else if (format.Equals(System.Drawing.Imaging.ImageFormat.Emf))
                    {
                        toFormat = System.Drawing.Imaging.ImageFormat.Emf;
                    }
                    else if (format.Equals(System.Drawing.Imaging.ImageFormat.Exif))
                    {
                        toFormat = System.Drawing.Imaging.ImageFormat.Exif;
                    }
                    else if (format.Equals(System.Drawing.Imaging.ImageFormat.Tiff))
                    {
                        toFormat = System.Drawing.Imaging.ImageFormat.Tiff;
                    }
                    else if (format.Equals(System.Drawing.Imaging.ImageFormat.Wmf))
                    {
                        toFormat = System.Drawing.Imaging.ImageFormat.Wmf;
                    }
 
                    bitmap.Save(mstream, toFormat);
                    
                    byte[] byData = new Byte[mstream.Length];
                    mstream.Position = 0;
                    mstream.Read(byData, 0, byData.Length);
                    mstream.Close();
                    if (thumbnailPath != "")
                    {
                        bitmap.Save(thumbnailPath, toFormat);
                    }
                    return byData;
                }
                catch (System.Exception e)
                {
                    throw e;
                }
                finally
                {
                    originalImage.Dispose();
                    bitmap.Dispose();
                    g.Dispose();
                }
}

調(diào)用示例

本調(diào)用示例實(shí)現(xiàn)判斷上傳的圖像大小,如果圖像大于2Mb則自動進(jìn)行壓縮處理。

string upfilename = Request.PhysicalApplicationPath + "\\upload.jpg"; //上傳的圖片路徑
string mtfilename = Request.PhysicalApplicationPath + "\\mt.jpg";  //縮略圖的圖片路徑
if (System.IO.File.Exists(upfilename))
{
    FileInfo fileInfo = new FileInfo(upfilename);
    float _fsize = fileInfo.Length / (1024*1024);
    if (_fsize >= 2)
    {
        MakeThumbnail(upfilename, null, mtfilename);
    }
    else
    {
        mtfilename = upfilename;
    }
    Response.Write("Result Filename is :"+mtfilename);
}

小結(jié)

輸出縮略圖可以采取動態(tài)輸出和靜態(tài)存儲方式,動態(tài)輸出耗性能,靜態(tài)存儲耗空間,我們可以以空間換時(shí)間來獲取更高的性能。我們需要根據(jù)項(xiàng)目的實(shí)際情況來決定采用哪種方式比較平衡。

到此這篇關(guān)于C#實(shí)現(xiàn)生成指定圖片的縮略圖的文章就介紹到這了,更多相關(guān)C#生成圖片縮略圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

康马县| 南安市| 唐海县| 广水市| 普定县| 桂林市| 金川县| 阿坝县| 航空| 孝昌县| 武宣县| 邻水| 虎林市| 舟曲县| 正定县| 嘉峪关市| 通州市| 山西省| 大余县| 田阳县| 满城县| 麻栗坡县| 南皮县| 云和县| 商洛市| 吉安市| 丰镇市| 长葛市| 克山县| 廊坊市| 古田县| 平远县| 兰西县| 上杭县| 青海省| 石河子市| 清丰县| 惠水县| 辉县市| 温宿县| 铜鼓县|