我正在尝试选择一个图像并将其拆分为9个并随机放置在画布中。我正处于选择图像的位置,但我不确定如何分割图像。 我看过类似的问题,但没有运气。我不是在寻找其他人来做这项工作,而是建议如何做到最好。
private void uploadImage_Click(object sender, RoutedEventArgs e)
{
//creates (but not yet displays) a OpenFile dialog box
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
//specifies the look of the open file dialog box
dlg.DefaultExt = ".jpg";
dlg.Filter = "Image Files|*.jpg;";
dlg.InitialDirectory = Environment.CurrentDirectory;
//shows the open file dialog box to user and awaits OK or Cancel
bool? result = dlg.ShowDialog();
//result is true if user selects an image file
if (result == true)
{
filename = dlg.FileName;
var uri = new Uri(filename);
var bitmap = new BitmapImage(uri);
Image bg = new Image();
bg.Source = new BitmapImage(uri);
}
else
return;
}
答案 0 :(得分:1)
我已经回答了类似的问题。要么使用我的解决方案,要么使用CroppedBitmap。 How to crop image and save into ImageSource in WPF?