Showing animated gif in Winforms without locking the file - c#

I'm trying to display images of various file types (including animated .gif files) in my Winforms application. I also have to be able to modify the files that are shown. (change the file name, delete them).
The problem is that a Picturebox locks the image file until the application is closed when using the normal way.
That means I can't do this:
private void Form1_Load(object sender, EventArgs e)
{
PictureBox pic = new PictureBox();
pic.Size = new Size(250, 250);
pic.Image = Image.FromFile("someImage.gif");
this.Controls.Add(pic);
//No use to call pic.Image = null or .Dispose of it
File.Delete("someImage.gif"); //throws exception
}
The workaround in the link above is as follows:
private void Form1_Load2(object sender, EventArgs e)
{
PictureBox pic = new PictureBox();
pic.Size = new Size(250, 250);
//using a FileStream
var fs = new System.IO.FileStream("someImage.gif", System.IO.FileMode.Open, System.IO.FileAccess.Read);
pic.Image = System.Drawing.Image.FromStream(fs);
fs.Close();
this.Controls.Add(pic);
pic.MouseClick += pic_MouseClick;
}
That works fine for normal image types, but it won't load animated .gifs, which is important to me. Trying to load one will make it look like this.
I've found a few other topics about it (this and this) but they're all about WPF and use BitmapImage. I've searched about how to use BitmapImage in a Winforms application, but haven't found anything apart from that it is supposed to somehow work.
I would like to stay with Winforms because I'm just getting used to it, but that's not a necessity.
To summarize: I need a way to show common image types (png, jpg, bmp, and animated gif) while still being able modify the file on the HDD. It is OK if that means unloading->modifying->reloading the file. I'd prefer Winforms, but other Frameworks would do.
Thanks for your help.
Edit: Another way I've tried
using (System.IO.FileStream fs = new System.IO.FileStream("E:\\Pics\\small.gif", System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
fs.CopyTo(ms);
pic.Image = Image.FromStream(ms);
}
But shows the same problem as the second example. The gif doesn't load.

Using a MemoryStream is indeed the right way to avoid the file lock. Which is a strong optimization btw, the lock is created by the memory-mapped file that the Image class uses to keep the pixel data out of the paging file. That matters a great deal when the bitmap is large. Hopefully not on an animated gif :)
A small mistake in your code snippet, you forgot to reset the stream back to the start of the data. Fix:
using (var fs = new System.IO.FileStream(...)) {
var ms = new System.IO.MemoryStream();
fs.CopyTo(ms);
ms.Position = 0; // <=== here
if (pic.Image != null) pic.Image.Dispose();
pic.Image = Image.FromStream(ms);
}
In case it needs to be said: do not dispose the memory stream. That causes very hard to diagnose random crashes later, pixel data is read lazily.

Essentialy you'll have to make a copy of the image file in your memory.
Pre .Net 4.0 (2.0,3.0,3.5) you'd have to create a FileStream and copy it to a MemoryStream and rewind it, as seen in another answer.
Since .Net 4.0 (4.0,4.5,...) Image.FromFile supports animated GIF's
If you work with .Net 4.0 or later following method will suffice:
Using System.IO, System.Drawing and System.Drawing.Imaging
private void Form1_Load(object sender, EventArgs e)
{
string szTarget = "C:\\someImage.gif";
PictureBox pic = new PictureBox();
pic.Dock = DockStyle.Fill;
Image img = Image.FromFile(szTarget); // Load image fromFile into Image object
MemoryStream mstr = new MemoryStream(); // Create a new MemoryStream
img.Save(mstr, ImageFormat.Gif); // Save Image to MemoryStream from Image object
pic.Image = Image.FromStream(mstr); // Load Image from MemoryStream into PictureBox
this.Controls.Add(pic);
img.Dispose(); // Dispose original Image object (fromFile)
// after this you should be able to delete/manipulate the file
File.Delete(szTarget);
}

Related

SaveAs and delete same time c# [duplicate]

