2012-03-23 85 views
0

我目前正在創建一個程序,每次用戶按下按鈕時都會動態地將圖像添加到dockPanel。我想知道如何獲得它,以便每次用戶將其他圖像添加到dockPanel時,圖像縮小,以便它們都是相同的大小和適合dockPanel。動態更改畫布中的圖像大小

這裏是我當前的代碼:

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

我試圖做myImage2.Height=80myImage2.Width=40(以爲這樣就可以通過改變比例大小,但它似乎沒有這樣的工作方式

任何想法?

感謝

+0

你** **需要使用帆布或你可以使用不同的佈局容器? – CodingGorilla 2012-03-23 19:28:45

+0

@編碼大猩猩對不起,我改變它爲dockPanel。將編輯 – user1219627 2012-03-23 19:29:38

+0

@編碼大猩猩我打開它來改變任何事情。唯一關於uniformgrid的是它不在我的工具箱中 – user1219627 2012-03-23 19:33:52

回答

2

我想你會用不同的容器中得到更好的服務,特別是TH e UniformGrid容器將完全按照您的要求進行。

這裏有一個很好的教程:http://www.wpftutorials.com/2011/03/wpf-uniformgrid.html

UPDATE

UniformGrid可能無法在工具箱中,但它是一個內置的原始,所以你有它。

+0

非常棒,這正是我需要的。偉大的參考感謝。任何想法爲什麼我的形象不是完全出現?它只顯示它的頂角 – user1219627 2012-03-23 19:41:24

+0

在UniformGrid? – CodingGorilla 2012-03-23 19:41:59

+0

是啊,它只顯示部分圖像 – user1219627 2012-03-23 19:48:04

0

如何UniformGrid添加到您的工具箱

- 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")