asp.net(C#)壓縮圖片,可以指定圖片模板高寬
//生成縮略圖函數(shù)
//順序參數(shù):源圖文件流、縮略圖存放地址、模版寬、模版高
//注:縮略圖大小控制在模版區(qū)域內(nèi)
public static void MakeSmallImg(System.IO.Stream fromFileStream, string fileSaveUrl, System.Double templateWidth, System.Double templateHeight)
{
//從文件取得圖片對(duì)象,并使用流中嵌入的顏色管理信息
System.Drawing.Image myImage = System.Drawing.Image.FromStream(fromFileStream, true);
//縮略圖寬、高
System.Double newWidth = myImage.Width, newHeight = myImage.Height;
//寬大于模版的橫圖
if (myImage.Width > myImage.Height || myImage.Width == myImage.Height)
{
if (myImage.Width > templateWidth)
{
//寬按模版,高按比例縮放
newWidth = templateWidth;
newHeight = myImage.Height * (newWidth / myImage.Width);
}
}
//高大于模版的豎圖
else
{
if (myImage.Height > templateHeight)
{
//高按模版,寬按比例縮放
newHeight = templateHeight;
newWidth = myImage.Width * (newHeight / myImage.Height);
}
}
//取得圖片大小
System.Drawing.Size mySize = new Size((int)newWidth, (int)newHeight);
//新建一個(gè)bmp圖片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(mySize.Width, mySize.Height);
//新建一個(gè)畫(huà)板
System.Drawing.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;
//清空一下畫(huà)布
g.Clear(Color.White);
//在指定位置畫(huà)圖
g.DrawImage(myImage, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, myImage.Width, myImage.Height),
System.Drawing.GraphicsUnit.Pixel);
///文字水印
//System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
//System.Drawing.Font f=new Font("宋體",10);
//System.Drawing.Brush b=new SolidBrush(Color.Black);
//G.DrawString("myohmine",f,b,10,10);
//G.Dispose();
///圖片水印
//System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
//Graphics a = Graphics.FromImage(bitmap);
//a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
//copyImage.Dispose();
//a.Dispose();
//copyImage.Dispose();
//保存縮略圖
bitmap.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
myImage.Dispose();
bitmap.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Title = "選擇圖片文件";
// fileDialog.Filter = "excel files (*.xls)|*.xls";
fileDialog.FilterIndex = 1;
if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
System.IO.FileStream file =System.IO.File.Open(fileDialog.FileName,System.IO.FileMode.Open);
System.IO.Stream strea = file;
file.Close();
MakeSmallImg(strea, "縮略圖.jpg", 150, 150);
// file.Close();
}
}
相關(guān)文章
設(shè)置默認(rèn)Ajax操作cache and error
設(shè)置默認(rèn)Ajax操作cache and error,需要的朋友可以參考一下2013-02-02ASP.NET在IE10中無(wú)法判斷用戶(hù)已登入及Session丟失問(wèn)題解決方法
IE10中Session丟失問(wèn)題引起眾多業(yè)內(nèi)人士的關(guān)注,今天發(fā)現(xiàn)在IE10中登錄網(wǎng)站時(shí),使用表單驗(yàn)證機(jī)制(FormsAuthentication)卻無(wú)法判斷該用戶(hù)已登入,保存的Session總會(huì)丟失,本文將介紹解決方法,感興趣的朋友可以參考下,或許對(duì)你有所幫助2013-02-02.Net Core3 用Windows 桌面應(yīng)用開(kāi)發(fā)Asp.Net Core網(wǎng)站
這篇文章主要介紹了.Net Core3 用Windows 桌面應(yīng)用開(kāi)發(fā)Asp.Net Core網(wǎng)站,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01ASP.NET對(duì)路徑"xxxxx"的訪(fǎng)問(wèn)被拒絕的解決方法小結(jié)
異常詳細(xì)信息: System.UnauthorizedAccessException: 對(duì)路徑“D:/temp1/MyTest.txt”的訪(fǎng)問(wèn)被拒絕2012-09-09.Net彈性和瞬態(tài)故障處理庫(kù)Polly實(shí)現(xiàn)彈性策略
這篇文章介紹了.Net彈性和瞬態(tài)故障處理庫(kù)Polly實(shí)現(xiàn)彈性策略的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06上傳圖片后使用數(shù)據(jù)庫(kù)保存圖片的示例分享
這篇文章主要介紹了上傳圖片后使用數(shù)據(jù)庫(kù)保存圖片的示例,需要的朋友可以參考下2014-03-03asp.net中通過(guò)ALinq讓Mysql操作變得如此簡(jiǎn)單
當(dāng)大家已經(jīng)習(xí)慣了使用.net 去操作SQL Server,有多少人曾經(jīng)嘗試過(guò)使用.net 去操作Mysql數(shù)據(jù)庫(kù)!在.net 的光環(huán)下,Mysql是顯得如此微不足道!但是Mysql的開(kāi)源又是如此具有誘惑。2011-07-07.NET使用Hisql實(shí)現(xiàn)菜單管理(增刪改查)
這篇文章介紹了.NET使用Hisql實(shí)現(xiàn)菜單管理(增刪改查)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07ABP框架中導(dǎo)航菜單的使用及JavaScript API獲取菜單的方法
ABP框架是基于ASP.NET的Web開(kāi)發(fā)框架,其中包含基本的菜單項(xiàng)可供調(diào)用,特別是自動(dòng)生成的js API使得能夠在客戶(hù)端獲取菜單,這里我們就來(lái)看一下ABP框架中導(dǎo)航菜單的使用及JavaScript API獲取菜單的方法2016-06-06