Fransic

导航

C#读取图片的方法--学习笔记

在从事Web开发时,面对图片的数据来源一般是两种方式:1、磁盘;2、内存(数据流);3、网络(域名的获取方式);

总结下分别读取这两种数据来源的方法:

1、磁盘:

  System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

2、内存中的数据流Stream:

  System.Drawing.Image originalImage = System.Drawing.Image.FromStream(originalImageStream);

3、网络:

  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(originalImagePath);
      request.UserAgent = "MOZILLA/4.0 (COMPATIBLE; MSIE 7.0; WINDOWS NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
      request.Method = "GET";

  HttpWebResponse response = (HttpWebResponse)request.GetResponse();

  System.Drawing.Image originalImage = System.Drawing.Image.FromStream(response.GetResponseStream());

posted on 2011-03-04 16:25  Fransic  阅读(1320)  评论(0编辑  收藏  举报