Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

Schreib mir einer Methode welche das ausgewählte Bild läd und es anschließend darstellt.…

  1. User

    Schreib mir einer Methode welche das ausgewählte Bild läd und es anschließend darstellt.

    public void UploadImage() { Console.WriteLine("UploadImage method was executed...");

            OpenFileDialog openFileDialog = new OpenFileDialog();
    
            openFileDialog.Filter = "Image files|*.png;*jpg;*jpeg;*jpng";   // User can only select these specific image formats
            openFileDialog.Multiselect = false;                             // User can only select one file at once
    
            if(openFileDialog.ShowDialog() == true)                         
            {
                // Checks if the uploaded file name is emtpy or null (invaild file)
                if(string.IsNullOrEmpty(openFileDialog.FileName))
                {
                    Console.WriteLine("File is invaild, please try again or try to upload a diffrent image!");
                } else
                {
                    string filePath = openFileDialog.FileName;
    
                    try
                    {
                        Image image = new Image();
    
                        // Adds the selected image to the imageList
                        imageList.Add(image);
                        PrintList();
    
                    } catch(Exception ex)
                    {
                        Console.WriteLine("An error occured while saving the file: " + ex.Message);
                    }
                }
    
            } else
            {
                // Occures if the user cancels the folder process
                Console.WriteLine("Process was cancelled by the user");
            }
    
  2. Assistant

    Um die Funktionalität zum Laden und Anzeigen eines Bildes zu implementieren, müssen wir WPF (Windows Presentation Foundation) verwenden, da unser Code eine Konsolenanwendung ist. Konvertieren Sie Ihre Anwendung in eine WPF-Anwendung, um das folgende Beispiel zu verwenden:

    Zuerst müssen Sie die Namespaces hinzufügen:

    using Microsoft.Win32;
    using System.IO;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media.Imaging;
    

    Erstellen Sie in Ihrer MainWindow.xaml.cs eine Methode zum Laden und Anzeigen des ausgewählten Bildes:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        public void UploadImage()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
    
            openFileDialog.Filter = "Image files|*.png;*.jpg;*.jpeg;*.jpng";
            openFileDialog.Multiselect = false;
    
            if (openFileDialog.ShowDialog() == true)
            {
                if (string.IsNullOrEmpty(openFileDialog.FileName))
                {
                    MessageBox.Show("File is invalid, please try again or try to upload a different image!");
                }
                else
                {
                    string filePath = openFileDialog.FileName;
    
                    try
                    {
                        BitmapImage bitmapImage = new BitmapImage();
                        bitmapImage.BeginInit();
                        bitmapImage.UriSource = new Uri(filePath, UriKind.Absolute);
                        bitmapImage.EndInit();
    
                        Image image = new Image();
                        image.Source = bitmapImage;
    
                        imageGrid.Children.Clear(); // Assuming 'imageGrid' is a Grid element in your XAML
                        imageGrid.Children.Add(image);
    
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("An error occurred while saving the file: " + ex.Message);
                    }
                }
            }
            else
            {
                MessageBox.Show("Process was cancelled by the user");
            }
        }
    }
    

    In Ihrer MainWindow.xaml, fügen Sie das Grid-Element hinzu, um das Bild anzuzeigen:

    <Window x:Class="YourNamespace.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
            <Grid x:Name="imageGrid" HorizontalAlignment="Center" VerticalAlignment="Center">
                <!-- Image will be displayed here -->
            </Grid>
        </Grid>
    </Window>
    

    Nun können Sie durch Aufrufen der UploadImage()-Methode ein Bild auswählen, laden und anzeigen. Beachten Sie, dass dieser Code nur in einer WPF-Anwendung funktioniert, nicht in einer Konsolenanwendung.