这里说的缩放图片的确就是缩放一个图片的大小,比如放大5倍。
但我的需求是像Photoshop,fireworks这样的图像编辑软件一样,放大后不自动插值,放大5倍后1*1的点显示为5*5的点。试了picturebox的StretchImage(SizeMode),Bitmap的改变图片大小构造方法和Graphics.DrawImage方法都未果,或者可能哪个参数不知道,请教绘图方面的大虾了。
但我的需求是像Photoshop,fireworks这样的图像编辑软件一样,放大后不自动插值,放大5倍后1*1的点显示为5*5的点。试了picturebox的StretchImage(SizeMode),Bitmap的改变图片大小构造方法和Graphics.DrawImage方法都未果,或者可能哪个参数不知道,请教绘图方面的大虾了。
//************************************************************//
//下面给出三个简单的方法,后面两个方法是扩展,估计有时用得着
//************************************************************//
/// <summary>
/// 缩小图片
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intWidth">缩小至宽度</param>
/// <param name="intHeight">缩小至高度</param>
public void SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight)
{ System.Drawing.Bitmap objPic,objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight);
objNewPic.Save(strNewPic); }
catch(Exception exp){throw exp;}
finally
{
objPic=null;
objNewPic=null;
}
} /// <summary>
/// 按比例缩小图片,自动计算高度
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intWidth">缩小至宽度</param>
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); }
catch(Exception exp){throw exp;}
finally
{
objPic=null;
objNewPic=null;
}
}
/// <summary>
/// 按比例缩小图片,自动计算宽度
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intHeight">缩小至高度</param>
public void SmallPic(string strOldPic, string strNewPic, int intHeight)
{ System.Drawing.Bitmap objPic,objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
int intWidth=(intHeight / objPic.Height) * objPic.Width;
objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight);
objNewPic.Save(strNewPic); }
catch(Exception exp){throw exp;}
finally
{
objPic=null;
objNewPic=null;
}
}
/// 不插补放大
/// </summary>
/// <param name="orgimg">原图</param>
/// <param name="times">放大倍率</param>
/// <returns></returns>
public Bitmap Zoom(Bitmap orgimg, int times)
{
Bitmap newimg = new Bitmap(orgimg.Width * times, orgimg.Height * times);
for (int i = 0; i < orgimg.Width; i++)
{
for (int j = 0; j < orgimg.Height; j++)
{
for (int x = i * times; x < (i + 1) * times; x++)
{
for (int y = j * times; y < (j + 1) * times; y++)
{
newimg.SetPixel(x, y, orgimg.GetPixel(i, j));
}
}
}
}
return newimg;
}