读书人

请帮忙给小弟我看上图片处理函数为什么

发布时间: 2012-11-01 11:11:31 作者: rapoo

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

读书人网 >C#

热点推荐