首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

请帮忙给小弟我看上图片处理函数为什么只能处理png图片,很是困惑

2012-11-01 
请帮忙给我看下图片处理函数为什么只能处理png图片,在线等,很是困惑功能:缩小图片另存public void SmallPi

请帮忙给我看下图片处理函数为什么只能处理png图片,在线等,很是困惑
功能:缩小图片另存 
public void SmallPic(string strOldPic, string strNewPic, int intWidth)
  {

  System.Drawing.Bitmap objPic, objNewPic;
  try
  {
  objPic = new System.Drawing.Bitmap(strOldPic);
  int intHeight = (intWidth / objPic.Width) * objPic.Height;
  objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
  objNewPic.Save(strNewPic);  
  // objNewPic.Save(strNewPic, System.Drawing.Imaging.ImageFormat.Gif);

  }
  catch (Exception exp) { throw exp; }
  finally
  {
  objPic = null;
  objNewPic = null;
  }
  }

调用SmallPic("G:\\My Documents\\Visual Studio 2008\\Projects\\opckb_net\\opckb_net\\upload\\UserFiles\\1.png", "G:\\My Documents\\Visual Studio 2008\\Projects\\opckb_net\\opckb_net\\upload\\UserFiles\\1_140.png", 140);
能成功执行
问题:其他格式例如jpg、gif出错
SmallPic("G:\\My Documents\\Visual Studio 2008\\Projects\\opckb_net\\opckb_net\\upload\\UserFiles\\2.gif", "G:\\My Documents\\Visual Studio 2008\\Projects\\opckb_net\\opckb_net\\upload\\UserFiles\\2_140.gif", 140);

System.ArgumentException: 参数无效。

  }
行 94: catch (Exception exp) { throw exp; }行 95: finally
行 96: {

[解决办法]
你直接用objNewPic.Save(strNewPic);
手工加扩展名,不要用System.Drawing.Imaging.ImageFormat.Gif来试试。
[解决办法]

C# code
      //功能:缩小图片另存          public void SmallPic(string strOldPic, string strNewPic, int intWidth)        {            System.Drawing.Bitmap objPic, objNewPic;            try            {                objPic = new System.Drawing.Bitmap(strOldPic);                //參數無效,是因為先除之後轉int型變成0了(可能是0至1之間的值),再乘就沒有意義了,所以改成先乘,後再除                [color=#FF0000]int intHeight = (intWidth * objPic.Height) / objPic.Width;[/color]                objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);                objNewPic.Save(strNewPic);              }            catch (Exception exp)             {                 throw exp;             }            finally            {                objPic = null;                objNewPic = null;            }        } 

热点排行