[C#][WinForm]如何切割圖片

  • 17191
  • 0
  • C#
  • 2009-12-07

[C#][WinForm]如何切割圖片

小鋪網友問題,自己實作記錄。

image


PictureBox[] picboxAry;
private void Form1_Load(object sender, EventArgs e)
{
    pictureBox1.Image = Image.FromFile(@"G:\Blue hills.jpg");//先在form1中拉入pictureBox1並載入圖檔
    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
    picboxAry = new PictureBox[3];//產生PictureBox陣列
    splitImage(@"G:\", @"G:\",Image.FromFile(@"G:\Blue hills.jpg"), 200, 800);//切割
    for (Int32 i = 0; i < picboxAry.Length; i++)
    {
        picboxAry[i] = new PictureBox();
        picboxAry[i].Size = new Size(160, 100);//設定大小
        picboxAry[i].Location = new Point(pictureBox1.Width+10, 105 * i);//設定座標
        picboxAry[i].SizeMode = PictureBoxSizeMode.StretchImage;
        picboxAry[i].Image = Image.FromFile(@"G:\" + 200 * i + ".jpg");
        this.Controls.Add(picboxAry[i]);
    }
 }

private void splitImage(String path,String file,Image img, Int32 sHeight, Int32 sWidth)
{
    Bitmap Mybmp = new Bitmap(sWidth, sHeight);
    Graphics gr = Graphics.FromImage(Mybmp);
    //重新繪製圖像並存檔
    for (Int32 y = 0; y < img.Height; y += sHeight)
    {
        for (Int32 x = 0; x < img.Width; x += sWidth)
        {
            gr.Clear(Color.Black);
            gr.DrawImage(img, new Rectangle(0, 0, Mybmp.Width, Mybmp.Height), x, y, sWidth, sHeight, GraphicsUnit.Pixel);
            gr.Save();
            Mybmp.Save(Path.Combine(path, file + y.ToString() + ".jpg"));
        }
    }
}