从WebSite中提取图像。 [英] Pull an image from a WebSite.

查看:82
本文介绍了从WebSite中提取图像。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从路径中拉出一张图片,格式为



www.MyImageSite.com/Images/Image1.jpg



我正在使用C#代码如下



I was Pulling an image from the path in the format

www.MyImageSite.com/Images/Image1.jpg

And I was using the C# code as follows

HttpWebRequest request = null;
                HttpWebResponse response = null;
                byte[] buff = null;

                string rootDir = GetAppSettingValue(Constants.K_APP_KEY_SC_IMAGE_PATH);
                string fullPath = rootDir + @"/" + fileName+ ".jpg";

                request = (HttpWebRequest)WebRequest.Create(fullPath);
                response = (HttpWebResponse)request.GetResponse();

                if (request.HaveResponse)
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        Stream receiveStream = response.GetResponseStream();
                        using (BinaryReader br = new BinaryReader(receiveStream))
                        {
                            buff = br.ReadBytes(500000);
                            br.Close();
                        }
                    }
                }

                return buff;







这里fileName是传递给函数的参数。



但现在我必须从网站上显示图像

MyImageSite.com/Images/[StockCode].jpg(没有www。)



有吗任何建议解决方案?




Here fileName is the argument passing to the function.

But now I have to show the image from the site
MyImageSite.com/Images/[StockCode].jpg (no www.)

Is there any suggestions solutions?

推荐答案

如果您只想从字符串中删除 www。,那么为什么不简单地从初始化中删除它string?



Stings暴露了一个函数,你可以在其中将字符串替换为其他格式。试试这个,



If you just wanted to remove the www. from the string, then why not simply remove it from the initial string?

Stings expose a function where you can replace a string to some other format. Try this,

// create a variable
string url = "www.example.com/images_folder/image_name.png";
// replace with an empty string
string urlWithoutWww = url.Replace("www.", "");





现在,您可以使用第二个不具有初始 www。的变量来显示您自己网站上的图像。



了解更多关于 String.Replace [ ^ ]。



Now you can use the second variable that won't have the initial www. in it to show the image from your own website.

Learn more on String.Replace[^].


这篇关于从WebSite中提取图像。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