C# 切图,部分代码

        public void GetImg() 
        {
            var imgPath = @"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg";
            System.Drawing.Image image = new System.Drawing.Bitmap(imgPath);
            //   int iStep = 100;
            int iStep = 256;
            int iLeft = 0, iTop = 0, iWidth = iStep, iHeight = iStep;

            for (int i = 0; i < image.Height / iStep; i++)

            {
                iTop = i * iStep;
                for (int j = 0; j < image.Width / iStep; j++)
                {
                    iLeft = j * iStep;

                    // 目标区域
                    Rectangle destRect = new Rectangle(0, 0, iWidth, iHeight);
                    // 源图区域
                    Rectangle srcRect = new Rectangle(iLeft, iTop, iWidth, iHeight);

                    // 新建Graphics对象
                    Bitmap newImage = new Bitmap(iWidth, iHeight);
                    Graphics g = Graphics.FromImage(newImage);

                    // 绘图平滑程序
                    g.SmoothingMode = SmoothingMode.HighQuality;

                    // 图片输出质量
                    g.CompositingQuality = CompositingQuality.HighQuality;

                    // 输出到newImage对象
                    g.DrawImage(image, destRect, srcRect, GraphicsUnit.Pixel);

                    // 释放绘图对象
                    g.Dispose();


                    string strDestFile = string.Format(
                     "{0}//x{1}y{2}.jpg",
                     imgPath, iLeft, iTop, iStep
                     );
                    newImage.Save(strDestFile);

                }
            }

        }

 

posted on 2013-04-26 12:48  无觉-李敏  阅读(442)  评论(0编辑  收藏  举报