日期:2014-05-17  浏览次数:20814 次

请帮忙给我看下图片处理函数为什么只能处理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;
            }
        }