其他分享
首页 > 其他分享> > 图片操作

图片操作

作者:互联网

1.修改网络上的图片大小

/// <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="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;            }
        }

标签:MemoryStream,stream,bm,bytes,ms,操作,byte,图片
来源: https://www.cnblogs.com/haigui-zx/p/16177567.html