首页 技术 正文
技术 2022年11月8日
0 收藏 446 点赞 2,061 浏览 2212 个字
   /// <summary>
/// 压缩图片
/// </summary>
/// <param name="iSource">图片文件</param>
/// <param name="outPath">路径</param> 例如 var outPath = Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(file.FileName));
/// <param name="flag">值越大代表图片质量越好,一般默认控制在50为最佳压缩质量</param>
/// <returns></returns>
public static bool ReduceImage(Image iSource, string outPath, int flag)
{
ImageFormat tFormat = iSource.RawFormat;
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = flag;
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageDecoders();
ImageCodecInfo jpegICIinfo = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[x];
break;
}
}
if (jpegICIinfo != null)
iSource.Save(outPath, jpegICIinfo, ep);
else
iSource.Save(outPath, tFormat);
return true;
}
catch
{
return false;
}
finally
{
iSource.Dispose();
}
}

  图片类型转换

 /// <summary>
///byte[] 图片转换
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static Image BytesToImage(byte[] buffer)
{
MemoryStream ms = new MemoryStream(buffer);
Image image = System.Drawing.Image.FromStream(ms);
return image;
}

  

  /// <summary>
/// 使用流方式,上传并保存文件
/// </summary>
/// <param name="s">文件流</param>
/// <param name="savepath">保存文件路径</param>
/// <param name="warning">处理警告信息,若为空,则处理成功</param>
/// <returns>返回文件保存完整路径</returns>
public static string uploadbystream(Stream s, string savepathFolder, out string warning)
{
try
{
int be = 0;
if (s.Length > 0)
{
MemoryStream ms = new MemoryStream(); while ((be = s.ReadByte()) != -1)
{
ms.WriteByte((byte)be);
}
string reletiveFolder = "UploadFile\\ManageImage\\" + savepathFolder + "\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month;
string folderPath = HttpContext.Current.Server.MapPath("~/") + reletiveFolder;
if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
string newpath = folderPath + "\\" + filename; string lastpath = "";
lastpath = newpath;
//FileExistMapPath(newpath, FileCheckModel.M, out lastpath); FileStream fs = new FileStream(lastpath, FileMode.Create); ms.WriteTo(fs); //释放
ms.Dispose();
fs.Dispose(); warning = "";
return reletiveFolder + "\\" + filename;
} warning = "error";
return "";
}
catch (Exception ex)
{
warning = "error" + ex.Message;
return "";
}
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848