C# 如何将图像 url 转换为 system.drawing.image

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11801630/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:19:08  来源:igfitidea点击:

How can I convert image url to system.drawing.image

c#asp.net.netvb.netitextsharp

提问by Mina Gabriel

I'm using VB.NetI have an url of an image, let's say http://localhost/image.gif

我正在使用VB.Net我有一个图片的网址,让我们说http://localhost/image.gif

I need to create a System.Drawing.Image object from that file.

我需要从该文件创建一个 System.Drawing.Image 对象。

Noticesave this to a file and then open it is not one of my options also i'm using ItextSharp

请注意将其保存到文件然后打开它不是我的选项之一,我也在使用ItextSharp

here is my code :

这是我的代码:

Dim rect As iTextSharp.text.Rectangle
        rect = iTextSharp.text.PageSize.LETTER
        Dim x As PDFDocument = New PDFDocument("chart", rect, 1, 1, 1, 1)

        x.UserName = objCurrentUser.FullName
        x.WritePageHeader(1)
        For i = 0 To chartObj.Count - 1
            Dim chartLink as string = "http://localhost/image.gif"
            x.writechart( ** it only accept system.darwing.image ** ) 

        Next

        x.WritePageFooter()
        x.Finish(False)

采纳答案by Varius

You could use WebClient class to download image and then MemoryStream to read it:

您可以使用 WebClient 类下载图像,然后使用 MemoryStream 来读取它:

C#

C#

WebClient wc = new WebClient();
byte[] bytes = wc.DownloadData("http://localhost/image.gif");
MemoryStream ms = new MemoryStream(bytes);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);

VB

VB

Dim wc As New WebClient()
Dim bytes As Byte() = wc.DownloadData("http://localhost/image.gif")
Dim ms As New MemoryStream(bytes)
Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(ms)

回答by Guilherme de Jesus Santos

You can try this to get the Image

你可以试试这个来获取图像

Dim req As System.Net.WebRequest = System.Net.WebRequest.Create("[URL here]")
Dim response As System.Net.WebResponse = req.GetResponse()
Dim stream As Stream = response.GetResponseStream()

Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(stream)
stream.Close()

回答by blang32

Dim c As New System.Net.WebClient
Dim FileName As String = "c:\StackOverflow.png"
c.DownloadFile(New System.Uri("http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=5"), FileName)
Dim img As System.Drawing.Image
img = System.Drawing.Image.FromFile(FileName)

回答by VahidN

iTextSharp is able to accept Uri's:

iTextSharp 能够接受 Uri 的:

Image.GetInstance(uri)

回答by T_D

The other answers are also correct, but it hurts to see the Webclient and MemoryStream not getting disposed, I recommend putting your code in a using.

其他答案也是正确的,但是看到 Webclient 和 MemoryStream 没有被处理会很痛苦,我建议将您的代码放在using.

Example code:

示例代码:

using (var wc = new WebClient())
{
    using (var imgStream = new MemoryStream(wc.DownloadData(imgUrl)))
    {
        using (var objImage = Image.FromStream(imgStream))
        {
            //do stuff with the image
        }
    }
}

The required imports at top of your file are System.IO, System.Net& System.Drawing

文件顶部所需的导入是System.IO, System.Net&System.Drawing

In VB.net the syntax was using wc as WebClient = new WebClient() {etc

在 VB.net 中,语法是using wc as WebClient = new WebClient() {

回答by Filix Mogilevsky

You can use HttpClient and accomplish this task async with few lines of code.

您可以使用 HttpClient 并通过几行代码异步完成此任务。

public async Task<Bitmap> GetImageFromUrl(string url)
    {
        var httpClient = new HttpClient();
        var stream = await httpClient.GetStreamAsync(url);
        return new Bitmap(stream);
    }