将图像切成9块C# [英] Cut an Image into 9 pieces C#

查看:132
本文介绍了将图像切成9块C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

图片分成9件

虽然我用Google搜索但是遗憾的是未能找到帮助。此 Code Project Tutorial 也是没能为我提供我真正需要的东西。

Though I googled enough but unfortunately failed to find a help. This Code Project Tutorial also failed to serve me what I actually need.

我在WinForm中有一个Image和9个PictureBox。

I have an Image and 9 PictureBox(s) in a WinForm.

Image img = Image.FromFile("media\\a.png"); // a.png has 312X312 width and height
//          some code help, to get
//          img1, img2, img3, img4, img5, img6, img7, img8, img9
//          having equal width and height
//          then...
pictureBox1.Image = img1;
pictureBox2.Image = img2;
pictureBox3.Image = img3;
pictureBox4.Image = img4;
pictureBox5.Image = img5;
pictureBox6.Image = img6;
pictureBox7.Image = img7;
pictureBox8.Image = img8;
pictureBox9.Image = img9;

以下是一个示例图片:

这是一个我的Picture Puzzle类项目的一部分。我已经完成了photoshop图片,现在想要动态剪切。

This is a part of my Picture Puzzle class project. I have done with photoshop images, now want to dynamically cut.

提前致谢。

推荐答案

首先,不使用img1,img2 ...使用大小为9的数组。然后使用这样的几个循环更容易做到这一点:

First off, instead of using img1, img2... use an array with a size of 9. Then it's much easier to do this using a couple of loops like this:

var imgarray = new Image[9];
var img = Image.FromFile("media\\a.png");
for( int i = 0; i < 3; i++){
  for( int j = 0; j < 3; j++){
    var index = i*3+j;
    imgarray[index] = new Bitmap(104,104);
    var graphics = Graphics.FromImage(imgarray[index]);
    graphics.DrawImage( img, new Rectangle(0,0,104,104), new Rectangle(i*104, j*104,104,104), GraphicsUnit.Pixel);
    graphics.Dispose();
  }
}

然后你可以像这样填写你的方框:

Then you can fill your boxes like this:

pictureBox1.Image = imgarray[0];
pictureBox2.Image = imgarray[1];
...

这篇关于将图像切成9块C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