您的位置:首页 > 其它

winfrom PictureBox控件显示GIF图片异常解决方案

2013-06-04 11:02 405 查看
问题:最近开发winfrom项目显示图片用到PictureBox控件,同样一张图片,在win7跟XP电脑上运行得到的结果不一样,经过调试发现XP系统上用PictureBox显示GIF会出现bug(目前在win7使用PictureBox控件没有出现这个问题)

解决方案:

我的解决方案是重写这个控件,测试发现GIF帧数大于10以上的,都会出现问题。

下面的方法为我解决这个bug

/// <summary>
/// 显示图片方法
/// </summary>
/// <param name="url"></param>
private  void ImgShow(string url)
{
if (url != null)
{
try
{
System.Net.WebClient web = new System.Net.WebClient();
byte[] buffer = web.DownloadData(url);
web.Dispose();
System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);
Image img = Image.FromStream(ms, true, true);
FrameDimension fd = new FrameDimension(img.FrameDimensionsList[0]);
int count = img.GetFrameCount(fd);//获取帧数(gif图片可能包含多帧,其它格式图片一般仅一帧)
if (count <= 10)
{
base.Image = img;
}
else
{
img.SelectActiveFrame(fd, 0);
base.Image = img.GetThumbnailImage(img.Width, img.Height, () => { return false; }, IntPtr.Zero);
}
}
catch (Exception)
{
base.Image = null;
}

}
else
{
base.Image = null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: