Xamarin forms: Image Cache - c#

Hi I am trying to build an app using xamarin forms PCL. I am implementing image gallery in which I have used a default image. All the images are on blob. I want to download image and cache that Image in device and as soon as download is complete I need to replace my default image with it.
And on loading app next time download image only if it is not present in cache.
I dont get any plugin for image caching and loading image from cache.
I have seen a plugin named FFPLUGIN but it didnt work.
Any idea how I can implement this? IMAGE CACHING

You could use the built in ImageCaching in Xamarin Forms shown here:
https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Downloaded_Image_Caching
Downloaded Image Caching
UriImageSource also supports caching of
downloaded images, configured through the following properties:
CachingEnabled - Whether caching is enabled ( true by default).
CacheValidity - A TimeSpan that defines how long the image will be
stored locally. Caching is enabled by default and will store the image
locally for 24 hours. To disable caching for a particular image,
instantiate the image source like this:
Image.Source = new UriImageSource {CachingEnabled = false,
Uri="http://server.com/image"}; To set a specific cache period (for
example, 5 days) instantiate the image source like this:
webImage.Source = new UriImageSource {
Uri = new Uri("https://xamarin.com/content/images/pages/forms/example-app.png"),
CachingEnabled = true,
CacheValidity = new TimeSpan(5,0,0,0) };
Built-in caching makes it very easy to support scenarios like scrolling lists of images, where
you can set (or bind) an image in each cell and let the built-in cache
take care of re-loading the image when the cell is scrolled back into
view.

Related

How can I assign an image in mdpi folder to an ImageView in Xamarin?

How can I assign an image to an ImageView in Xamarin?
To do this before what I did was to add the images to "drawable" and from design mode (Designing Layout) I select the image in the "src" field, leaving something like this:
src | # drawable / splashlogo
 
and if I wanted to do it from code at runtime I just put:
    
var drawableImage = Resources.GetDrawable (Resources.GetIdentifier ("splashlogo", "drawable", PackageName));
ProfileIcon.Background = (drawableImage);
The problem with this method is that I started having memory consumption problems because the resolution of the images was very high, and now that I am using the folders intended for each screen density (mdpi, hdpi, xhdpi ...) I don't know how assign the images neither at runtime nor in the layout design.
Could someone tell me how to do it please?
as Cheesebaron said,Android will automatically pick the correct image based on the display density from your mdpi, hdpi, xhdpi folders.if you want to set Image of your mipmap in you code-behind,you could try this:
ProfileIcon.SetImageResource(Resource.Mipmap.splashlogo);
or
var drawableImage = Resources.GetDrawable(Resources.GetIdentifier("splashlogo", "mipmap", PackageName));
ProfileIcon.SetImageDrawable(drawableImage);
Nothing should change. Android will automatically pick the correct image based on the display density.
Also when assigning image from code-behind instead of in layout. Consider just passing along the Resource Id to the ImageView, instead of first loading the drawable into memory, then pass it along to the ImageView. This can simply be done like:
ProfileIcon.SetImageResource(Resource.Drawable.splashlogo);
Otherwise, if you insist on loading it from resources first, I highly suggest you put it in a using statement to dispose of the managed side:
using(var drawableImage = Resources.GetDrawable(Resources.GetIdentifier ("splashlogo", "drawable", PackageName)))
ProfileIcon.Background = (drawableImage);

Locked resources (image files) management

The application is a desktop document management system. Image files (of scanned docs) are stored within a shared network folder and its indices within a database. Now, when the image of a selected document page is displayed the user has the option to delete it (via a contextual menu). The problem is, if I try to do this then it throws an exception (the resource is locked) which has all the sense given that it's being shown on screen. So, currently I maintain a persistent delete queue. Once the app starts I go to the queue and delete the pages of the documents whose indices were deleted from DB and given that they aren't being displayed the deletion succeed but this seems to be pretty bad code (I mean it works, but not as clean as it should, I guess).
How bad my quick solution is. Given that the app is single-user then the user needs to star the app to use it. Is this a very bad idea or can I implemented using another path.
The images are shown (within the document viewer) by binding it to the current file:
View:
<Image Name="PageViewedPath" Margin="20" Grid.Column="0" />
ViewModel:
public string PageViewedPath { get; set; }
And once the user clicks next or previous I change (within the ViewModel the PageViewedPath). Maybe the problem is this binding which I can't control in detail, I'm using Caliburn Micro so that's why just by setting the image name the binding is done.
I think maybe overriding this binding and creating a hardcopy of the image before is being shown must work but I'm not sure if it will and worse, how to do it.
I've had a similar problem in an application I developed that was using an image pool. Although the image was not displayed anymore, the file was locked and could not be deleted.
I solved my problem by loading images with BitmapCacheOption.OnLoad, something like this:
Image myImage = new Image();
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.UriSource = imageUri;
// End initialization.
bi.EndInit();
myImage.Source = bi;
Here's a link to an msdn post that shows how to use BitmapCacheOption from xaml:
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/3cb97997-941f-42a8-a03b-f84b152c1139/
If you code lock the files from your own code - stop locking. You probably missing some using/Dispose calls somewhere around loading an image.
If it is not your code or you need to handle failures due to using shared files location - your solution may be ok. Also most users will not expect such behavior - my normal expectation is that file is either deleted instantaneously or never.

