Byte[] zu Image

Na dann. Gegeben ist folgende XAML-Datei:
Code:
<Window x:Class="WpfImageFromByteArrayDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Image x:Name="ImageViewer"/>
    </Grid>
</Window>

Die Codebehind-Datei sieht folgendermaßen aus:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WpfImageFromByteArrayDemo
{
    /// <summary>
    /// Interaktionslogik für Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private const String IMAGEPATH = @"C:\Users\Public\Pictures\Sample Pictures\Garden.jpg";

        public Window1()
        {
            InitializeComponent();

            HandleImage(IMAGEPATH, this.ImageViewer);
        }

        private void HandleImage(String imagePath, Image image)
        {
            byte[] imageByteArray = GetImage(imagePath);

            MemoryStream ms = new MemoryStream(imageByteArray);
            BitmapDecoder bDecoder = BitmapDecoder.Create(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
            
            if (bDecoder != null && bDecoder.Frames.Count > 0)
                image.Source = bDecoder.Frames[0];
        }

        private byte[] GetImage(String imagePath)
        {
            return File.ReadAllBytes(imagePath);
        }
    }
}
Damit wird das angegebene Image eingelesen (in meinem Fall von einem File, du liest das ja aus dem Web ein und hast dann ebenfalls wie ich, ein Byte-Array zur Verfügung).

Anschließend wird über einen BitmapDecoder das Image geladen.
 
Noch ein Hinweis: Dem BitmapDecoder.Create kannst du als ersten Parameter ein Uri-Objekt übergeben. Gib da mal deine URL an ;-) Damit reduziert sich der Code etwas:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WpfImageFromByteArrayDemo
{
    /// <summary>
    /// Interaktionslogik für Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        

        public Window1()
        {
            InitializeComponent();

            HandleImage(this.ImageViewer);
        }

        private void HandleImage(Image image)
        {
            BitmapDecoder bDecoder = BitmapDecoder.Create(new Uri("http://dotnet-gui.com/themes/default/images/common/logo_dotnet_gui.gif"), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);

            if (bDecoder != null && bDecoder.Frames.Count > 0)
                image.Source = bDecoder.Frames[0];
        }
    }
}
 
spitze, denke so sollte es funktionieren.

vielen dank fuer deine hilfe, jetzt hab ich es verstanden !


EDIT:

Problem dank dir gelöst, big thx!
 
Zuletzt bearbeitet:
es hat sich ein neues problem aufgetan, hab es nun so geloest:

Code:
void treeview_item_MouseDown(object sender, MouseButtonEventArgs e)
        {
            bDecoder = BitmapDecoder.Create(new Uri(((TreeViewItem)sender).Tag.ToString()), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
            bDecoder.DownloadCompleted += new EventHandler(bDecoder_DownloadCompleted);
            bDecoder.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(bDecoder_DownloadProgress);
        }


geht auch, allerdings wird das downloaded event nie zweimal fuer das selbe bild geworfen.
also wenn ich zweimal das selbe bild downloaden will geht er nur einmal in den downloaded bzw progress eventhandler.

gibts dafuer ne loesung?




[EDIT]:

problem umgangen, loesung nicht mehr notwendig!
 
Zuletzt bearbeitet:

Neue Beiträge

Zurück