vb.net 在VB.NET中使用WebRequest将URL保存为图像缩略图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/379512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using WebRequest to save URL as Image thumbnail in VB.NET
提问by DaveJustDave
I'm trying to write a simple routine where I pass it a URL and it goes and renders the content of the webresponse as a jpg. I found a solution somehwere in C# and ported it to vb.net, however when I run it, it throws an argumentexception "parameter is not valid" when trying to instantiate the image. Can someone take a look at the following code and let me know if I'm on the right track?
我正在尝试编写一个简单的例程,我向它传递一个 URL,然后它将 webresponse 的内容呈现为 jpg。我在 C# 中找到了一个解决方案并将其移植到 vb.net,但是当我运行它时,它在尝试实例化图像时抛出参数异常“参数无效”。有人可以看看下面的代码,让我知道我是否在正确的轨道上?
Sub SaveUrl(ByVal aUrl As String)
Dim response As WebResponse
Dim remoteStream As Stream
Dim readStream As StreamReader
Dim request As WebRequest = WebRequest.Create(aUrl)
response = request.GetResponse
remoteStream = response.GetResponseStream
readStream = New StreamReader(remoteStream)
Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(remoteStream)
img.Save(aUrl & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
response.Close()
remoteStream.Close()
readStream.Close()
End Sub
To Clarify:Yes, I know i need a LOT more code to accomplish what I want to do, which is to render/take a screen capture of a URL (html, images, all the markup, everything) and save it as a jpg thumbnail.
澄清:是的,我知道我需要更多的代码来完成我想做的事情,即渲染/获取 URL(html、图像、所有标记、所有内容)的屏幕截图并将其另存为 jpg缩略图。
If you've used Google Chrome, you've seen the launch page that has thumbnails of all the sites you use frequently. Something like that.
如果您使用过 Google Chrome,您就会看到启动页面,其中包含您经常使用的所有网站的缩略图。类似的东西。
Update:Ok I've found commercial paid products to accomplish this, like http://www.websitesscreenshot.com/Index.htmlbut no open source implementations.
更新:好的,我找到了商业付费产品来实现这一点,例如http://www.websitesscreenshot.com/Index.html但没有开源实现。
回答by Joel Coehoorn
Just because the Image class has a .FromStream()
method doesn't mean that anystream is a valid image, and the html response from a web server certainly would not be a valid image. Rendering a web page is not a "simple routine". There's a lot that goes into it.
仅仅因为 Image 类有一个.FromStream()
方法并不意味着任何流都是有效的图像,并且来自 Web 服务器的 html 响应肯定不是有效的图像。呈现网页不是一个“简单的例程”。有很多内容。
You might try using the WebBrowser control, since that can do a lot of the work for you.
您可以尝试使用 WebBrowser 控件,因为它可以为您完成很多工作。
回答by Konrad Rudolph
Well, either:
好吧,要么:
My.Computer.Network.DownloadFile("http://example.com/file.jpeg", "local.jpeg")
Or, to createa thumbnail:
或者,要创建缩略图:
Using wc As New Net.WebClient()
Dim img = Image.FromStream(wc.OpenRead("http://example.com/file"))
img.GetThumbnailImage(32, 32, Function() False, Nothing).Save("c:\local.jpg")
End Using
… maybe add some error handling. ;-)
...也许添加一些错误处理。;-)
回答by Konrad Rudolph
I've just found a German code snippet on ActiveVBthat creates a screenshot form a web site. Perhaps you can build on that? Unfortunately, the code is not only explained in German, but also in VB6. Still, the logic is essentially the same and shouldn't be hard to port to .NET. Hope that helps.
我刚刚在 ActiveVB 上找到了一个德语代码片段,它从网站上创建了一个屏幕截图。也许你可以以此为基础?不幸的是,代码不仅用德语解释,而且用 VB6 解释。尽管如此,逻辑本质上是相同的,并且应该不难移植到 .NET。希望有帮助。
回答by Yuliy
What are you trying to do?
你想做什么?
Are you trying convert a web page to JPEG? This will require a bit more code, as what your code is trying to do is to download an already existing image (such as a gif, png, or even another jpeg) and converts it to jpeg. You would need to have something render the HTML document, then you would need to capture an image of the rendered document, and then save that as a JPEG.
您是否正在尝试将网页转换为 JPEG?这将需要更多的代码,因为您的代码试图做的是下载已经存在的图像(例如 gif、png 或什至另一个 jpeg)并将其转换为 jpeg。您需要对 HTML 文档进行渲染,然后您需要捕获渲染文档的图像,然后将其保存为 JPEG。
回答by Zachary Yates
回答by Matt Hamilton
I know you're asking for VB, but here's some C# code that I use in a project of mine to capture local thumbnails from image URLs (asynchronously). I've tried to strip out all the project-specific stuff so it makes sense as a stand-alone example.
我知道您要求使用 VB,但这里有一些 C# 代码,我在我的一个项目中使用它来从图像 URL(异步)捕获本地缩略图。我试图去掉所有特定于项目的东西,所以它作为一个独立的例子是有意义的。
var wc = new WebClient();
wc.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
wc.DownloadDataCompleted += wc_DownloadDataCompleted;
wc.DownloadDataAsync(imageProvider.Image, "yourFilenameHere");
And here's the wc_DownloadDataCompleted event handler:
这是 wc_DownloadDataCompleted 事件处理程序:
private void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
{
var filename = e.UserState.ToString();
using (var ms = new MemoryStream(e.Result))
{
var bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.DecodePixelWidth = 80; // _maxThumbnailWidth;
bi.EndInit();
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bi));
var filename = Path.Combine(_thumbFolder, filename);
try
{
using (var fs = new FileStream(filename, FileMode.Create))
{
encoder.Save(fs);
}
}
catch { }
}
}
}
Edit
编辑
I guess I should add that this was in a WPF application - hence the use of "BitmapImage" and "BitmapFrame" to decode the stream. So it may not help you. I'm gonna leave it here anyway.
我想我应该补充一点,这是在 WPF 应用程序中 - 因此使用“BitmapImage”和“BitmapFrame”来解码流。所以它可能对你没有帮助。反正我要把它留在这里。
回答by Nox
Or you could just do this:
或者你可以这样做:
Dim webclient As New Net.WebClient
Dim FileName As String = "C:\Share\local.Gif"
webclient.DownloadFile(URI, FileName)
PictureBox1.Image = Image.FromFile(FileName)