Thursday, May 14, 2015

Image Split

public static void ImageSplit(string Filname)
        {
            // string Filname = @"E:\dodge_image\2.tif";
            Image img = Image.FromFile(Filname); // a.png has 312X312 width and height
            //int widthThird = (int)((double)img.Width / 3.0 + 0.5);
            //int heightThird = (int)((double)img.Height / 3.0 + 0.5);

            int widthThird = (int)((double)img.Width / 8.0 + 3.0);
            int heightThird = (int)((double)img.Height / 8.0 + 3.0);

            int k = 0;
            Bitmap[,] bmps = new Bitmap[8, 8];
            for (int i = 0; i < 8; i++)
                for (int j = 0; j < 8; j++)
                {
                    bmps[i, j] = new Bitmap(widthThird, heightThird);
                    Graphics g = Graphics.FromImage(bmps[i, j]);
                    g.DrawImage(img, new Rectangle(0, 0, widthThird, heightThird), new Rectangle(j * widthThird, i * heightThird, widthThird, heightThird), GraphicsUnit.Pixel);
                    g.Dispose();
                    if (!Directory.Exists("E:\\output\\" + Path.GetFileNameWithoutExtension(Filname)))
                        Directory.CreateDirectory("E:\\output\\" + Path.GetFileNameWithoutExtension(Filname));
                    bmps[i, j].Save("E:\\output\\" + Path.GetFileNameWithoutExtension(Filname) + "\\" + Path.GetFileNameWithoutExtension(Filname) + "_" + k + ".bmp", ImageFormat.Bmp);
                    k++;
                }

        }

No comments:

Post a Comment