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

前端 未结 6 953
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-13 06:53

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

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

6条回答
  •  悲哀的现实
    2020-12-13 07:39

    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.

    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

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

提交回复
热议问题