I am trying to delete local copy(on the computer) of an image file once uploaded using file dialog. It throws The process cannot access the file 'C:\Documents and Settings\username\My Documents\My Pictures\1220.bmp' because it is being used by another process.
private void _btnImportPhoto_Click(object sender, RoutedEventArgs e)
{
//user clicked import/change photo, open file dialog to browse for photo
System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
fileDialog.Multiselect = false;
fileDialog.Filter = ResourceFile.PhotoFileTypes;
if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FilePath = fileDialog.FileName;
FilePathCopy = fileDialog.FileName;
string safeFilePath = fileDialog.SafeFileName;
Bitmap bitmap = new Bitmap(FilePath);
CurrentPhoto = bitmap;
Bitmap bitmap1 = new Bitmap(FilePathCopy); //A copy to save when if delete local copy is chosen
m_PhotoCopy = bitmap1;
FileSystem.DeleteFile(FilePath);
}
_btnSave.IsEnabled = _btnCancel.IsEnabled = true;
}
}
Please let me know how to work around this issue.
Thanks.
you need to dispose of the bitmap object try doing this. As this will dispose of the bitmap object as soon as it leaves the using context { }
using (Bitmap bitmap1 = new Bitmap(FilePathCopy))
{
//do all bitmap stuff in here
}
First I see
Bitmap bitmap = new Bitmap(FilePath);
CurrentPhoto = bitmap;
Where CurrentPhoto I presume some global variable that you want hold.
This, instead throws and exception:
FileSystem.DeleteFile(FilePath);
Cause image file at FilePath is actually CurrentPhoto. What you can do.
1) If use of CurrentPhoto has any sense inside this function, do what you want to do inside this function, and after dispose CurrentPhoto object, even in a way like #Bobby suggests (using block)
2) If you want to have it by the way, you can try to use Bitmap's Clone
method like this:
CurrentPhoto = bitmap.Clone();
and after call your:
bitmap.Dispose();
FileSystem.DeleteFile(FilePath);
Should work.
Try this...
http://www.lockergnome.com/blade/2006/11/28/windows-error-message-error-deleting-file-or-folder/
It will allow you to delete files or folder.

Images not appearing on WPF form when loading asynchronously

