程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C# 根據URL獲取圖片信息/縮略圖

C# 根據URL獲取圖片信息/縮略圖

編輯:C#基礎知識
C# 根據URL返回HTML_根據URL獲取圖片信息/縮略圖
/// <summary>
/// 根據URL 返回HTML
/// </summary>
private List<string> GetHtmlByUrl(string Url)
{
WebClient client = new WebClient();
byte[] page = client.DownloadData(Url);
string content = System.Text.Encoding.UTF8.GetString(page);
List<string> _list = GetImgUrlByHtmlText(content);
return _list;
}

 

 

/---------------------根據URL--獲取圖片信息--------------/

//後綴名
string _suffix = _imgurls[i].Substring(_imgurls[i].Length - 4);
//圖片文件名
string _imageInputTitle = "雲鼎寶貝" + DateTime.Now.ToString("yyyyMMddhhmmss" + i) + _suffix;
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(_imgurls[i]);
webRequest.Method = "GET";
System.Net.HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
//獲取圖片保存到流
System.IO.Stream _stream = webResponse.GetResponseStream();
//保存到圖片
System.Drawing.Image _image = System.Drawing.Image.FromStream(_stream);

// int _picwidth = _imgWidth.Length <= 0 ? _image.Width : Convert.ToInt32(_imgWidth);
// int _picheight = _imgHeight.Length <= 0 ? _image.Height : Convert.ToInt32(_imgHeight);

//根據參數形成縮略圖
//_image = _image.GetThumbnailImage(_image.Width, _image.Height, null, IntPtr.Zero);

//實例化MemoryStream
System.IO.MemoryStream _ImageMem = new System.IO.MemoryStream();

//實例化圖片格式
//ImageFormat _imgFormat = new ImageFormat(new Guid());

//根據圖片後綴名判斷類型.
// if (_suffix.ToLower().Equals(".jpg")) _imgFormat = ImageFormat.Jpeg;
// if (_suffix.ToLower().Equals(".png")) _imgFormat = ImageFormat.Png;
// if (_suffix.ToLower().Equals(".gif")) _imgFormat = ImageFormat.Gif;

//保存
_image.Save(_ImageMem, _image.RawFormat);
//轉換為字節流
byte[] _ImageBytes = _ImageMem.GetBuffer();
//釋放資源
_image.Dispose();
//關閉流
_stream.Close();

 

 

 

/------------圖片生成縮略圖--------------?

#region  生成縮略圖  
  ///<summary>  
  /// 生成縮略圖  
  /// </summary>  
  /// <param name="originalImagePath">源圖路徑(物理路徑)</param>  
  /// <param name="thumbnailPath">縮略圖路徑(物理路徑)</param>  
  /// <param name="width">縮略圖寬度</param>  
  /// <param name="height">縮略圖高度</param>  
  /// <param name="mode">生成縮略圖的方式</param>      
  public  void MakeThumbnail(string originalImagePath,  string thumbnailPath, int width, int height, string mode out string outthumbnailPath)  
  {  
   System.Drawing.Image originalImage =System.Drawing.Image.FromFile(originalImagePath);  
             
   int towidth = width;  
   int toheight = height;  
         
   int x = 0;  
   int y = 0;  
   int ow = originalImage.Width;  
   int oh = originalImage.Height;         
   switch (mode)  
   {          
    case "HW"://指定高寬縮放(可能變形)                  
     break;  
    case "W"://指定寬,高按比例                      
     toheight = originalImage.Height * width/originalImage.Width;  
     break;  
    case "H"://指定高,寬按比例  
     towidth = originalImage.Width * height/originalImage.Height;                      
     break;          
    case "Cut"://指定高寬裁減(不變形)                  
     if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight)  
     {  
      oh = originalImage.Height;  
      ow = originalImage.Height*towidth/toheight;  
      y = 0;  
      x = (originalImage.Width - ow)/2;  
     }  
     else  
     {  
      ow = originalImage.Width;  
      oh = originalImage.Width*height/towidth;  
      x = 0;  
      y = (originalImage.Height - oh)/2;  
     }  
     break;                      
    default :  
     break;  
   }      
             
   //新建一個bmp圖片  
   System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth,toheight); 
   //新建一個畫板  
   Graphics g = System.Drawing.Graphics.FromImage(bitmap); 
   //設置高質量插值法  
   g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 
   //設置高質量,低速度呈現平滑程度  
   g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
   //清空畫布並以透明背景色填充  
   g.Clear(Color.Transparent);         
   //在指定位置並且按指定大小繪制原圖片的指定部分  
   g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),  
    new Rectangle(x, y, ow,oh),  
    GraphicsUnit.Pixel); 
   try  
   {              
    //以jpg格式保存縮略圖  
    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);  
    outthumbnailPath=thumbnailPath; 
   }  
   catch(System.Exception e)  
   {  
    throw e;  
   }  
   finally  
   {  
    originalImage.Dispose();  
    bitmap.Dispose();                          
    g.Dispose();  
   }  
  } 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved