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

PictureBox[] picboxAry;

private voidForm1_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 voidsplitImage(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"));

        }

    }

}


这个要根据自己实际的情况修改,我已经用上了,不错。

你可能感兴趣的:(String,image,C#,File,Path,WinForm)