user1219627
user1219627

Reputation: 787

dynamically changing image size in Canvas

I am currently creating a program that dynamically adds Image to a dockPanel every time a user presses a button. I was wondering how I could get it so that every time the user adds another image to the dockPanel the images shrink so that they are both the same size and fit inside the dockPanel.

Here is my current code:

 Uri myUri = new Uri(@"C:\Users\Jim\Desktop\Project\bin\Debug\pic.bmp", UriKind.RelativeOrAbsolute);
            BmpBitmapDecoder decoder2 = new BmpBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource bitmapSource2 = decoder2.Frames[0];
        // Draw the Image
        Image myImage2 = new Image();
        myImage2.Source = bitmapSource2;
        myImage2.Stretch = Stretch.None;
        myImage2.Margin = new Thickness(20);
        dockPanel1.Children.Add(myImage2);

I tried doing myImage2.Height=80 and myImage2.Width=40 (thinking that it would change the size through percentage but it didn't seem to work this way.

Any Ideas?

Thanks

Upvotes: 0

Views: 1235

Answers (2)

Roy
Roy

Reputation: 984

How to add UniformGrid to your toolbox

- In your toolbox, right-click the header "All WPF Controls"

- Click "Choose Items..."

- Select the "WPF Components" tab

- Scroll down to "UniformGrid"

- Click the checkbox so it's checked

- Click "Ok"

- In your toolbox, drag and drop the new UniformGrid control so it's placed in the correct position alphabetically (on mine, it's between "Treeview" and "Viewbox")

Upvotes: 0

CodingGorilla
CodingGorilla

Reputation: 19872

I think you'd be better served by using a different container, specifically the UniformGrid container will do exactly what you want.

Here's a good tutorial: http://www.wpftutorials.com/2011/03/wpf-uniformgrid.html

UPDATE

UniformGrid may not be in the toolbox, but it is a built-in primitive so you do have it.

Upvotes: 2

Related Questions