首页 > 编程 > C# > 正文

C#判断一个图像是否是透明的GIF图的方法

2020-01-24 01:43:10
字体:
来源:转载
供稿:网友

本文实例讲述了C#判断一个图像是否是透明的GIF图的方法。分享给大家供大家参考。具体如下:
1. 使用方法如下:

System.Drawing.Image objImage = DownloadImage("https://www.google.com/images/srpr/logo3w.png");if (IsTransparentPalette(objImage.Palette)) {//your code....}

2. C#代码如下:

public bool IsTransparentPalette(System.Drawing.Imaging.ColorPalette palette){  if (palette.Flags!= 1 )    return false;  int total_colors = palette.Entries.GetLength(0);  for (int i = 0; i < total_colors - 1; i++)  {    if (palette.Entries[i].A != 0)    {      return false;    }  }  return true;}public System.Drawing.Image DownloadImage(string url){  System.Drawing.Image tmpImage = null;  try  {    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);    request.AllowWriteStreamBuffering = true;    request.UserAgent = UserAgent;    request.Accept = "GET HTTP/1.1";    request.Timeout = 2000;    System.Net.WebResponse webResponse = request.GetResponse();    System.IO.Stream webStream = webResponse.GetResponseStream();    if (webStream != null) tmpImage = System.Drawing.Image.FromStream(webStream);    webResponse.Close();    webResponse.Close();  }  catch (Exception exception)  {    return null;  }  return tmpImage;}

希望本文所述对大家的C#程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表