激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - ASP.NET教程 - asp.net(C#)壓縮圖片,可以指定圖片模板高寬

asp.net(C#)壓縮圖片,可以指定圖片模板高寬

2019-07-26 11:07ASP.NET之家 ASP.NET教程

今天要在web程序處理圖片,指定圖片的高寬大小。google了一把資料。覺得此方法挺不錯(cuò)的,大家可以借鑒一下,如果小弟寫的有不對(duì)的地方請(qǐng)大家指點(diǎn)一下:以下代碼在winform寫的,在web下測(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è)畫板 
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; 
//清空一下畫布 
g.Clear(Color.White); 
//在指定位置畫圖 
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(); 
}

 

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


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(); 


}

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美囗交| 国产乱乱视频 | 九一成人| 久久久久久久高清 | 羞羞视频.www在线观看 | 亚洲国产网站 | 369看片你懂的小视频在线观看 | 99国产精品国产免费观看 | 欧美一级高清片在线 | 亚洲二区三区在线 | 成人精品久久久 | 伊人手机在线观看 | 中文字幕在线永久视频 | 一级片免费 | 性爱视频免费 | 欧美日韩精品一区二区三区不卡 | 日本搞逼视频 | 国产精品av久久久久久网址 | 久久亚洲春色中文字幕久久 | 欧美视频一区二区 | 精品成人av一区二区三区 | 在线成人一区二区 | 成年人小视频在线观看 | 一级毛片播放 | 色玖玖综合| 羞羞的视频免费 | 人人舔人人射 | 欧美成人精品欧美一级乱黄 | 国产精品1区2区在线观看 | 成人午夜视频免费看 | 一级免费在线 | 国产一区二区视频观看 | 国产精品剧情一区二区在线观看 | 中文字幕精品在线观看 | 国产成人自拍视频在线 | 在线成人看片 | 日本高清视频网站www | 欧美一级小视频 | 久久国产精品久久久久 | 亚洲成在人 | 最新亚洲视频 |