国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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

2024-07-10 13:26:18
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:


//生成縮略圖函數
//順序參數:源圖文件流、縮略圖存放地址、模版寬、模版高
//注:縮略圖大小控制在模版區域內
public static void MakeSmallImg(System.IO.Stream fromFileStream, string fileSaveUrl, System.Double templateWidth, System.Double templateHeight)
{
//從文件取得圖片對象,并使用流中嵌入的顏色管理信息
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);
//新建一個bmp圖片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(mySize.Width, mySize.Height);
//新建一個畫板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//設置高質量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//設置高質量,低速度呈現平滑程度
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();
}


復制代碼 代碼如下:


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

}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嫩江县| 班戈县| 苍溪县| 曲松县| 曲阳县| 苗栗县| 高州市| 沁阳市| 普安县| 定州市| 黑龙江省| 兴仁县| 乐清市| 确山县| 泰宁县| 登封市| 赣榆县| 台北市| 安宁市| 舒城县| 静宁县| 阳新县| 邛崃市| 河曲县| 荆州市| 宁国市| 法库县| 丰原市| 尼勒克县| 乐陵市| 望谟县| 台安县| 夏河县| 丹棱县| 武清区| 道孚县| 大方县| 水富县| 平罗县| 临城县| 连江县|