Search code examples
c#imageuwpdrag-and-drop

UWP Image is displayed after drag and drop but is NULL when later retrieved


Scenario: I get multiple images via a drag and drop into my application, then after a user interaction these Images are (with other data) taken and put into an object from which i want to upload them eventually.

Here is my Code(I'll always remove unneccessary clutter like: GalerieImage.Name = xy):

Receiving from the drag and drop:

if (e.DataView.Contains(StandardDataFormats.StorageItems))
            {
                var Items = await e.DataView.GetStorageItemsAsync();

                foreach (StorageFile storageItem in Items)
                {
                    var DroppedImages = new BitmapImage();
                    await DroppedImages.SetSourceAsync(await storageItem.OpenReadAsync());
                    //DroppedImages.UriSource = new Uri(storageItem.Path);

                    Image GalerieImage = new Image();
                    GalerieImage.Source = DroppedImages;

                    ((RelativePanel)sender).Children.Add(GalerieImage);
                }
            }

After this is executed the images get rendered in the application. If i look into each of the Images that come out of this they (DroppedImages(BitmapImage)) have the UriSource set to NULL even though the storage Item has its source set to the source of the pictures. (this is easily fixed by the commented line in the code. They have to correct with and height and all and they get correctly rendered.

If i now execute this code:

foreach (UIElement image in this.PanelBildgalerie.Children)
            {
                try
                {
                    if(((Image)image).Name != "no")
                    {
                        dataObject.GalerieBilder.Add((Image)image);
                    }
                }
                catch
                {

                }
            }

It throws a System.NullReferenceException at dataObject.GalerieBilder.Add((Image)image);and has no data besides the name i gave them and the height and with i set.

My Problem: I need to get the image data from these images that are dropped and i don't know why it is giving me a null Reference since they are getting rendered without a problem and the names of the Images that cause the exception match up with the ones that are getting rendered.

Thank you in advance for your help.


Solution

  • It throws a System.NullReferenceException at dataObject.GalerieBilder.Add((Image)image);

    During the test, we could get image with foreach PanelBildgalerie's children, and their source contains value but not null, please make sure you have initialize GalerieBilder collection class before add the image.

    My Problem: I need to get the image data from these images that are dropped

    If you want to save dropped image, you could insert them into BitmapImage collection within Drop event, and you have no need to fetch from PanelBildgalerie.Children.

    private List<BitmapImage> _images = new List<BitmapImage>();
    private async void Image_Drop(object sender, DragEventArgs e)
    {
        if (e.DataView.Contains(StandardDataFormats.StorageItems))
        {
            var Items = await e.DataView.GetStorageItemsAsync();
    
            foreach (StorageFile storageItem in Items)
            {
                var DroppedImages = new BitmapImage();
                await DroppedImages.SetSourceAsync(await storageItem.OpenReadAsync());
                //DroppedImages.UriSource = new Uri(storageItem.Path);
    
                Image GalerieImage = new Image();
                GalerieImage.Source = DroppedImages;
    
                ((RelativePanel)sender).Children.Add(GalerieImage);
                _images.Add(DroppedImages);
            }
        }
    }
    

    If you do want to save Image, please make sure dataObject.GalerieBilder is not null.

    private List<Image> _dropImage = new List<Image>();
    private void Button_Click(object sender, RoutedEventArgs e)
    {
       
        foreach (UIElement image in PanelBildgalerie.Children)
        {
            try
            {
                if (((Image)image).Name != "no")
                {
                     _dropImage.Add(image as Image);
                }
            }
            catch
            {
    
            }
        }
    }