读书人

asp.net 依据图片地址URL 生成缩略图片

发布时间: 2013-07-01 12:33:04 作者: rapoo

asp.net 根据图片地址URL 生成缩略图片
急!!!
通过一个图片的url 生成一张图片
[解决办法]
几种方式:
1、读取图片,然后用C#的方法重新生成一张新的图片,再用新的图片显示,我原来就是这么做的。
2、使用脚本,没用过,你可以查查
[解决办法]

引用:
急!!!
通过一个图片的url 生成一张图片
http://download.csdn.net/detail/ce2010/4465793
[解决办法]
public void MakeThumbnail(string imgPath_old, int width, int height)
{

System.Drawing.Image img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(System.IO.File.ReadAllBytes(imgPath_old)));

int towidth = width; int toheight = height;
int x = 0; int y = 0; int ow = img.Width;
int oh = img.Height;
// 按值较大的进行等比缩放(不变形)
if ((double)img.Width / (double)towidth < (double)img.Height / (double)toheight)
{
toheight = height;
towidth = img.Width * height / img.Height;
}
else
{
towidth = width;
toheight = img.Height * width / img.Width;
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
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(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(img, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),


System.Drawing.GraphicsUnit.Pixel);

bitmap.Save(imgPath_old, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
img.Dispose();
g.Dispose();
}

protected void Page_Load(object sender, EventArgs e)
{
String filePath = Server.MapPath("~/xxx.jpg");
new WebClient().DownloadFile("http://www.baidu.com/img/bdlogo.gif",filePath );
MakeThumbnail(filePath , 100, 100);
}


[解决办法]

#region 生成缩略图相关函数
///<summary>
/// 获取一个图片按等比例缩小后的大小。
///</summary>
///<param name="maxWidth">需要缩小到的宽度</param>
///<param name="maxHeight">需要缩小到的高度</param>
///<param name="imageOriginalWidth">图片的原始宽度</param>
///<param name="imageOriginalHeight">图片的原始高度</param>
///<returns>返回图片按等比例缩小后的实际大小</returns>
public static Size GetNewSize(int maxWidth, int maxHeight, int imageOriginalWidth, int imageOriginalHeight)
{
double w = 0.0;
double h = 0.0;
double sw = Convert.ToDouble(imageOriginalWidth);
double sh = Convert.ToDouble(imageOriginalHeight);
double mw = Convert.ToDouble(maxWidth);
double mh = Convert.ToDouble(maxHeight);
if (sw < mw && sh < mh)
{
w = sw;
h = sh;


}
else if ((sw / sh) > (mw / mh))
{
w = maxWidth;
h = (w * sh) / sw;
}
else
{
h = maxHeight;
w = (h * sw) / sh;
}
return new Size(Convert.ToInt32(w), Convert.ToInt32(h));
}
///<summary>
/// 对给定的一个图片(Image对象)生成一个指定大小的缩略图。
///</summary>
///<param name="originalImage">原始图片</param>
///<param name="thumMaxWidth">缩略图的宽度</param>
///<param name="thumMaxHeight">缩略图的高度</param>
///<returns>返回缩略图的Image对象</returns>
public static System.Drawing.Image GetThumbNailImage(System.Drawing.Image originalImage, int thumMaxWidth, int thumMaxHeight)
{
Size thumRealSize = Size.Empty;
System.Drawing.Image newImage = originalImage;
Graphics graphics = null;
try
{
thumRealSize = GetNewSize(thumMaxWidth, thumMaxHeight, originalImage.Width, originalImage.Height);


newImage = new Bitmap(thumRealSize.Width, thumRealSize.Height);
graphics = Graphics.FromImage(newImage);
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.Clear(Color.Transparent);
graphics.DrawImage(originalImage, new Rectangle(0, 0, thumRealSize.Width, thumRealSize.Height), new Rectangle(0, 0, originalImage.Width, originalImage.Height), GraphicsUnit.Pixel);
}
catch { }
finally
{
if (graphics != null)
{
graphics.Dispose();
graphics = null;
}
}
return newImage;
}
///<summary>
/// 对给定的一个图片文件生成一个指定大小的缩略图。
///</summary>
///<param name="originalImage">图片的物理文件地址</param>
///<param name="thumMaxWidth">缩略图的宽度</param>
///<param name="thumMaxHeight">缩略图的高度</param>


///<returns>返回缩略图的Image对象</returns>
public static System.Drawing.Image GetThumbNailImage(string imageFile, int thumMaxWidth, int thumMaxHeight)
{
System.Drawing.Image originalImage = null;
System.Drawing.Image newImage = null;
try
{
originalImage = System.Drawing.Image.FromFile(imageFile);
newImage = GetThumbNailImage(originalImage, thumMaxWidth, thumMaxHeight);
}
catch { }
finally
{
if (originalImage != null)
{
originalImage.Dispose();
originalImage = null;
}
}
return newImage;
}
///<summary>
/// 对给定的一个图片文件生成一个指定大小的缩略图,并将缩略图保存到指定位置。
///</summary>
///<param name="originalImageFile">图片的物理文件地址</param>
///<param name="thumbNailImageFile">缩略图的物理文件地址</param>
///<param name="thumMaxWidth">缩略图的宽度</param>
///<param name="thumMaxHeight">缩略图的高度</param>


public static void MakeThumbNail(string originalImageFile, string thumbNailImageFile, int thumMaxWidth, int thumMaxHeight)
{
System.Drawing.Image newImage = GetThumbNailImage(originalImageFile, thumMaxWidth, thumMaxHeight);
try
{
newImage.Save(thumbNailImageFile, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch
{ }
finally
{
if (newImage != null)
{
newImage.Dispose();
newImage = null;
}
}
}

读书人网 >C#

热点推荐