C#怎么在内存中生成图片缩略图
2个回答
2013-11-18
展开全部
可以调用Image 对象的方法来生成缩略图,比如:public void MakeThumbImage(string fileToMake,string fileToSave){ Image img = Image.FromFile(fileToMake);// Properties.Resources.login; //前两个参数是 要生成的缩略图的宽高值;中间是一个回调函数(委托),第三个是必须要求的参数;
Image thumbImage= img.GetThumbnailImage(120, 200, new Image.GetThumbnailImageAbort(delegate() { return 0 < 1; }), IntPtr.Zero); //这里委托用了匿名方法为了简便。 temp.Save(fileToSave);//保存缩略图。}
Image thumbImage= img.GetThumbnailImage(120, 200, new Image.GetThumbnailImageAbort(delegate() { return 0 < 1; }), IntPtr.Zero); //这里委托用了匿名方法为了简便。 temp.Save(fileToSave);//保存缩略图。}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-11-18
展开全部
图片缩略类方法:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Text;
namespace Manage
{
/// <summary>
/// ImagesDIY 的摘要说明
/// </summary>
public class ImagesDIY
{
private int _tWidth; //设置缩略图初始宽度
private int _tHeight; //设置缩略图初始高度 public ImagesDIY()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//计算缩略图的宽度和高度_tWidth,_tHeight
//参数:oWidth为图片原始宽度,oHeight为图片原始高度
public void bili(int oWidth, int oHeight,int newW, int newH)
{
//按比例计算出缩略图的宽度和高度
if (oWidth >= oHeight)
{
_tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(newW)
/ Convert.ToDouble(oWidth)));
_tWidth = newW;
}
else
{
_tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(newH)
/ Convert.ToDouble(oHeight)));
_tHeight = newH;
}
//System.Web.HttpContext.Current.Response.Write("宽度:" + _tWidth + " 高度:" + _tHeight + "<br>");
}
/// <summary>
/// Resize图片
/// </summary>
/// <param name="bmp">原始Bitmap</param>
/// <param name="newW">新的宽度</param>
/// <param name="newH">新的高度</param>
/// <param name="Mode">保留着,暂时未用</param>
/// <returns>处理以后的图片</returns>
public Bitmap KiResizeImage(Bitmap bmp, int newW, int newH, int Mode)
{
try
{
Bitmap b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b); // 插值算法的质量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// 高质量
g.SmoothingMode = SmoothingMode.HighQuality; g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose(); return b;
}
catch
{
return null;
}
}
/// <summary>
/// 缩放
/// </summary>
/// <param name="imageUrl"></param>
/// <param name="savaPath"></param>
/// <param name="newW"></param>
/// <param name="newH"></param>
/// <param name="Mode"></param>
/// <returns></returns>
public void KiResizeImage(string imageUrl, string savePath, int newW, int newH, int Mode)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(imageUrl); bili(image.Width, image.Height, newW, newH);//比例缩放 Bitmap NBM = new System.Drawing.Bitmap(image, image.Width, image.Height);
image = DIY.KiResizeImage(NBM, _tWidth, _tHeight, Mode);
if (File.Exists(savePath))
File.Delete(savePath);
image.Save(savePath, ImageFormat.Jpeg);
}
}
}
页面调用的方法:
protected void Button1_Click(object sender, EventArgs e)
{
//保存原始图
string path = "d:\\0123.jpg";
string tpath = "d:\\0123a.jpg";
System.Drawing.Image image = System.Drawing.Image.FromFile(FileUpload1.PostedFile.FileName);
image.Save(path, ImageFormat.Jpeg); //保存缩略图方法
ImagesDIY diy = new ImagesDIY();
diy.KiResizeImage(FileUpload1.PostedFile.FileName, tpath, 100, 100, 1);
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Text;
namespace Manage
{
/// <summary>
/// ImagesDIY 的摘要说明
/// </summary>
public class ImagesDIY
{
private int _tWidth; //设置缩略图初始宽度
private int _tHeight; //设置缩略图初始高度 public ImagesDIY()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//计算缩略图的宽度和高度_tWidth,_tHeight
//参数:oWidth为图片原始宽度,oHeight为图片原始高度
public void bili(int oWidth, int oHeight,int newW, int newH)
{
//按比例计算出缩略图的宽度和高度
if (oWidth >= oHeight)
{
_tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(newW)
/ Convert.ToDouble(oWidth)));
_tWidth = newW;
}
else
{
_tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(newH)
/ Convert.ToDouble(oHeight)));
_tHeight = newH;
}
//System.Web.HttpContext.Current.Response.Write("宽度:" + _tWidth + " 高度:" + _tHeight + "<br>");
}
/// <summary>
/// Resize图片
/// </summary>
/// <param name="bmp">原始Bitmap</param>
/// <param name="newW">新的宽度</param>
/// <param name="newH">新的高度</param>
/// <param name="Mode">保留着,暂时未用</param>
/// <returns>处理以后的图片</returns>
public Bitmap KiResizeImage(Bitmap bmp, int newW, int newH, int Mode)
{
try
{
Bitmap b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b); // 插值算法的质量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// 高质量
g.SmoothingMode = SmoothingMode.HighQuality; g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose(); return b;
}
catch
{
return null;
}
}
/// <summary>
/// 缩放
/// </summary>
/// <param name="imageUrl"></param>
/// <param name="savaPath"></param>
/// <param name="newW"></param>
/// <param name="newH"></param>
/// <param name="Mode"></param>
/// <returns></returns>
public void KiResizeImage(string imageUrl, string savePath, int newW, int newH, int Mode)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(imageUrl); bili(image.Width, image.Height, newW, newH);//比例缩放 Bitmap NBM = new System.Drawing.Bitmap(image, image.Width, image.Height);
image = DIY.KiResizeImage(NBM, _tWidth, _tHeight, Mode);
if (File.Exists(savePath))
File.Delete(savePath);
image.Save(savePath, ImageFormat.Jpeg);
}
}
}
页面调用的方法:
protected void Button1_Click(object sender, EventArgs e)
{
//保存原始图
string path = "d:\\0123.jpg";
string tpath = "d:\\0123a.jpg";
System.Drawing.Image image = System.Drawing.Image.FromFile(FileUpload1.PostedFile.FileName);
image.Save(path, ImageFormat.Jpeg); //保存缩略图方法
ImagesDIY diy = new ImagesDIY();
diy.KiResizeImage(FileUpload1.PostedFile.FileName, tpath, 100, 100, 1);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询