在网上找了一段压缩图片的代码如下:
public void ystp(string filePath, string filePath_ystp) //压缩图片
{
//Bitmap
Bitmap bmp = null;
//ImageCoderInfo
ImageCodecInfo ici = null;
//Encoder
Encoder ecd = null;
//EncoderParameter
EncoderParameter ept = null;
//EncoderParameters
EncoderParameters eptS = null;
try
{
bmp = new Bitmap(filePath);
ici = this.getImageCoderInfo("image/jpeg");
ecd = Encoder.Quality;
eptS = new EncoderParameters(1);
ept = new EncoderParameter(ecd, 10L);
eptS.Param[0] = ept;
bmp.Save(filePath_ystp, ici, eptS);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
bmp.Dispose();
ept.Dispose();
eptS.Dispose();
}
}
private ImageCodecInfo getImageCoderInfo(string coderType)// 获取图片编码类型信息
{
ImageCodecInfo[] iciS = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo retIci = null; foreach (ImageCodecInfo ici in iciS)
{
if (ici.MimeType.Equals(coderType))
retIci = ici;
} return retIci;
}虽然压缩之后效果上有点差,不过勉强能用,不过更让人压抑的是,在本地通过,传到服务器之后确报错说什么System.NullReferenceException: 未将对象引用设置到对象的实例。定位在这句bmp.Dispose();这个明明是实例华了,为什么会没有呢?的却上传图片花了几秒中时间,难道是因为这几秒,这个对象自己已经释放了,可是这张图片没有上传成功啊,不知道是怎么回事,请各位帮忙,谢!
public void ystp(string filePath, string filePath_ystp) //压缩图片
{
//Bitmap
Bitmap bmp = null;
//ImageCoderInfo
ImageCodecInfo ici = null;
//Encoder
Encoder ecd = null;
//EncoderParameter
EncoderParameter ept = null;
//EncoderParameters
EncoderParameters eptS = null;
try
{
bmp = new Bitmap(filePath);
ici = this.getImageCoderInfo("image/jpeg");
ecd = Encoder.Quality;
eptS = new EncoderParameters(1);
ept = new EncoderParameter(ecd, 10L);
eptS.Param[0] = ept;
bmp.Save(filePath_ystp, ici, eptS);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
bmp.Dispose();
ept.Dispose();
eptS.Dispose();
}
}
private ImageCodecInfo getImageCoderInfo(string coderType)// 获取图片编码类型信息
{
ImageCodecInfo[] iciS = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo retIci = null; foreach (ImageCodecInfo ici in iciS)
{
if (ici.MimeType.Equals(coderType))
retIci = ici;
} return retIci;
}虽然压缩之后效果上有点差,不过勉强能用,不过更让人压抑的是,在本地通过,传到服务器之后确报错说什么System.NullReferenceException: 未将对象引用设置到对象的实例。定位在这句bmp.Dispose();这个明明是实例华了,为什么会没有呢?的却上传图片花了几秒中时间,难道是因为这几秒,这个对象自己已经释放了,可是这张图片没有上传成功啊,不知道是怎么回事,请各位帮忙,谢!
解决方案 »
- 如何禁止"a.aspx/admin"类似访问方式?
- 关于数据库更新UPDATE语法
- WEb开发环境 VS2005 OR DREAMWEAVER?
- 如何在服务端获取客户端操作动态生成控件(checkbox)的提交后状态?
- 多用户博客间的互相访问
- 为什么取不出输出参数?各位老大过来看看。
- 请问<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">问
- 能否对某个控件关闭回发的验证
- DataGrid新手问题!高手帮忙!!!!
- 高分求救:Request的filter的问题!!
- 模板列更新数据库
- 登录实现不了,请各位高手指点!
ImageCodecInfo ici = null;
//Encoder
Encoder ecd = null;
//EncoderParameter
EncoderParameter ept = null;
//EncoderParameters
EncoderParameters eptS = null;
try
{
ici = this.getImageCoderInfo("image/jpeg");
ecd = Encoder.Quality;
eptS = new EncoderParameters(1);
ept = new EncoderParameter(ecd, 10L);
eptS.Param[0] = ept;
bmp.Save(filePath_ystp, ici, eptS);
}
所有用户(包括代理)!购买1G空间联系客服免费赠送1个CN国际域名!
最终解释权归上海互通网络科技有限公司所有!
详情请访问:www.515dns.com
QQ:287895593