將圖像加載到面板 (Load image to panel)


問題描述

將圖像加載到面板 (Load image to panel)

I created a folder "images" inside my project. When the function SetImage() is called, i want to take the image from /images/image.jpg and place it on my Panel. The panel declaration on xaml looks like this:  

xmlns:my="clr‑namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf="clr‑namespace:System.Windows.Forms;assembly=System.Windows.Forms"

            <my:WindowsFormsHost Name="windowsFormsHost1">
                <wf:Panel x:Name="panelWinform"  Dock="Fill" />
            </my:WindowsFormsHost>

Example that works ti display a control in the panel:  

panelWinform.Controls.Clear();
panelWinform.Controls.Add(controlObject);
controlObject.Dock = System.Windows.Forms.DockStyle.Fill;

How can place the picture instead of the controlObject? And what does the .clear() does? Do I need to call it again?

‑‑‑‑‑

參考解法

方法 1:

It would be very easy without  WinForms. You might have a Grid or some other Panel:

<Grid x:Name="panel">
</Grid>

and simply add an Image control to the Panel as shown below.

public void SetImage()
{
    var uri = new Uri("pack://application:,,,/images/image.png");
    var bitmap = new BitmapImage(uri);
    var image = new Image { Source = bitmap };

    panel.Children.Add(image);
}

The image URI looks like that because I assume that the image file is a resource in your Visual Studio project. See Pack URIs for some details. You may as well use an absolute or relative file path for the URI. 

(by ilanschClemens)

參考文件

  1. Load image to panel (CC BY‑SA 3.0/4.0)

#controls #wpf #C#






相關問題

如何在不阻塞的情況下用大量信息填充列表視圖? (How to populate listview with a lot of information without blocking?)

查看項目是否在列錶框控件中的最有效方法 (Most efficient way to see if an item is or is not in a listbox control)

tcng - 匹配“if”語句中的端口範圍 (tcng - match port range in "if" statement)

將圖像加載到面板 (Load image to panel)

Cycle2 Plugin Ẩn / Hiện các nút jQuery (Cycle2 Plugin Hide/Show buttons jQuery)

如何使用 Windows 資源管理器顯示項目和詳細信息展開/折疊 (How to Display Items and Details with Windows Explorer Expand/Collapse)

ASP.NET 2.0 JQuery AJAX 登錄 (ASP.NET 2.0 JQuery AJAX Login)

單步執行控件看不到第二個下拉菜單 (Step through controls fail to see second dropdown)

用戶控件、服務器控件和自定義控件之間有什麼區別? (What are the differences between User Controls, Server Controls & Custom Controls?)

GroupBox 控件中的圓角 (Rounded corners in GroupBox control)

在 .NET 中編寫大型多步驟表單的最有效方法是什麼? (Most efficient way to code a large multi-step form in .NET?)

如何從 ASP .NET MVC 中的動態生成控件中獲取數據? (How can I get data from dynamic generated controls in ASP .NET MVC?)







留言討論