I draw diagrams, but I see old image on the new page

I am developing a C# web project. I run it on the local web server.
I draw. I show image as follows:
bitmap.Save(Server.MapPath("diagram.jpg"), ImageFormat.Jpeg);
Image1.ImageUrl = ResolveUrl("diagram.jpg");
I don't see new image. Only old one, which I had after changing image name
(Say, I change diagram.jpg to diagram2.jpg).
Browser is Firefox.
The design page in C# is simple. Just Image and few TextBoxes on the page.
No UpdatePanel and such.
Something with caching... But how to fight with that...
But how to fight with that.
Always use a separate path / name. Pug a GUID somewhere. Simple like that. Different file can not be cached.
I'm not sure what ResolveUrl does, but try adding a querystring to the image url so that the page always gets a "fresh" file. Something like this:
Image1.ImageUrl = ResolveUrl(string.Format("diagram.jpg?v={0}", Guid.NewGuid()));
You can alternative write the image file as
diagram.jpg?ver=2
to keep the same image file, but force the browser to update it.
If Image has Same name and URL browser picks the image from the cache and displays the same for faster loading of the pages.
Even if you change the image server side the same cached image is displayed until you clear the cache of the browser. You can use query string to change image url like below.
Image1.ImageUrl = ResolveUrl("diagram.jpg?" + DateTime.Now.Ticks.ToString());

how to resize a picture programatically and add it to the web site

Hi there
I am working with visual web developer and would a) like to know how I could programatcally add a picture to the website, instead of going to Website and then add existing item. The idea I have is to upload the picture using the file upload control but then I want to have it added so that it can be accessed using a gridview.
b)I would also like to know how to size the picture using C# code obviously so that is displayed at the correct size.
best regards
arian
here is a full project with source code to manipulate images, including resize.
http://www.codeproject.com/KB/web-image/ASPImaging1.aspx
And a sample code only for resize
http://weblogs.asp.net/gunnarpeipman/archive/2009/04/02/resizing-images-without-loss-of-quality.aspx
You can use GetThumbnailImage to easily create a smaller verson of the uploaded image. The code looks something like (it's free typed without a compiler, so there may be some errors):
System.Drawing.Image pic = new System.Drawing.Bitmap(sourceFilename);
System.Drawing.Image thumb = pic.GetThumbnailImage(targetXSize,targetYSize,
new System.Drawing.Image.GetThumbnailImageAbort(this.GetThumbnailImageAbort),
IntPtr.Zero);
thumb.Save(thumbPathName);
I believe the Image implements IDisposable, so you need to remember to Dispose of them when you're done.
The abort parameter can be a function that simply does this (can't remember off the top of my head when it gets called):
bool GetThumbnailImageAbort() {
return false;
}

How to display Images from internet in a windows phone 7 app?

I am making an application which gets urls of images by parsing an RSS feed.I want to diplay that images in the application one after another when tapped on screen .How can i do it? Is it required to download all images before displaying? Please explain.
Thanks and regards
vaysage
Maybe I don't understand your question correctly but you should be able to set the Source of an Image element directly to an URI specified in your RSS feed item.
<Image x:Name="m_Image" Source="http://www.microsoft.com/silverlight/images/ms-silverlight-logo.png"/>
When changing item (by tapping) you can easily swap the source of the image from your code.
Uri uri = new Uri("...", UriKind.Absolute);
ImageSource imgSource = new BitmapImage(uri);
m_Image.Source = imgSource;
Using LowProfileImageLoader (as mentioned by Thomas Joulin and Mick N) is a good way to load images in the background and keep the UI responsive.
Parse you're RSS Feed to get the images URL's (using for exemple HTTPWebRequest)
Set the binding for the source of each of you images (since it's web based, I recommend LowProfileImageLoader which will load images in the background.
Create a SlideShow.xaml view, based on a pivot. Dynamically add Pivot items
On tap on a thumbnail, launch the SlideShow.xaml, at the specified index
You will need to get the images download in some form.
You might find these posts an interesting read.
Keep a low profile [LowProfileImageLoader helps the Windows Phone 7 UI thread stay responsive by loading images in the background] - Delay's Blog
**There's no substitute for customer feedback! [Improving Windows Phone 7 application performance now a bit easier with LowProfileImageLoader and DeferredLoadListBox updates] - Delay's Blog

Categories

Resources