locked
Drag & Drop RRS feed

  • Frage

  • Hallo,

    hab die neue Möglichekit von Drag & Drop in SL4 ausprobiert. Unter Windows Server 2008 SP2 funktioniert das Beispiel unter Win7 kommt es zur Meldung "Schwerwiegender Fehler (Ausnahme von HRESULT: 0x8000FFFF (E_UNEXPECTED))" in Zeile source.SetSource(fs); - egal aus welchem Verzeichnis die Datei stammt.

    Thx

        private void TextArea_Drop(object sender, DragEventArgs e)
        {
                if (e.Data == null) return;
                Paragraph para;
                InlineUIContainer container;
                Image img;
                BitmapImage source;
          
          IDataObject obj = e.Data as IDataObject;
          
          if (obj != null)
          {
    
            FileInfo[] files = obj.GetData(System.Windows.DataFormats.FileDrop) as FileInfo[];
    
            foreach (FileInfo file in files)
            {
                        
              if (IsImage(file))
              {
                using (FileStream fs = file.OpenRead())
                {
                  source = new BitmapImage();
                  source.SetSource(fs);
                  para = new Paragraph();
    
                  container = new InlineUIContainer();
                  
                  img = new Image();
                  img.Source = source;
                  
                  para.Inlines.Add(container);
                  TextArea.Blocks.Add(para);
    
                  //beta bug!!!
    
                  container.Child = img;
                }
              }
              
            }
            
          }
                
        }
    Mittwoch, 12. Mai 2010 06:50

Antworten

  • Ich habe das mal nachgestellt und kann bei mir unter Windows 7 64 Bit kein Problem erkennen, solange die mit Drag and Drop gezogene Datei auch wirklich ein Bild ist. Wenn die Datei kein in eine BitmaImage ladbares Bild beinhaltet, dann kommt der von dir genannte Fehler bei der Konvertierung des Dateiinhaltes in ein BitmapImage-Objekt. Du solltest also zuerst deine Methode IsImage überprüfen, damit sie auch wirklich passend arbeitet.

    Hier mein XAML zur Demo:

    <navigation:Page x:Class="SilverlightApplication1.Page8" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
      d:DesignWidth="640" d:DesignHeight="480"
      Title="Page8 Drag and Drop">
     <Grid x:Name="LayoutRoot">
     <RichTextBox Name="TextArea" Drop="TextArea_Drop" AllowDrop="True" MinHeight="300"/>
     </Grid>
    </navigation:Page>

    Und dazu der CodeBehind in VB10:

    Imports System.IO
    Imports System.Windows.Media.Imaging
    
    Partial Public Class Page8
     Inherits Page
    
     Public Sub New()
     InitializeComponent()
     End Sub
    
     Private Sub TextArea_Drop(ByVal sender As System.Object, ByVal e As System.Windows.DragEventArgs)
     If e.Data Is Nothing Then Exit Sub
     If e.Data.GetType Is GetType(DataObject) Then
     Dim obj As IDataObject = CType(e.Data, IDataObject)
     Dim files() As FileInfo = CType(obj.GetData(System.Windows.DataFormats.FileDrop), FileInfo())
     For Each file In files
     If file.Extension = ".jpg" Then
      Using fs As FileStream = file.OpenRead()
      Dim source As New BitmapImage()
      source.SetSource(fs)
      Dim img As New Image()
      img.Source = source
      Dim container As New InlineUIContainer()
      container.Child = img
      Dim para As New Paragraph()
      para.Inlines.Add(container)
      TextArea.Blocks.Add(para)
      End Using
     End If
     Next
     End If
     End Sub
    
    End Class
    --
    Peter
    Donnerstag, 13. Mai 2010 07:44

