十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本文实例为大家分享了C#根据http和ftp地址获取对应图片的具体代码,供大家参考,具体内容如下
从策划到设计制作,每一步都追求做到细腻,制作可持续发展的企业网站。为客户提供成都网站制作、成都做网站、网站策划、网页设计、主机域名、雅安服务器托管、网络营销、VI设计、 网站改版、漏洞修补等服务。为客户提供更好的一站式互联网解决方案,以客户的口碑塑造优易品牌,携手广大客户,共同发展进步。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); } } }
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。