时间:2021-05-20
本文实例为大家分享了C#根据http和ftp地址获取对应图片的具体代码,供大家参考,具体内容如下
public class GetBitmapImageClass { public BitmapSource GetImageHttp(string url,int width) { var image = new BitmapImage(); int BytesToRead = 100; if (!string.IsNullOrEmpty(url)) { WebRequest request = WebRequest.Create(new Uri(url, UriKind.Absolute)); request.Timeout = -1; WebResponse response = request.GetResponse(); Stream responseStream = response.GetResponseStream(); BinaryReader reader = new BinaryReader(responseStream); MemoryStream memoryStream = new MemoryStream(); byte[] bytebuffer = new byte[BytesToRead]; int bytesRead = reader.Read(bytebuffer, 0, BytesToRead); while (bytesRead > 0) { memoryStream.Write(bytebuffer, 0, bytesRead); bytesRead = reader.Read(bytebuffer, 0, BytesToRead); } image.BeginInit(); image.DecodePixelWidth = width; image.CacheOption = BitmapCacheOption.OnLoad; memoryStream.Seek(0, SeekOrigin.Begin); image.StreamSource = memoryStream; image.EndInit(); image.Freeze(); memoryStream.Close(); reader.Close(); responseStream.Close(); response.Close(); } return image; } public BitmapSource GetImageFtp(string url, int width) { var image = new BitmapImage(); if (!string.IsNullOrEmpty(url)) { FtpWebRequest reqFtp; reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(url)); reqFtp.Method = WebRequestMethods.Ftp.DownloadFile; reqFtp.UseBinary = true; FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse(); Stream ftpStream = response.GetResponseStream(); MemoryStream mStream = new MemoryStream(); ftpStream.CopyTo(mStream); mStream.Position = 0; int length = (int)mStream.Length; byte[] returnbyte = new byte[length]; mStream.Read(returnbyte, 0, length); mStream.Close(); ftpStream.Close(); response.Close(); System.IO.MemoryStream stream = new System.IO.MemoryStream(returnbyte); image.BeginInit(); image.DecodePixelWidth = width; image.CacheOption = BitmapCacheOption.OnLoad; stream.Seek(0, SeekOrigin.Begin); image.StreamSource = stream; image.EndInit(); image.Freeze(); stream.Close(); } return image; } [DllImport("gdi32.dll", SetLastError = true)] private static extern bool DeleteObject(IntPtr hObject); public BitmapSource ToBitmapSource(System.Drawing.Bitmap bmp) { try { var ptr = bmp.GetHbitmap(); var source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( ptr, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); DeleteObject(ptr); return source; } catch { return null; } } //获取缩略图 public BitmapSource GetBitImage(string imageLink) { //"http://172.17.1.231:8083/3050273262379466760/2017/05/28/09/340800100999/09163448402.jpg?fid=1267520" if (imageLink.StartsWith("http://")) { return GetImageHttp(imageLink,200); } //ftp格式的 else if (imageLink.StartsWith("ftp://")) { return GetImageFtp(imageLink, 200); } } //获取原图 public BitmapSource GetHightBitImage(string imageLink) { //"http://172.17.1.231:8083/3050273262379466760/2017/05/28/09/340800100999/09163448402.jpg?fid=1267520" if (imageLink.StartsWith("http://")) { return GetImageHttp(imageLink, 0); } //ftp格式的 else if (imageLink.StartsWith("ftp://")) { return GetImageFtp(imageLink, 0); } } }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
获取指定页面中的图片地址:curlnews.baidu.com|grep-Eio'(http|ftp|https)://[A-Za-z0-9_./]+(.jpg
图片地址获取步骤如下: 1、新建一个txt文档,用于保持获取的图片地址; 2、登录一个网页,点击右键,会弹出一个菜单; 3、在这个菜单里面,找到复制图片地
利用python抓取网络图片的步骤是:1、根据给定的网址获取网页源代码2、利用正则表达式把源代码中的图片地址过滤出来3、根据过滤出来的图片地址下载网络图片以下是
核心代码",$str);?>PHP获取所有图片地址正则表达式不带图片的所有图片地址匹配:preg_match_all('/(\s+src\s?\
源码部分对应的图片地址改为自己的图片地址:HTML代码:1/3文本12/3文本23/3文本3??