这里说的缩放图片的确就是缩放一个图片的大小,比如放大5倍。
但我的需求是像Photoshop,fireworks这样的图像编辑软件一样,放大后不自动插值,放大5倍后1*1的点显示为5*5的点。试了picturebox的StretchImage(SizeMode),Bitmap的改变图片大小构造方法和Graphics.DrawImage方法都未果,或者可能哪个参数不知道,请教绘图方面的大虾了。

解决方案 »

  1.   


    //************************************************************//
    //下面给出三个简单的方法,后面两个方法是扩展,估计有时用得着
    //************************************************************//
      /// <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;
       }
      }
      

  2.   

    楼上的也是搜索来的代码,也还是用的bitmap的构造函数,放大后自动插值了。不是我要的那种效果。您也应该用过ps,fireworks之类的软件,他们的图片编辑工作区,有放大,缩小图片的功能,放大5倍后1*1的点是显示为5*5的一个点。不是插值补全显示的25个点谢谢您的回复,您提供的方法无效。
      

  3.   

    实现很简单,就是得花时间敲代码,以下代码经过测试可行(自己写的)        /// <summary>
            /// 不插补放大
             /// </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;
            }
      

  4.   

    这也是我刚准备直接用循环一个一个地setpixel了。您直接给我贴出来了。感谢@