打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c# 改变图片的大小(w,h)

本文介绍获取网络上的图片将其大小尺寸改成自己想要的

   /// <summary>        /// 图片大小裁剪        /// </summary>        /// <param name="filePath"></param>        /// <returns></returns>        public byte[] ResizeImage(string filePath)        {            WebRequest request = (WebRequest)HttpWebRequest.Create(filePath);            WebResponse response = request.GetResponse();            using (Stream stream = response.GetResponseStream())            {                Bitmap bm = (Bitmap)Image.FromStream(stream);                bm = GetThumbnail(bm, 50, 40);                MemoryStream ms = new MemoryStream();                bm.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);                byte[] bytes = ms.GetBuffer();  //byte[]   bytes=   ms.ToArray(); 这两句都可以,至于区别么,下面有解释                ms.Close();                return bytes;            }        }
  /// <summary>        /// 修改图片的大小        /// </summary>        /// <param name="b"></param>        /// <param name="destHeight"></param>        /// <param name="destWidth"></param>        /// <returns></returns>        public Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidth)        {            System.Drawing.Image imgSource = b;            System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat;            int sW = 0, sH = 0;            // 按比例缩放                       int sWidth = imgSource.Width;            int sHeight = imgSource.Height;            if (sHeight > destHeight || sWidth > destWidth)            {                if ((sWidth * destHeight) > (sHeight * destWidth))                {                    sW = destWidth;                    sH = (destWidth * sHeight) / sWidth;                }                else                {                    sH = destHeight;                    sW = (sWidth * destHeight) / sHeight;                }            }            else            {                sW = sWidth;                sH = sHeight;            }            Bitmap outBmp = new Bitmap(destWidth, destHeight);            Graphics g = Graphics.FromImage(outBmp);            g.Clear(Color.Transparent);            // 设置画布的描绘质量                     g.CompositingQuality = CompositingQuality.HighQuality;            g.SmoothingMode = SmoothingMode.HighQuality;            g.InterpolationMode = InterpolationMode.HighQualityBicubic;            g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);            g.Dispose();            // 以下代码为保存图片时,设置压缩质量                 EncoderParameters encoderParams = new EncoderParameters();            long[] quality = new long[1];            quality[0] = 100;            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);            encoderParams.Param[0] = encoderParam;            imgSource.Dispose();            return outBmp;        }

 asp.net mvc 前台调用

 /// <summary>        /// 得到图像        /// </summary>        /// <param name="path"></param>        /// <returns></returns>        public FileResult GetAvator(string path)        {            return File(service.ResizeImage(path), "image/png");        }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
网站性能优化:动态缩略图技术实现思路
等比例缩放图片(C#)
Java的图片处理工具类
vfp9终结一直以来的打印纸张设置
javascript 自制提示框
微信小程序canvas画布转化出来的图片模糊问题的解决方案
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服