I'm attempting to display (in a ListBox with a custom DataTemplate) a series of BitmapSource frames (thumbnails) extracted from a multi-page tiff image. When I process the tiff on the UI thread, and either directly add the images to a listbox's item collection or add them to a bound ObservableCollection, they show fine in the list. However, when trying to load each thumbnail asynchronously (either with BackgroundWorker or using asynchronous tasks), I see behavior I can't work out:
The first thumbnail loads as expected
The next, and all subsequent thumbnails get items in the list (I see the border), but all that shows is a blank image. It shows the correct number of items, but no images after the first.
I've played around with trying to freeze the thumbnails (no good), trying to send them back to the UI thread and adding them to the collection there via the worker's ReportProgress (no good), but I can't seem to get them to show.
Working on UI thread (where SyncImages is an ObservableCollection bound to my ListBox, and OnPropertyChanged handles the notification event):
private void LoadSynchronous()
{
Stream imageStreamSource = new FileStream(ImagePath, FileMode.Open, FileAccess.Read, FileShare.Read);
var decoder = BitmapDecoder.Create(imageStreamSource, BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.Default);
foreach (var frame in decoder.Frames)
{
//frame.Freeze(); //Tried this but no effect.
SyncImages.Add(frame);
}
OnPropertyChanged("SyncImages");
}
Not working (this example adds the frames directly to the list, but I've also tried via binding with no difference in result):
private void LoadAsync(object sender, DoWorkEventArgs e)
{
Stream imageStreamSource = new FileStream(ImagePath, FileMode.Open, FileAccess.Read, FileShare.Read);
var decoder = BitmapDecoder.Create(imageStreamSource, BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.Default);
foreach (var frame in decoder.Frames)
{
// frame.Freeze();
(sender as BackgroundWorker).ReportProgress(0, frame);
}
OnPropertyChanged("AsyncImages");
}
private void ReportAsyncProgress(object send, ProgressChangedEventArgs e)
{
var frame = (BitmapSource) e.UserState;
LbAsynchronous.Items.Add(frame);
}
Hoping someone can shed some light on what is going on here. I know the code works to extract the frames, so even in the async example they must be loaded, but it seems almost like the UI thread can't access the properties of the source that hold the image data to display them on the form (which is why I tried freezing).
Any thoughts would be appreciated!
Example image: http://i.imgur.com/75wMkmS.png
#Clemens answer from his comment on the original question provided the solution. Ensuring that the file stream was being closed responsibly and changing the BitmapCacheOption to OnLoad now shows each image in the asynchronous load.
The final code for the asynchronous load looks like:
private void LoadAsync(object sender, DoWorkEventArgs e)
{
BitmapDecoder decoder;
using (Stream imageStreamSource = new FileStream(ImagePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
decoder = BitmapDecoder.Create(imageStreamSource, BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.OnLoad);
}
foreach (var frame in decoder.Frames)
{
frame.Freeze();
(sender as BackgroundWorker).ReportProgress(0, frame);
}
}
private void UpdateAsync(object send, ProgressChangedEventArgs e)
{
SyncImages.Add((BitmapSource)e.UserState);
OnPropertyChanged("SyncImages");
}

Image box flickers after new image is loaded

So i have this code which for every string in the array it adds it the file path and uses it as the file path for the image box, this is the code:
private async void Button7_Click(object sender, RoutedEventArgs e)
{
string[] images = new string[] { "Star_00001.png", "Star_00002.png", "Star_00003.png", "Star_00004.png", "Star_00005.png", "Star_00006.png", "Star_00007.png", "Star_00008.png"};
string path = "Assets/Star/";
foreach(string file in images)
{
string thepath = Path.Combine(path,file);
await Task.Delay(46);
BitmapImage Image = new BitmapImage();
Image.UriSource = new Uri(this.BaseUri, thepath);
StarImage.Source = Image;
}
}
Now everytime the new image is loaded in to the StarImage, it flickers, as far as I know their is no way to stop this because it is the effect of loading a new image in the image box, however does anyone know any alternatives to stop this and give the effect of an animation?
I think some sort of buffering technique may reduce or eliminate the flickering problem you have.
I've never done this in c#, but essentially you draw to an image which is not yet visible referred to as the buffer, and then make it visible when the image is completely drawn.
This may help.
try this:
private async void Button7_Click(object sender, RoutedEventArgs e)
{
string[] images = new string[] { "Star_00001.png", "Star_00002.png", "Star_00003.png", "Star_00004.png", "Star_00005.png", "Star_00006.png", "Star_00007.png", "Star_00008.png" };
string path = "Assets/Star/";
foreach (string file in images)
{
string thepath = Path.Combine(path, file);
await Task.Delay(46);
BitmapImage Image = new BitmapImage();
Image.BeginInit();
Image.UriSource = new Uri(this.BaseUri, thepath);
Image.CacheOption = BitmapCacheOption.OnLoad;
Image.EndInit();
StarImage.Source = Image;
}
}
I've been racking my brains for months trying to solve this, and then I found this post. It helped me solve this! I can't understand for the life of me why Microsoft keeps changing the syntax to do things in windows.
How to set Background of a Button without flicker?

Dynamic WPF Image Loading Issue

I have an issue that I think has not been covered in the multitude of other WPF image loading issues. I am scanning in several images and passing them to a "Preview Page". The preview page takes the image thumbnails and displays what a printout would look like via a generated bitmap.
The weird thing to me is, it will work fine if I run the program the first time. Upon reaching the end of the process and hitting "start over", the preview will return blank. I am creating the BitmapImage in a method that saves the bitmap as a random file name so I do not believe theres a lock on the file the second time around. Also, if I go to look at the temporary file created through explorer, it is drawn correctly so I know the appropriate data is getting to it.
Finally, when I navigate away from this page, I am clearing necessary data. I'm really perplexed and any help would be appreciated.
//Constructor
public Receipt_Form() {
InitializeComponent();
printData = new List<Object>();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e) {
// populates global variable fileName
var task = System.Threading.Tasks.Task.Factory.StartNew(() => outputToBitmap()); task.ContinueWith(t => setImage(fileName),
System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext());
// I started the image creation in a separate thread because I
// thought it may be blocking the UI thread, but it didn't matter
}
private void setImage(string imageURI) {
BitmapImage image;
using (FileStream stream = File.OpenRead(imageURI)) {
image = new BitmapImage();
image.BeginInit();
image.StreamSource = stream;
image.CacheOption = BitmapCacheOption.OnLoad;
image.EndInit();
}
receiptPreview.Source = image;
//this works the first iteration but not the second, though the temp file is created successfully
}
Found the issue - the Modern UI container was getting cleared when transitioning off the page.

Getting the size of a downloaded image in Silverlight

If I've got a Silverlight Image control that downloads from a full URL, how can I get the size (in bytes) of the downloaded image without making another web call?
I can't find anything on the Image or the BitmapImage source behind it that would tell me. And even the DownloadProgress event on the BitmapImage only gives a percentage.
I never noticed it before, but that is kind of a strange gap in the framework...
You'll probably have to download the image by itself using a WebClient object. That'll give you a stream of bytes. You can check the length of the stream, and then create a bitmap from the stream.
Code to set up the web client and begin the download (note, it's an async call, so we assign an event handler to fire when it completes the download.)
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
Uri someImageUri = new Uri("http://www.somesite.com/someimage.jpg");
wc.OpenReadAsync(someImageUri);
Here's an example of what the event handler method might look like:
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
System.IO.Stream imageStream = e.Result;
long imageSize = imageStream.Length;
BitmapImage bi = new BitmapImage();
bi.SetSource(imageStream);
Image image = new Image();
image.Source = bi;
}
Obviously, if you already have an image control on your form, you wouldn't need to create a new one, or if you did want to create it, you'll have to add it to a parent panel of some kind...
~Chris

Categories

Resources