求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
引用命名控件using SystemIO;
用服务器控件<asp:fileupload runat="server" id="fileupload1"></asp:fileupload>
cs代码 fileupload1SaveAs(ServerMapPath(fileupload1FileName));
无法直接读取客户端目录下的文件,目前有两种方法可以参考:
1:批量上传
这种就是在选择文件的时候可以多选文件,网上有一些组件可以实现,例如CFUpdate。
2:压缩上传
将要上传的统一压缩,然后上传压缩包,在服务器端做解压处理。
可以根据具体情况进行选择。
0条评论