这是我把图像分割成9块的代码
imgs = new Bitmap[3, 3];
int width, height;
width = _thePicture.Width / 3;
height = _thePicture.Height / 3;
for (int x = 0; x < 3; ++x)
{
for (int y = 0; y < 3; ++y)
{
// Create the sliced bitmap
imgs[x,y] = Bitmap.CreateBitmap(_thePicture, x * width, y * height, width, height);
}
}
_img1.SetImageBitmap(imgs[0, 0]);
_img2.SetImageBitmap(imgs[1, 0]);
_img3.SetImageBitmap(imgs[2, 0]);
_img4.SetImageBitmap(imgs[0, 1]);
_img5.SetImageBitmap(imgs[1, 1]);
_img6.SetImageBitmap(imgs[2, 1]);
_img7.SetImageBitmap(imgs[0, 2]);
_img8.SetImageBitmap(imgs[1, 2]);
_img9.SetImageBitmap(imgs[2, 2]);
这将裁剪图像并插入位图的矩阵数组中。问题是用户在纵向模式下获取图像,当应用程序分割图像时,它是以景观形式出现的,而不是以相等的形式出现的。任何帮助都将不胜感激。
发布于 2017-01-11 02:32:29
问题是用户在纵向模式下获取图像,当应用程序分割图像时,它是以景观形式出现的,而不是以相等的形式出现的。
当用户在纵向模式下拍照时,您需要给出一个指示符(比如isPortrait
)。在纵向模式下,您可以在分割图像之前旋转位图:
//set isPortrait=true if user take picture in portrait mode
private async void BtnClick_Click(object sender, System.EventArgs e)
{
...
//rotate the image if user take the picture in portrait mode
if (isPortrait)
{
thisPicture = RotateBitmap(_thisPicture, 90);
}
SplitImage(_thisPicture);
}
public Bitmap RotateBitmap(Bitmap bitmap, int degree)
{
int w = bitmap.Width;
int h = bitmap.Height;
Matrix mtx = new Matrix();
mtx.SetRotate(degree);
return Bitmap.CreateBitmap(bitmap, 0, 0, w, h, mtx, true);
}
private void SplitImage(Bitmap _thePicture)
{
Bitmap[,] imgs;
imgs = new Bitmap[3, 3];
int width, height;
width = _thePicture.Width / 3;
height = _thePicture.Height / 3;
for (int x = 0; x < 3; ++x)
{
for (int y = 0; y < 3; ++y)
{
// Create the sliced bitmap
imgs[x, y] = Bitmap.CreateBitmap(_thePicture, x * width, y * height, width, height);
}
}
_img1.SetImageBitmap(imgs[0, 0]);
_img2.SetImageBitmap(imgs[1, 0]);
_img3.SetImageBitmap(imgs[2, 0]);
_img4.SetImageBitmap(imgs[0, 1]);
_img5.SetImageBitmap(imgs[1, 1]);
_img6.SetImageBitmap(imgs[2, 1]);
_img7.SetImageBitmap(imgs[0, 2]);
_img8.SetImageBitmap(imgs[1, 2]);
_img9.SetImageBitmap(imgs[2, 2]);
}
https://stackoverflow.com/questions/41550292
复制相似问题