求asp.net 图片上传压缩图片大小代码 就是减小图片的分辨率存储图片到服务器
c#生成缩略图的类
using SystemIO;
using SystemDrawing;
using SystemDrawingImaging;
/// <summary>
/// 处理类
/// 1、生成缩略或按照比例改变的大小和画质
/// 2、将生成的缩略图放到指定的目录下
/// </summary>
public class ImageClass
{
public Image ResourceImage;
private int ImageWidth;
private int ImageHeight;
public string ErrMessage;
/// <summary>
/// 类的构造函数
/// </summary>
/// <param name="ImageFileName">文件的全路径名称</param>
public ImageClass(string ImageFileName)
{
ResourceImage=ImageFromFile(ImageFileName);
ErrMessage="";
}
public bool ThumbnailCallback()
{
return false;
}
/// <summary>
/// 生成缩略图重载方法1,返回缩略图的Image对象
/// </summary>
/// <param name="Width">缩略图的宽度</param>
/// <param name="Height">缩略图的高度</param>
/// <returns>缩略图的Image对象</returns>
public Image GetReducedImage(int Width,int Height)
{
try
{
Image ReducedImage;
ImageGetThumbnailImageAbort callb=new ImageGetThumbnailImageAbort(ThumbnailCallback);
ReducedImage=ResourceImageGetThumbnailImage(Width,Height,callb,IntPtrZero);
return ReducedImage;
}
catch(Exception e)
{
ErrMessage=eMessage;
return null;
}
}
/// <summary>
/// 生成缩略图重载方法2,将缩略图文件保存到指定的路径
/// </summary>
/// <param name="Width">缩略图的宽度</param>
/// <param name="Height">缩略图的高度</param>
/// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:Images ilenamejpg</param>
/// <returns>成功返回true,否则返回false</returns>
public bool GetReducedImage(int Width,int Height,string targetFilePath)
{
try
{
Image ReducedImage;
ImageGetThumbnailImageAbort callb=new ImageGetThumbnailImageAbort(ThumbnailCallback);
ReducedImage=ResourceImageGetThumbnailImage(Width,Height,callb,IntPtrZero);
ReducedImageSave(@targetFilePath,ImageFormatJpeg);
ReducedImageDispose();
return true;
}
catch(Exception e)
{
ErrMessage=eMessage;
return false;
}
}
/// <summary>
/// 生成缩略图重载方法3,返回缩略图的Image对象
/// </summary>
/// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填08</param>
/// <returns>缩略图的Image对象</returns>
public Image GetReducedImage(double Percent)
{
try
{
Image ReducedImage;
ImageGetThumbnailImageAbort callb=new ImageGetThumbnailImageAbort(ThumbnailCallback);
ImageWidth=ConvertToInt32(ResourceImageWidthPercent);
ImageHeight=ConvertToInt32(ResourceImageWidthPercent);
ReducedImage=ResourceImageGetThumbnailImage(ImageWidth,ImageHeight,callb,IntPtrZero);
return ReducedImage;
}
catch(Exception e)
{
ErrMessage=eMessage;
return null;
}
}
/// <summary>
/// 生成缩略图重载方法4,返回缩略图的Image对象
/// </summary>
/// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填08</param>
/// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:Images ilenamejpg</param>
/// <returns>成功返回true,否则返回false</returns>
public bool GetReducedImage(double Percent,string targetFilePath)
{
try
{
Image ReducedImage;
ImageGetThumbnailImageAbort callb=new ImageGetThumbnailImageAbort(ThumbnailCallback);
ImageWidth=ConvertToInt32(ResourceImageWidthPercent);
ImageHeight=ConvertToInt32(ResourceImageWidthPercent);
ReducedImage=ResourceImageGetThumbnailImage(ImageWidth,ImageHeight,callb,IntPtrZero);
ReducedImageSave(@targetFilePath,ImageFormatJpeg);
ReducedImageDispose();
return true;
}
catch(Exception e)
{
ErrMessage=eMessage;
return false;
}
}
}
本篇文章来源于:开发学院 http://educodepubcom 原文链接:http://educodepubcom/2011/0118/28884php
数据库存储,其实是存储在服务器上的路径或的绝对地址 。它是一个字符串,所以数据库字段的类型可使用varchar可变的,长度不超过255。在前台调用时,需要将路径放置在img标签的src属性中,即可显示
0条评论