2012-03-08 11 views

回答

1

首先下载从URL的形象然后将它保存在数据库

 string tnail = ""; 
    WebClient client = new WebClient(); 
    using (Image src = Image.FromFile("http://www.example.com/image.jpg")) 
         { 
          using (Bitmap dst = new Bitmap(25, 33)) 
          { 
           using (Graphics g = Graphics.FromImage(dst)) 
           { 
            g.SmoothingMode = SmoothingMode.HighQuality; 
            g.InterpolationMode = InterpolationMode.High; 
            g.DrawImage(src, 0, 0, dst.Width, dst.Height); 
           } 
           tnail = tname; 
           tnail = Path.ChangeExtension(tnail, null); 
           tnail += "_thumbnail"; 
           tnail = Path.ChangeExtension(tnail, "jpg"); 
           dst.Save(Path.Combine(Server.MapPath(imagepath), tnail), ImageFormat.Jpeg); 

          } 
         } 
+0

'Image.FromFile()'不接受URI作为参数,因为它会抛出'ArgumentException':HTTP:// msdn.microsoft.com/en-us/library/stf701f5(v=vs.110).aspx – Monica 2014-08-16 19:31:37

0

如果我们假设你有一个持久化实体(以下称为ImageFile),有代表文件的字节数组属性,你可以使用[WebClient.DownloadData][1]

using(WebClient client = new WebClient()) 
{ 
    var img = new ImageFile(); 
    img.Data = client.DownloadData("http://www.example.com/image.jpg"); 

    // continue with saving your file.  
} 
相关问题