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

來源:互聯網
上載者:User

複製代碼 代碼如下://產生縮圖函數
//順序參數:源圖檔案流、縮圖存放地址、模版寬、模版高
//註:縮圖大小控制在模版地區內
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();

}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.