Locked resources (image files) management - c#

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.

Related

Overwriting and Image Open and Currently Binded to In WPF Program

I am binding to 5 different images in my program and wish to leave the capability of the user to replace or update the photos under the same name. So when updating these pictures, the binding will be notified and change while the program is running.
The program is a Digital VMB (visual management board) at my workplace, so it needs to remain running and have these photos be updated on the server without a hitch. Currently I am binding to a property in my C# which is a string containing the image location.
In c#:
public string OpenFilePathProp { get; set; }
In XAML:
<Image x:Name="OpenWOImage" Source="{Binding OpenFilePathProp}" Stretch="Fill" Margin="25,0,25,0"/>
When the user goes to copy and replace the image with the newer one, they can't as it's "currently open in another process". Which I suppose is the data binding in my WPF.
Can this be overcome by opening the images into a filestream and then binding to the stream? If so, I'm completely unsure on how to bind to a filestream; I'm quite new to WPF AND C#.
Thanks for the help. I HAVE tried to look for a solution to this, but I think I'm just getting confused and I don't think it will resolve my problem since it seems the binding is what's "keeping the image open" and I'm not sure how to bind to an Image object AND close that object to allow for overwriting
EDIT: Thought I should mention that I've managed to copy the images in question to the AppData folder for my VMB program,
like,
string AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\";
System.IO.File.Copy(Properties.Settings.Default.OpenWOFilePath, System.IO.Path.Combine(AppData, "ROAMaintenanceVMB\\OpenWOFilePath_Copy.jpeg"), true);
this way I can "check" every so often to see that someone has overwritten the photos on the server, and THEORETICALLY copy the "new" photos to the AppData folder, overwriting the previous versions. THIS is where the issue of the images already being open in another program arises.
Following Peter Duniho suggestion about using WriteableBitmap worked in my instance.
string imagePath = "";
Uri imageUri = new Uri(imagePath);
BitmapImage bitmapImage = new BitmapImage(imageUri);
ImageProperty = new WriteableBitmap(bitmapImage);
<Image Source="{Binding ImageProperty}"/>
What's going on here is:
BitmapImage is created from the given path
WriteableBitmap is created based on the BitmapImage (a "deep copy"?)
The important thing here is the original image is not locked, as the binding is tied to the copy of the image of WriteableBitmap - not to the image itself, so the image can be deleted / replaced freely.

Xamarin forms: Image Cache

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.

Displaying image that was previously uploaded to server

For a project I'm working I would like the user (admin) to be able to change the picture on the page he is currently on. I managed to upload the image to "the server" using interlink. This basicly uploads it to a given folder on a server, in my case being: Interlink/Uploads.
But now I don't really know how can I tell my website to replace the source of the image that is currently shown with the source of the uploaded image.
Another something I would like to do is create a simple image gallery with all the images in that folder, once again I don't know how to do this.
I hope somebody can help me, Thanks.
Thomas
Edit: Just so clarify, the application is written in silverlight (XAML, C#). I apologise for any inconvenience.
I take it that the "Silverlight" portion of this question is related only to Interlink (your file uploader), and not to the page itself, which I presume is straight HTML.
If that's the case, you've got several options for changing the local image. The simplest way is simply to wait until you know that your file upload has finished (presumably Interlink has some way of notifying you that this is the case), and then run something like this bit of JavaScript:
<script type='text/javascript'>
function changeImage(newImageSource) {
document.getElementById('myTargetImage').setAttribute('src', newImageSource);
}
</script>
As far as displaying a simple image gallery with all the images in the folder, my recommendation would be to look into one of the numerous jquery plugins that handle this sort of thing, e.g.:
http://www.1stwebdesigner.com/css/fresh-jquery-image-gallery-display-solutions/
EDIT: Silverlight Options
You basically have the same options, except you're doing them in C# instead of JavaScript. For instance, when Interlink tells you that the new image has been uploaded, run this:
string imageName = "something.jpeg";
var ub = new UriBuilder(HtmlPage.Document.DocumentUri);
ub.Path = "/Interlink/Uploads/" + imageName;
img.Source = new BitmapImage(ub.Uri);
And for an image carousel, something like this:
http://3dimagecarousel.codeplex.com/
You'll just need to provide the URL's of all the images. The easiest way to do that is probably to expose a web service method that lists them all.

Image not loading correctly from BitmapImage in code behind

Okay, I'm attempting to load an image to an Image control on my page. Now as far as I know, I know how to do this (the method I'm using has worked before). But for some reason it's not working this time. The image is not being loaded.
Okay, let's start with the XAML. You can see it here.. Please note my excellent artistic skills. You can see that the top image is the left one on the screen and the bottom image is the right one. As you can also see, the image on the right is loaded in the XAML from a file in the Assets/Images folder. It should also be noted that we can take from this that the image loads okay - there aren't errors with the image. We can also see that the images are not blocked by anything (when the program runs, the right image shows just fine).
So the left image, 'image1' is the one that I'm loading from the code behind. Here is the code behind for that page. As you can see, there's not a lot. Yes I do use a view model, but it doesn't interface with the image at all (its only exposed properties are an Entity (for a selected item) and an ObservableCollection of entities (for the ItemsSource of a control)). As you can also see, I'm attempting to load the same image as is used for the static image (so I can say for sure that the image is okay for Silverlight).
If you're wondering about the BitmapCreateOptions line, that was something that someone suggested to me to add. I have tried removing it, but that doesn't seem to cause any difference.
The next question is - have I attempted to use the event handler for the failure? Yes I have. The error I get is 'AG_E_NETWORK_ERROR'.
I'm not sure what exactly this is about - I'm not loading from a network.
Can anyone offer assistance? Thanks.
I can't help but notice that in your XAML, you have a "../" at the start of the image path, but this is missing from the path you're using in code-behind. Could this be the problem?
Have You tried to call BeginInit and EndInit before and after setting the uri?
BitmapImage bmpImg = new BitmagImage();
bmpImg.BeginInit();
bmpImg.UriSource = new Uri(...);
bmpImg.EndInit();
Or use the constructor that takes an Uri. msdn

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;
}

Categories

Resources