Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin.Forms.Image not displaying

kenneth.leekenneth.lee USMember ✭✭✭

Hi Everyone,

I recently found a bug within my app. The problem is that the phone it is deployed on has a camera which is able to capture images with pixels more than the limit on OpenGLRenderer. Having said that that means Images from the server coming down to the app that is bigger than the limit will also not show.

Bitmap too large to be uploaded into a texture (5000x5000, max=4096x4096)

My first thought is to scale the image down proportionately so it doesn't get stretched.
Currently the image is being set as a Xamarin.Forms.Image object

Image _image = new Image()
{
    Source = file.Path,
    VerticalOptions = LayoutOptions.CenterAndExpand,
Aspect = Aspect.Fit
};

I assumed that setting the Aspect to Fit would have maybe tried to scale it to fit the screen - it was hopeful.
I then proceeded to try _image.Scale = 0.5 but that didn't do anything. I got the same size in the debug log.

I was wondering if there are an easier way to scale Xamarin.Forms.Image...

Answers

  • PierreParmPierreParm FRMember ✭✭

    I don't have a solution right now but what you call scaling does not reduce the actual size of the data, it only reduces visually the image.

    Maybe trying this : https://stackoverflow.com/questions/25265686/xamarin-forms-resize-camera-picture/25268405#25268405
    I don't recommend Xamarin Labs though but there are ways to do the same with other libraries.

  • anton.emelyancevanton.emelyancev USMember ✭✭

    Try to use FFImageLoading's CachedImage instead of FX Image. It can resize (actualy downsamples) images to specified size and can do alot of other usefull stuff.

  • kenneth.leekenneth.lee USMember ✭✭✭

    @PierreParm Ahh that makes sense. The solution you suggested doesn't exactly downsample a Xamarin.Forms.Image object but that would solve the problem. I haven't yet figured out really how I want to approach this problem of mine. I don't want the users to lose quality with their images but I also don't want to be storing original sized and downsample sized versions of one image.

    @AntonEmelyancev Yeah i saw actually saw FFImageLoading before posting but i saw on the readme that the build was failing so i was a bit put off trying but maybe if i don't end up finding a solution i will have to.

  • kenneth.leekenneth.lee USMember ✭✭✭

    @AntonEmelyancev So i finally decided its time to give FFImageLoading a go and this is what I got...

            var cachedImage = new CachedImage()
            {
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.Center,
                DownsampleUseDipUnits = true, // have tried this with false aswell 
                WidthRequest = 300,
                HeightRequest = 300,
                DownsampleWidth = 4096,
                DownsampleHeight = 4096,
                Source = image.Source
            };
    

    image.Source is a image that is 5000px by 5000px and even with these settings it didn't seem to downsample it to 4096x4096 or 300x300 (Which was just a hopeful attempt). It still however is giving me
    Bitmap too large to be uploaded into a texture (5000x5000, max=4096x4096)
    I'm not sure exactly what I'm doing wrong.

  • anton.emelyancevanton.emelyancev USMember ✭✭
    edited July 2017

    Just read what intellisence tells you about DownsampleUseDipUnits property. Or some documentation/samples.

    If set to true DownsampleWidth and DownsampleHeight properties will use dip units otherwise, false

    You are to use DownsampleToViewSize property to enable downsampling.

    No offence and I'm not complaining, but it will be more efficient if people at first tried to read some documentation or even intellisence tooltips (they actualy are created for such use case - to provide tips). Everyone makes such mistakes, but a bit more investigating prevents from such typos in future.

  • kenneth.leekenneth.lee USMember ✭✭✭
    edited July 2017

    @anton.emelyancev

    The reason i tried both true and false values for DownsampleUseDipUnits is that i'm not sure which unit OpenGLRenderer is using cause all i get is ...

    @kenneth.lee said:
    Bitmap too large to be uploaded into a texture (5000x5000, max=4096x4096)

    Also I want to state that I have already read the API on the wiki and i have already tried DownsampleToViewSize - probably i should have stated that.

    With DownsampleToViewSize enabled I still get a image of size 5000x5000 and the exact error stated before.

    EDIT:

    the error actually states...

    W/OpenGLRenderer(18538): Bitmap too large to be uploaded into a texture (5000x5000, max=4096x4096)

  • kenneth.leekenneth.lee USMember ✭✭✭

    Currently the fix i found was to setting large heap size to true and disabling accelerated hardware on the android manifest. This apparently seems like a quick fix and might pose issues to your app ... but so far it is the appropriate fix for me right now.

  • vinothswamivinothswami Member ✭✭✭

    provide the extension even if it is .png image format.

Sign In or Register to comment.