Alle Antworten

  • Ich habe das mal nachgestellt und kann bei mir unter Windows 7 64 Bit kein Problem erkennen, solange die mit Drag and Drop gezogene Datei auch wirklich ein Bild ist. Wenn die Datei kein in eine BitmaImage ladbares Bild beinhaltet, dann kommt der von dir genannte Fehler bei der Konvertierung des Dateiinhaltes in ein BitmapImage-Objekt. Du solltest also zuerst deine Methode IsImage überprüfen, damit sie auch wirklich passend arbeitet.

    Hier mein XAML zur Demo:

    <navigation:Page x:Class="SilverlightApplication1.Page8" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
      d:DesignWidth="640" d:DesignHeight="480"
      Title="Page8 Drag and Drop">
     <Grid x:Name="LayoutRoot">
     <RichTextBox Name="TextArea" Drop="TextArea_Drop" AllowDrop="True" MinHeight="300"/>
     </Grid>
    </navigation:Page>

    Und dazu der CodeBehind in VB10:

    Imports System.IO
    Imports System.Windows.Media.Imaging
    
    Partial Public Class Page8
     Inherits Page
    
     Public Sub New()
     InitializeComponent()
     End Sub
    
     Private Sub TextArea_Drop(ByVal sender As System.Object, ByVal e As System.Windows.DragEventArgs)
     If e.Data Is Nothing Then Exit Sub
     If e.Data.GetType Is GetType(DataObject) Then
     Dim obj As IDataObject = CType(e.Data, IDataObject)
     Dim files() As FileInfo = CType(obj.GetData(System.Windows.DataFormats.FileDrop), FileInfo())
     For Each file In files
     If file.Extension = ".jpg" Then
      Using fs As FileStream = file.OpenRead()
      Dim source As New BitmapImage()
      source.SetSource(fs)
      Dim img As New Image()
      img.Source = source
      Dim container As New InlineUIContainer()
      container.Child = img
      Dim para As New Paragraph()
      para.Inlines.Add(container)
      TextArea.Blocks.Add(para)
      End Using
     End If
     Next
     End If
     End Sub
    
    End Class
    --
    Peter
    Donnerstag, 13. Mai 2010 07:44
  • Hallo M i c h i,

    Ich konnte es auch erfolgreich testen unter Windows 7 32-bit mit Visual Studio 2010 RTMRel. Es kommt keine Fehlermeldung.

    [XAML]

    <UserControl x:Class="SilverlightApplication1.MainPage"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     mc:Ignorable="d"
     d:DesignHeight="300" d:DesignWidth="400">
    
    <Grid x:Name="LayoutRoot" Background="White">
    
      <Grid.RowDefinitions>
    
       <RowDefinition Height="50"></RowDefinition>
    
       <RowDefinition></RowDefinition>
    
      </Grid.RowDefinitions>
    
      <Border Grid.Row="1" x:Name="photoBorder" 
    
       BorderBrush="Blue" BorderThickness="4">
    
       <Canvas x:Name="photoPanel" Grid.Row="1" 
    
         Background="Gray" AllowDrop="True"
    
         Drop="photoPanel_Drop"
    
         DragEnter="photoPanel_DragEnter"
    
         DragLeave="photoPanel_DragLeave">
    
       </Canvas>
    
      </Border>
    
     </Grid>
    </UserControl>

     

    [C# Code Behind]

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.IO;
    using System.Windows.Media.Imaging;
    
    namespace SilverlightApplication1
    {
     public partial class MainPage : UserControl
     {
      public MainPage()
      {
       InitializeComponent();
      }
    
      private void photoPanel_Drop(object sender, DragEventArgs e)
      {
    
       photoBorder.BorderBrush = new SolidColorBrush(Colors.Blue);
    
       IDataObject data = e.Data;
    
       FileInfo[] files = (FileInfo[])data.GetData
    
        (DataFormats.FileDrop);
    
       foreach (FileInfo f in files)
       {
    
        if (IsImage(f))
        {
    
         Stream s = f.OpenRead();
    
         BitmapImage bitmap = new BitmapImage();
    
         bitmap.SetSource(s);
    
         Image img = new Image();
    
         img.Source = bitmap;
    
         img.Width = 100;
    
         img.Height = 80;
    
         Point p = e.GetPosition(photoPanel);
    
         img.Margin = new Thickness(p.X, p.Y, 0, 0);
    
         photoPanel.Children.Add(img);
    
        }
    
       }
    
      }
    
      private void photoPanel_DragEnter(object sender, DragEventArgs e)
      {
    
       photoBorder.BorderBrush = new SolidColorBrush(Colors.Red);
    
      }
    
      private void photoPanel_DragLeave(object sender, DragEventArgs e)
      {
    
       photoBorder.BorderBrush = new SolidColorBrush(Colors.Blue);
    
      }
    
      private bool IsImage(FileInfo file)
      {
       string ext = file.Extension.ToLower();
    
       string[] extensions = 
       {
        ".bmp",
        ".jpg",
        ".jpeg",
        ".gif",
        ".png"
       };
    
       return extensions.Contains(ext);
      }
    
     }
    }

     

    Silverlight 4: DropTarget mit Silverlight: Bilder in die RichTextArea einfügen

    Grüße,

    Robert

    Donnerstag, 13. Mai 2010 10:47
  • mh, geht bedingt jpeg und png funktionieren, bmp und gif leider nicht, das ist auch unabhängig vom OS

    Dienstag, 18. Mai 2010 06:58