这是我将图像分成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]);
这将裁剪图像并插入位图的矩阵数组中。 问题是用户在纵向模式下拍摄图像,当应用程序将图像以横向形式分割而不是相同的部分。 任何帮助将不胜感激。
答案 0 :(得分:0)
问题是用户以纵向模式拍摄图像,当应用程序将图像以横向形式分割而不是相同的部分时。
当用户以纵向模式拍照时,您需要给出指示(让我们说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]);
}