在内存中动态生成缩略图
生活随笔
收集整理的這篇文章主要介紹了
在内存中动态生成缩略图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "image/jpeg";
//url傳來的圖片名
string img = context.Request.QueryString["img"];
if (!string .IsNullOrEmpty(img))
{
//獲取圖片的物理路徑
string path = context.Request.MapPath("images/"+img);
//加載大圖
Image big = Image.FromFile(path);
int oldWidth = big.Width;
int oldHeigth = big.Height;
int newWidth = 150;
int newHeigth = 100;
//保持縱橫比例
if (oldWidth > oldHeigth)
{
newHeigth = newWidth * oldHeigth / oldWidth;
}
else
{
newWidth = newHeigth * oldWidth / oldHeigth;
}
//生成小圖
Bitmap bitmap = new Bitmap(newWidth,newHeigth);
Graphics g = Graphics.FromImage(bitmap);
//把大圖,畫到小圖上
g.DrawImage(big,0,0,bitmap.Width,bitmap.Height);
//釋放資源
g.Dispose();
//把bitmap輸出到瀏覽器
bitmap.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
context.Response.ContentType = "image/jpeg";
//url傳來的圖片名
string img = context.Request.QueryString["img"];
if (!string .IsNullOrEmpty(img))
{
//獲取圖片的物理路徑
string path = context.Request.MapPath("images/"+img);
//加載大圖
Image big = Image.FromFile(path);
int oldWidth = big.Width;
int oldHeigth = big.Height;
int newWidth = 150;
int newHeigth = 100;
//保持縱橫比例
if (oldWidth > oldHeigth)
{
newHeigth = newWidth * oldHeigth / oldWidth;
}
else
{
newWidth = newHeigth * oldWidth / oldHeigth;
}
//生成小圖
Bitmap bitmap = new Bitmap(newWidth,newHeigth);
Graphics g = Graphics.FromImage(bitmap);
//把大圖,畫到小圖上
g.DrawImage(big,0,0,bitmap.Width,bitmap.Height);
//釋放資源
g.Dispose();
//把bitmap輸出到瀏覽器
bitmap.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
?
轉載于:https://www.cnblogs.com/gylspx/archive/2011/12/01/1133sa.html
總結
以上是生活随笔為你收集整理的在内存中动态生成缩略图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对话框大小与像素关系
- 下一篇: Python-Learn