Colin Roe
Colin Roe

Reputation: 804

How to split an image into 9 pieces and place in a canvas randomly?

I am trying to select an image and split it into 9 pieces and place randomly in a canvas. I am at the point where I have selected an image, but I am unsure of how to split the image. I have looked at similar questions but no luck. I'm not looking for someone else to do the work, but advice on how best to do it.

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;
}

Upvotes: 2

Views: 3110

Answers (1)

Erti-Chris Eelmaa
Erti-Chris Eelmaa

Reputation: 26268

I have already answered similiar question. Either use my solution, or resort to CroppedBitmap. How to crop image and save into ImageSource in WPF?

Upvotes: 1

Related Questions