本文实例讲述了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#程序设计有所帮助。