locked
Image dimensions help please RRS feed

  • Question

  • User364323 posted

    I've been banging my head on the keyboard for long enough so I'm asking for some help. I have a XF app (iOS, android, and UWP) where the user takes some pictures or pick from the gallery and I store the full filepath locally. Then, I generate a word doc with OpenXML and include all the pictures. That part is all working. I used the code from Microsoft on inserting the image here: https://docs.microsoft.com/en-us/office/open-xml/how-to-insert-a-picture-into-a-word-processing-document . But, that sample hard codes the height and width, skewing my images. So I need to get the dimension of the original image and pass them in. This is seemingly way too difficult. I'm using FFImageLoading and Plugin.Media and have seen some others with similar issues, but none of those solutions seem to work for me. Anyone have a more straight forward way to get the height and width of an image starting from the full filepath? I'm still relatively new to XF development so i'm hoping I'm just missing some simple method call or something.

    Thursday, August 15, 2019 9:32 PM

All replies

  • User382871 posted

    Try to use ImageInformation.OriginalHeight and ImageInformation.OriginalWidth to get the original size of the image. private void Image_Success(object sender, CachedImageEvents.SuccessEventArgs e) { var h = e.ImageInformation.OriginalHeight; var w = e.ImageInformation.OriginalWidth; } <ffimageloading:CachedImage x:Name="image" HeightRequest="100" WidthRequest="100" Success="Image_Success" Source="icon"/> Refer to: https://forums.xamarin.com/discussion/comment/230185/#Comment_230185

    Friday, August 16, 2019 2:44 AM
  • User364323 posted

    I had found that thread and tried that solution already. Now I very well may have done something else wrong, but as others in the thread indicated, I think the success event is not getting triggered on android - which is my primary target. Also, I'm doing everything in the code behind, so not using XAML at all for this part. I'll put it back in and post the code. Hopefully someone can spot the mistake.

    Friday, August 16, 2019 4:00 AM
  • User382871 posted

    I think the success event is not getting triggered on android - which is my primary target.

    I've tested the code on android, it works. And you can add breakpoints to vertify it.

    Friday, August 16, 2019 7:08 AM
  • User364323 posted

    Hopefully I can explain what is going on with this. I'm thoroughly confused. Probably just my inexperience. In the code below I try two options - one as suggested, creating the CachedImage and one with a TaskParameter, something I found in another thread somewhere. I put a break point in each Success event. the strange thing to me is the call to ImageInformation.OriginalHeight on the image never happens at all. the call in the Taskparameter does get called, but not until AFTER the main method has finished, so my calculation using h is still 0. Then if I trigger the word doc generation immediately again, the TaskParameter Success event is triggered properly so h and w are set as expected. I still have some aspect ratio issues, probably from the conversion to EMU, but that is a different issue. So why does the CachedImage.Success never trigger, and the TaskParameter.Success triggers late on the first try, but fine on the second?

        CachedImage img = new CachedImage();
        img.Success += (sender, e) =>
            {
                var himg = e.ImageInformation.OriginalHeight;
                var wimg = e.ImageInformation.OriginalWidth;
            };
    
            img.Source = imageFilePath;
    
            ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
            using (FileStream stream = new FileStream(imageFilePath, FileMode.Open))
            {
                imagePart.FeedData(stream);
            }
            cell.RemoveAllChildren();
    
        string relationshipId = mainPart.GetIdOfPart(imagePart);
    
            var h = 0;
            var w = 0;
            TaskParameter imageTask;
            imageTask = ImageService.Instance.LoadFile(imageFilePath);
            imageTask.Success((ImageInformation info, LoadingResult result) =>
            {
                h = info.OriginalHeight;
                w = info.OriginalWidth;
    
            });
            imageTask.Preload();
    
            //convert pixels to EMU
            long LCX = (long)Math.Round((decimal)h * 9525);
            long LCY = (long)Math.Round((decimal)w * 9525);
    
    Friday, August 16, 2019 8:40 PM
  • User382871 posted

    Have you loaded the 'img' your created to the page? I loaded the CachedImage in xaml. If you create the CachedImage in code behind, add the image to the page's layout. ``` CachedImage img = new CachedImage(); img.Success += (sender, e) => { var himg = e.ImageInformation.OriginalHeight; var wimg = e.ImageInformation.OriginalWidth; };

    img.Source = imageFilePath; ...

    StackLayout layout = new StackLayout(); layout.Children.Add(img); Content = layout; ```

    Monday, August 19, 2019 7:49 AM
  • User364323 posted

    Hmm - perhaps that is part of the issue, the img not being loaded, but since I'm not displaying anything at this point, I don't really have anywhere to load it. There is no page for this - it is all in code behind. The user process is they are looking at a page with some summary information. at the bottom is a button that says "Submit Report." This button then pulls a bunch of data, including image paths, generates the word doc, and submits it. The user just gets an alert saying "Report Submitted" - assuming it was successful.

    Monday, August 19, 2019 8:42 PM