C#中調(diào)整圖像大小的步驟詳解
在本篇文章中,我將介紹如何在C#中來調(diào)整你想要的圖像大小。要實現(xiàn)這一目標(biāo),我們可以采取以下幾個步驟:
1.首先要獲取你想要調(diào)整大小的圖像:
string path = Server.MapPath("~/Images"); System.Drawing.Image img = System.Drawing.Image.FromFile(string.Concat(path,"/3904.jpg"));
2.將圖像轉(zhuǎn)換為Bitmap:
Bitmap b = new Bitmap(img);
3.創(chuàng)建一個調(diào)整圖像大小的方法:
private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size) { //獲取圖片寬度 int sourceWidth = imgToResize.Width; //獲取圖片高度 int sourceHeight = imgToResize.Height; float nPercent = 0; float nPercentW = 0; float nPercentH = 0; //計算寬度的縮放比例 nPercentW = ((float)size.Width / (float)sourceWidth); //計算高度的縮放比例 nPercentH = ((float)size.Height / (float)sourceHeight); if (nPercentH < nPercentW) nPercent = nPercentH; else nPercent = nPercentW; //期望的寬度 int destWidth = (int)(sourceWidth * nPercent); //期望的高度 int destHeight = (int)(sourceHeight * nPercent); Bitmap b = new Bitmap(destWidth, destHeight); Graphics g = Graphics.FromImage((System.Drawing.Image)b); g.InterpolationMode = InterpolationMode.HighQualityBicubic; //繪制圖像 g.DrawImage(imgToResize, 0, 0, destWidth, destHeight); g.Dispose(); return (System.Drawing.Image)b; }
在上面的方法中,我們獲取了位圖圖像,然后繪制了不同尺寸的圖像(這里繪制出的圖像是基于指定的縱橫比)
4.調(diào)用上述方法,得到調(diào)整大小之后的圖片:
System.Drawing. Image i = resizeImage(b, new Size(100, 100));
輸出結(jié)果:
到此這篇關(guān)于C#中調(diào)整圖像大小的步驟詳解的文章就介紹到這了,更多相關(guān)C#圖像大小內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實現(xiàn)判斷一個時間點是否位于給定時間區(qū)間的方法
這篇文章主要介紹了C#實現(xiàn)判斷一個時間點是否位于給定時間區(qū)間的方法,涉及C#針對時間的轉(zhuǎn)換與判定相關(guān)技巧,需要的朋友可以參考下2015-08-08C#中IEnumerator<T>和IEnumerable的區(qū)別
在C#中,IEnumerator<T>和IEnumerable是用于實現(xiàn)迭代的接口,本文主要介紹了C#中IEnumerator<T>和IEnumerable的區(qū)別,具有一定的參考價值,感興趣的可以了解一下2024-01-01舊項目升級新版Unity2021導(dǎo)致Visual?Studio無法使用的問題
在項目開發(fā)過程中,不可避免的會升級開發(fā)工具。這次我在舊項目版本升級到新版Unity2021.2.x時,出現(xiàn)Visual?Studio無法定位等問題,這里我給大家分享下解決方法,舊項目升級新版Unity2021導(dǎo)致Visual?Studio無法使用的問題,需要的朋友可以參考下2021-12-12C# 中的GroupBy的動態(tài)拼接問題及GroupBy<>用法介紹
這篇文章主要介紹了C# 中的GroupBy的動態(tài)拼接問題,在文章給大家提到了C# List泛型集合中的GroupBy<>用法詳解,需要的朋友可以參考下2017-12-12