Invalid image data - xamarin

my problem is that image_1 I displayed as logo in 'Xamarin.Forms.Image' worked good until i changed it to image_2. It did not work, so I changed it back to image_1. Now it does not work too and I got some errors:
[0:] Could not load image named: {0}: nurse.png
[0:] FileImageSourceHandler: Could not find image or image file was invalid: File: nurse.png
[0:] ImageLoaderSourceHandler: Image data was invalid: Xamarin.Forms.StreamImageSource
As I said, it was working before the change. My Xamarin.Forms.Image looks like this:
<Image
x:Name="main_logo"
Source="logo.png"
Aspect="AspectFit" >
logo.png is in main Xamarin.Forms part of solution (where the App.xaml is).
I do not understand why it does not work now If it did before. Same error occurs when i try to make ImageButton. I tried to clean/rebuild, delete all files from /bin folder and then rebuild, not working. Any help appreciated!

Related

Image from AppDataDirectory failed to be displayed in the Image view

When my .net maui app starts it loads some images from the cloud and stores to FileSystem.AppDataDirectory folder. Then in offline mode it supposed to take them from the folder to display in the Image view, but the code does not work (binding will be used, but for the demo sake it's hardcoded):
<Image WidthRequest="30" HeightRequest="30">
<Image.Source>
<FileImageSource File="/data/user/0/mytestapp/files/Icontest.svg" />
</Image.Source>
</Image>
Same image works fine if loaded from the cloud when I replace File source to Url source.
/data/user/0/mytestapp/files/ is what FileSystem.AppDataDirectory call returns, I just added file name to the end
Any idea if it's supported at all to load image files from the folder?
note: caching with url source does not serve the app requirements.
SVG get pre-rendered and ship with your app. You have to reference them as PNG in your XAML. (On IOS, it wont even display if you use .svg) But this is not it.
Once replace your SVG resources with PNG. You will hit another problem.
The android will cache your files in your file system, and there is nothing you can do about it: https://github.com/dotnet/maui/issues/9773
There are two work arounds:
Change the name of the file every time, so it is never found.
Clear the cache by deleting everything from here: Path.Combine(FileSystem.CacheDirectory, "image_manager_disk_cache");
I think the negative effects of both workarounds are obvious. Fingers crossed that it gets fixed next year.
At first, the Image control can't get the file from the device's external storage directly. The FileImageSource is used to get the image file in the project which is set as embedded resource.
So you can need to use a stream to read the file and then use the ImageSource.FromStream to set the image source.
Such as:
var memoryStream = new MemoryStream();
using (var source = System.IO.File.OpenRead(path))
{
source.CopyTo(memoryStream);
}
image.Source = ImageSource.FromStream(() => memoryStream)
But there is a bug about ImageSource is not work when set Image.Source to file in external storage. Even the image file has been read as a stream, the image will not display. You can follow up this issue on the github.

Problems loading image

example :
Image files do not work when loaded using the method
response()->download(public_path('/users/').$attachment->path) ;
The error I get is
Could not load image 'name.jpeg'
The error you say is different than the error in the image. Are you sure the jpeg is not corrupted? Can you open the image in other program? And verify the file is located in
/public/users/name.jpeg

Image is not visible in android project with XAML forms - Xamarin even though provided solutions applied

I tried solutions from the following links but none worked.
where-to-put-images-for-xamarin-froms-application
developer.xamarin.com/guides/xamarin-forms/user-interface/images/#Local_Images
image-not-showing-in-xamarin-forms-app.html
I used below syntax
<Image Source="lock.png" HeightRequest="20" WidthRequest="20" HorizontalOptions="Center" VerticalOptions="Center" Grid.Row="0" Grid.Column="0"></Image>
Here is the image for where i've kept image
No image displays
Edit
These are the only things in my XAML page.
In my case the issue was of assembly.
when trying all the possible solutions like
1.) Changing Build Action
2.) Adding Image to all the folders within Resources
3.) Naming Convention should not include - in File/Image Name.
4.) Changing Background Color
I also got one solution while searching for embedded Images that assembly was missing here.
This change worked for me in using Local Images.
Got that solution from this link
Right click the lock.png image and see if there is an option "include in project"
If there is, project cannot find image
In my case I need to use method Forms.Init(Context activity, Bundle bundle), in my SplashScreen OnCreate method and Activity class OnCreate method. But it also important that images build action properties is set on AndroidResource.

Application Resources Not Working as Image Source?

I have a very basic layout which includes a spot for an image:
<Image src="{{ cIconSrc }}" />
At first I uploaded an image to the ./resources folder and set the source of the image as:
"~/resources/chrome_icon.png"
...which worked great. Then I read that the correct way to do it is to create several variations of the image and place them within the App_Resources folder structure for both Android and IOS. I've done that, but when I try to switch my code so that it sets the source of the image using:
res://chrome_icon
...it is not working. I don't see the image shown on the page at all. Can someone please tell me what I'm missing? Here are a few screenshots so that you can see that I put the icon variants where they should go:
Android folders
IOS screenshot

Silverlight: Image Source with no file extension doesn`t display

In my Silverlight project, images for which the source URI does not contain the file extension don`t display, although the documentation says it should.
I set the image source like so:
imgCompanyLogo.Source = new BitmapImage(new Uri(Application.Current.Host.Source, "/Files/" + logoName));
Now, if "logoName" contains the file extension (like ".png" for example), the image is displayed fine, but it simply doesn't if the file is stored without an extension.
This seems to contradict the documentation here which states:
"The format-specific filename extensions such as .png are not necessarily required to be in the URI naming, but if the retrieved file is not determined to be a valid image format, a runtime exception is thrown."
I'm not getting any runtime exception either.
Is this a known issue or am I missing something simple?
Thanks!
PS: Just a little twist, the images display fine while debugging, not when the system is deployed...
I've made some test and the problem seems to be due to the response from the server.
If you try using .png within your project with a Build Action sets to Resource, both images will load regardless the extension.
Now if you try with images hosted on a server, it won't have the same behavior. Actually, if you try to browse the link to an image without extension directly in your browser, it will result in something else. On Chrome it will download the file, and on IE it will display the result as plain text.
That's because of the MIME type. A png should be returned with the type image\png.
There is a trick with .htaccess to set up the MIME-type but you need to have to specify for which extension. It works like this:
AddType image\png yourExtension [Extension2] [Extension3] ..
And if you want to see if why the image didn't load on your Image control, you can add an eventhandler to the ImageFailed event:
<Image Source="..." ImageFailed="Image_ImageFailed" />
But the error message that you will see is not really helpful:
ErrorException = {System.Exception: AG_E_NETWORK_ERROR}

Resources