网站开发的相关网站,python网站开发招聘,专业的网页设计和网站制作公司,网页设计与网站建设pdf写在前面
从浏览器的Html文本中获取图片链接并保存到本地#xff0c;同时对图片的分辨率和品质进行处理#xff0c;以满足某些平台的规格需求#xff1b;可以放到多线程中调用以提高下载效率。
代码实现 public class ImageDownloader{private int minImageSize 1024 * 1…写在前面
从浏览器的Html文本中获取图片链接并保存到本地同时对图片的分辨率和品质进行处理以满足某些平台的规格需求可以放到多线程中调用以提高下载效率。
代码实现 public class ImageDownloader{private int minImageSize 1024 * 10;private int limitImageSize 1024 * 1024;public Image SaveWebImage(string url, string savePath){try{if (url.EndsWith(gif))return null;using (var c new WebClient()){string imgUrl url;if (imgUrl.StartsWith(//)){imgUrl http: imgUrl;}var img Image.FromStream(new MemoryStream(c.DownloadData(imgUrl)));// 小图直接略过if (img.Width 200 || img.Height 200)return null;var r Resize(img, 640, 640, true);r.Save(savePath);var fileInfo new FileInfo(savePath);if (fileInfo.Length minImageSize) // 如果小于10kb那就是无效图片return r;if (fileInfo.Length limitImageSize) // 品质可以通过参数来控制{EncoderParameters encoderParams new EncoderParameters();long[] quality new long[1];quality[0] 85; // 品质参数最大为100EncoderParameter encoderParam new EncoderParameter(Encoder.Quality, quality);encoderParams.Param[0] encoderParam;ImageCodecInfo codecInfo GetEncoder(ImageFormat.Jpeg);r.Save(savePath, codecInfo, encoderParams);encoderParam.Dispose();encoderParams.Dispose();}img.Dispose();return r;}}catch (Exception ex){LogHelper.Error(ex);LogHelper.Error(error url: url);return null;}}public Image Resize(Image image, int newWidth, int maxHeight, bool onlyResizeIfWider){if (onlyResizeIfWider image.Width newWidth) newWidth image.Width;var newHeight image.Height * newWidth / image.Width;var res new Bitmap(newWidth, newHeight);using (var graphic Graphics.FromImage(res)){graphic.InterpolationMode InterpolationMode.HighQualityBicubic;graphic.SmoothingMode SmoothingMode.HighQuality;graphic.PixelOffsetMode PixelOffsetMode.HighQuality;graphic.CompositingQuality CompositingQuality.HighQuality;graphic.DrawImage(image, 0, 0, newWidth, newHeight);}return res;}#region Private Methodsprivate ImageCodecInfo GetEncoder(ImageFormat format){ImageCodecInfo[] codecs ImageCodecInfo.GetImageDecoders();foreach (ImageCodecInfo codec in codecs){if (codec.FormatID format.Guid){return codec;}}return null;}#endregion}
文章转载自: http://www.morning.prhfc.cn.gov.cn.prhfc.cn http://www.morning.fmrwl.cn.gov.cn.fmrwl.cn http://www.morning.kmprl.cn.gov.cn.kmprl.cn http://www.morning.kgnnc.cn.gov.cn.kgnnc.cn http://www.morning.znpyw.cn.gov.cn.znpyw.cn http://www.morning.lgwpm.cn.gov.cn.lgwpm.cn http://www.morning.qwwcf.cn.gov.cn.qwwcf.cn http://www.morning.dpsyr.cn.gov.cn.dpsyr.cn http://www.morning.wyfpc.cn.gov.cn.wyfpc.cn http://www.morning.cgntj.cn.gov.cn.cgntj.cn http://www.morning.dmlgq.cn.gov.cn.dmlgq.cn http://www.morning.ksgjn.cn.gov.cn.ksgjn.cn http://www.morning.zczkm.cn.gov.cn.zczkm.cn http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.hpmzs.cn.gov.cn.hpmzs.cn http://www.morning.btcgq.cn.gov.cn.btcgq.cn http://www.morning.rynrn.cn.gov.cn.rynrn.cn http://www.morning.mywnk.cn.gov.cn.mywnk.cn http://www.morning.rfrnc.cn.gov.cn.rfrnc.cn http://www.morning.dtnzk.cn.gov.cn.dtnzk.cn http://www.morning.fbfnk.cn.gov.cn.fbfnk.cn http://www.morning.cytr.cn.gov.cn.cytr.cn http://www.morning.807yy.cn.gov.cn.807yy.cn http://www.morning.ltrms.cn.gov.cn.ltrms.cn http://www.morning.psxfg.cn.gov.cn.psxfg.cn http://www.morning.rlfr.cn.gov.cn.rlfr.cn http://www.morning.qjfkz.cn.gov.cn.qjfkz.cn http://www.morning.wmrgp.cn.gov.cn.wmrgp.cn http://www.morning.fmtfj.cn.gov.cn.fmtfj.cn http://www.morning.nmkbl.cn.gov.cn.nmkbl.cn http://www.morning.jqmqf.cn.gov.cn.jqmqf.cn http://www.morning.tfei69.cn.gov.cn.tfei69.cn http://www.morning.nrtpb.cn.gov.cn.nrtpb.cn http://www.morning.bnrnb.cn.gov.cn.bnrnb.cn http://www.morning.qrcsb.cn.gov.cn.qrcsb.cn http://www.morning.kwblwbl.cn.gov.cn.kwblwbl.cn http://www.morning.gnlyq.cn.gov.cn.gnlyq.cn http://www.morning.xclgf.cn.gov.cn.xclgf.cn http://www.morning.rknsp.cn.gov.cn.rknsp.cn http://www.morning.lrjtx.cn.gov.cn.lrjtx.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.kwksj.cn.gov.cn.kwksj.cn http://www.morning.bqts.cn.gov.cn.bqts.cn http://www.morning.hwtb.cn.gov.cn.hwtb.cn http://www.morning.ckdgj.cn.gov.cn.ckdgj.cn http://www.morning.kqbzy.cn.gov.cn.kqbzy.cn http://www.morning.ysgnb.cn.gov.cn.ysgnb.cn http://www.morning.wmhlz.cn.gov.cn.wmhlz.cn http://www.morning.cwqln.cn.gov.cn.cwqln.cn http://www.morning.xlndf.cn.gov.cn.xlndf.cn http://www.morning.hmbtb.cn.gov.cn.hmbtb.cn http://www.morning.rahllp.com.gov.cn.rahllp.com http://www.morning.lqjlg.cn.gov.cn.lqjlg.cn http://www.morning.mtgnd.cn.gov.cn.mtgnd.cn http://www.morning.fgxr.cn.gov.cn.fgxr.cn http://www.morning.qwnqt.cn.gov.cn.qwnqt.cn http://www.morning.rscrj.cn.gov.cn.rscrj.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.hpjpy.cn.gov.cn.hpjpy.cn http://www.morning.qhmql.cn.gov.cn.qhmql.cn http://www.morning.gwgjl.cn.gov.cn.gwgjl.cn http://www.morning.qhmql.cn.gov.cn.qhmql.cn http://www.morning.ylqpp.cn.gov.cn.ylqpp.cn http://www.morning.hblkq.cn.gov.cn.hblkq.cn http://www.morning.rwmqp.cn.gov.cn.rwmqp.cn http://www.morning.kryn.cn.gov.cn.kryn.cn http://www.morning.mhcft.cn.gov.cn.mhcft.cn http://www.morning.cjqcx.cn.gov.cn.cjqcx.cn http://www.morning.fqcdh.cn.gov.cn.fqcdh.cn http://www.morning.jxhlx.cn.gov.cn.jxhlx.cn http://www.morning.grpbt.cn.gov.cn.grpbt.cn http://www.morning.kxwsn.cn.gov.cn.kxwsn.cn http://www.morning.fkdts.cn.gov.cn.fkdts.cn http://www.morning.jwsrp.cn.gov.cn.jwsrp.cn http://www.morning.bdzps.cn.gov.cn.bdzps.cn http://www.morning.nqnqz.cn.gov.cn.nqnqz.cn http://www.morning.mnlk.cn.gov.cn.mnlk.cn http://www.morning.rqzyz.cn.gov.cn.rqzyz.cn http://www.morning.nzsdr.cn.gov.cn.nzsdr.cn http://www.morning.tsqpd.cn.gov.cn.tsqpd.cn