I have following image resize and save code.
This code is executed in different thread in ASP.NET MVC Project. As
using (FileStream fs = new FileStream(file.File.FullName,
FileMode.Open, FileAccess.Read, FileShare.Read))
{
BitmapDecoder decoder = BitmapDecoder.Create(fs,
BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
var img = decoder.Frames[0];
img.Freeze();
double width = Size;
double height = Size;
if (img.PixelWidth > img.PixelHeight)
{
height = (int)(((double)img.PixelHeight / (double)img.PixelWidth)
* (double)width);
}
else
{
width = (int)(((double)img.PixelWidth / (double)img.PixelHeight)
* (double)height);
}
var target = new TransformedBitmap(img,
new ScaleTransform(width / (double)img.PixelWidth, height
/ (double)img.PixelHeight, 0, 0));
RenderOptions.SetBitmapScalingMode(target, BitmapScalingMode.HighQuality);
var t = BitmapFrame.Create(target);
RenderOptions.SetBitmapScalingMode(t, BitmapScalingMode.HighQuality);
target.Freeze();
t.Freeze();
JpegBitmapEncoder enc = new JpegBitmapEncoder();
enc.Frames.Add(t);
enc.QualityLevel = 90;
using (FileStream fsout = saved.File.OpenWrite())
{
enc.Save(fsout);
}
}
I have used using around every IDisposable object, WPF objects do not implement IDisposable so I cant dispose them manually.
I read somewhere that calling Freeze will prevent memory leaks, but even after this, no improvement. Memory usage of this process keep on increasing.
What I need is, when an Image is uploaded, it has to be resized in 4 different resolutions, 150x150, 350x350, 700x700 and 1000x1000 , each of these resolutions are invoked parallely using Parallel.ForEach method.
My rest of website is just plain simple Database access using Entity Framework, which I doubt can cause any memory leak upto 3GB of memory usage.
I tried putting this in a loop in a stand-alone test program and followed the function with an explicit GC.Collect(). On .NET 4.5 at least, this shows that this code does not leak -- the memory stays steady after many many calls.
I think you'll need to look elsewhere in your code. You should try to profile it with Analyze > Launch Performance Wizard... and select .NET Memory Allocations and see what's using the memory.
For fun, I also tried .NET 3.5 and didn't experience a leak their either.
Related
Okay I have a an wpf image object, and it displays live images. So I have used a timer to refresh the image.
public void LoadLiveImage()
{
System.Windows.Media.PixelFormat pf = PixelFormats.Bgr24;
int stride = 4 * ((24 * cameraFrame.img_width + 31) / 32);
BitmapSource bmpImage= BitmapSource.Create(cameraFrame.img_width, cameraFrame.img_height, cameraFrame.img_width, cameraFrame.img_height, pf, null, cameraFrame.img_pixel, stride);
RemoteCameraImage.Source = bmpImage;
}
void dispatcherTimer_Tick(object sender, EventArgs e)
{
LoadLiveImage();
}
No issues, this is working fine.
However, I tried to move this to a thread and no image is displayed.
private void showLiveImage()
{
while (this.isCameraViewOpen)
{
if (RemoteCameraImage.Dispatcher.CheckAccess())
{
System.Windows.Media.PixelFormat pf = PixelFormats.Bgr24;
int stride = 4 * ((24 * cameraFrame.img_width + 31) / 32);
BitmapSource bmpImage = BitmapSource.Create(cameraFrame.img_width, cameraFrame.img_height, cameraFrame.img_width, cameraFrame.img_height, pf, null, cameraFrame.img_pixel, stride);
RemoteCameraImage.Source = bmpImage;
System.Threading.Thread.Sleep(5);
}
else
this.RemoteCameraImage.Dispatcher.Invoke(DispatcherPriority.Normal, new ImageUpdater(this.showLiveImage));
}
}
The showLiveImage isrunning as a thread. The image is received, there is not problem in that. I tested by saving the img_pixel array to a bmp file and file is generated. Just that the image is not displayed on. So I put a messagebox to be shown after the source is assigned, and then I m able to see the image on Image object. SO I think the problem I increased the Sleep time, but even the image is not refreshed. WHAT could be the issue?
EDIT:
After moving the code which was updating the image to another function, it works fine. And I used BeginInvoke() instead of invoke an all works fine.
Sometime ago I had similiar problems and I found this on Stackoverflow, which sovled it for me
Images on second thread
WPF Dispatcher {"The calling thread cannot access this object because a different thread owns it."}
If you have no intention of modifying the image once you have created
it you can freeze it using Freezable.Freeze and then assign to
GeneratedImage in the dispatcher delegate (the BitmapImage becomes
read-only and thus threadsafe as a result of the Freeze). The other
option would be to load the image into a MemoryStream on the
background thread and then create the BitmapImage on the UI thread in
the dispatcher delegate with that stream and the StreamSource property
of BitmapImage.
Try to use the Application.Current.Dispatcher instead of the one you used.
I believe this will do the trick.
Cheers
I know very similar questions were asked here in the past - but neither had a solution for my problem:
I load a image from memory into a BitmapImage:
private static BitmapImage LoadImage(byte[] imageData)
{
if (imageData == null || imageData.Length == 0) return null;
var image = new BitmapImage();
using (var mem = new MemoryStream(imageData))
{
mem.Position = 0;
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = null;
image.StreamSource = mem;
image.EndInit();
}
image.Freeze();
return image;
}
And then use this (with INotifyPropertyChange) to bind the resulting BitmapImage to the Source of a Image object (on a Page).
Problem is: this will leak memory (a lot in my case up to 300MB on 2-3 images!)
You don't even find this using Profilers - only the .net Memory Profiler got me on track (as it's in unmanaged memory where all the bytes go - so ANTS tell me ".NET is using 19,24MB of 367,3MB total private bytes allocated to the application" - nice):
No matter what I try - I don't get this leak away.
Tried (single and all at once):
clear the Visual-Tree / remove the Image on Unload
Set the Image-Source to null
use ImageBrush in Rectangle instead of Image
other CacheOptions without Disposing the MemoryStream
don't Freeze the Image
I don't get this - really!
As soon as I stop using the Image in the Source everything is ok (no leak).
Someone any options I can try?
REMARK
Seems like this is no bug at all (see my second comment) - I have to check this so I will let the question open for now - maybe this can help with the other questions on this as well
Sorry guys - this was indeed no "BUG" but caused by high-resolution pictures.
Please comment on this if I should delete the question or if I should leave it here as other people might come into the same situation...
First, I load a BitmapImage into the Image control, whice is located on the Window.
Then I work with the Image control and then close the Window.
I do it 2-3 times in a minute and my memory fills up very quickly because the images do not unload from the memory for some reason when the window is closed.
So how do I unload BitmapImage from Image.Source control manually to free the RAM?
I believe the solution you are looking for is at http://www.ridgesolutions.ie/index.php/2012/02/03/net-wpf-bitmapimage-file-locking/. In my case, I was trying to find a way to delete the file after it was created, but it appears to be a solution to both issues.
Doesn't free up memory:
var bitmap = new BitmapImage(new Uri(imageFilePath));
Frees up memory, and allows file to be deleted:
var bitmap = new BitmapImage();
var stream = File.OpenRead(imageFilePath);
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.EndInit();
stream.Close();
stream.Dispose();
Optionally, also freeze the BitmapImage:
bitmap.Freeze();
In my situation it seems that the bitmap caching was the issue. I was previously loading bitmaps like this:
Bitmap bitmap = new Bitmap();
using(var stream = new FileStream(...))
{
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.EndInit();
}
bitmap.Freeze();
image.Source = bitmap;
Continuously replacing image.Source the same way just built up memory, manually forcing garbage collection wasn't really helping.
Instead, disabling the caching and having it use the stream (requires leaving the stream open until the image is displayed) paired with manual garbage collection eliminated memory build up for me.
Stream mediaStream;
void DisposeMediaStream()
{
if (mediaStream != null)
{
mediaStream.Close();
mediaStream.Dispose();
mediaStream = null;
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);
}
}
void Update()
{
DisposeMediaStream();
var bitmap = new BitmapImage();
mediaStream = new FileStream(...);
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.None;
bitmap.StreamSource = mediaStream;
bitmap.EndInit();
bitmap.Freeze();
ControlImage.Source = bitmap;
}
This way I can cycle through tons of images (like Windows Photo Viewer) and memory stays low. Note that the stream does not have to stay open once the image has actually rendered.
You can set the object to null, so that the BitmapImage object is no longer referenced.
In this situation, the GC should take care of freeing up resources.
You can call GC.Collect but it can affect performance if used too often.
You can call Dispose() on the images in the window's Closed event. I think it may also be possible to optimise your memory footprint using different caching options.
Edit:
You can't call Dispose(), instead, you might consider BitmapCacheOption.None. The image will be read directly from disk and not cached in memory.
There is a website that contains a single image from a webcam. Each time the site is hit, the most current image of the webcam is displayed. I want to make a real time video by hitting the site continuously.
I have searched and tried several things but cannot get it to refresh at a reasonable rate.
public MainWindow()
{
InitializeComponent();
this.picUri = "http://someurl";
this.thWatchVideo = new Thread(new ThreadStart(Watch));
_image = new BitmapImage();
_image.BeginInit();
_image.CacheOption = BitmapCacheOption.None;
_image.UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
_image.CacheOption = BitmapCacheOption.OnLoad;
_image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
_image.UriSource = new Uri(this.picUri);
_image.EndInit();
this.imgVideo.Source = _image;
this.thWatchVideo.Start();
}
public void Watch()
{
while(true)
{
UpdateImage();
}
}
public void UpdateImage()
{
if (this.imgVideo.Dispatcher.CheckAccess())
{
_image = new BitmapImage();
_image.BeginInit();
_image.CacheOption = BitmapCacheOption.None;
_image.UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
_image.CacheOption = BitmapCacheOption.OnLoad;
_image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
_image.UriSource = new Uri(this.picUri);
_image.EndInit();
this.imgVideo.Source = _image;
}
else
{
UpdateImageCallback del = new UpdateImageCallback(UpdateImage);
this.imgVideo.Dispatcher.Invoke(del);
}
}
Problem is, this is too slow and takes too long to refresh and the app just hangs.
I got this to work in Windows Forms with the PictureBox control but cannot get it to work in WPF. I refuse to believe that WPF is inferior to forms.
This app will always just hang (whether winforms or WPF) because you've got an infinite loop running everything it does on the UI thread. Your app hangs because you're not allowing the UI thread any time to process user input (such as resizing the window or trying to close the app).
With regard to your performance: have you tried profiling your code? I suspect that the problem is to do with you repeatedly hammering a webserver for an image, since you're never likely to get enough requests-per-second to make any kind of real-time video from static images. (There's a reason that we have video streaming codecs!)
instead of recreating whole image try to change only UriSource property.
Check out my answer to this: Showing processed images from an IP camera
Also, make sure the communication is done on a separate thread.
I suggest that the Bitmap image is a dependency object being created on a non-GUI thread. You then invoke UpdateImage on the GUI thread. Since the bitmap image dependency object wasn't created on/(owned by) the GUI thread, you get the "different thread owns it" error.
How about this as a workaround?
Copy the image temporarily to a local file location in your Watch routine.
Add a Thread.Sleep to the watch routine so that you don't hammer the CPU with the endless loop on this thread.
Use BeginInvoke instead of Invoke.
Load and update the image in the UpdateImage routine so that the image and the imgVideo objects are on the GUI thread. Update the image by reading it from your local file copy.
Without knowing the specifics of how you make Watch run on its own thread (using Background worker?) I think this approach will work for you.
I need to open all frames from Tiff image in WPF into memory and then delete the source. And after that I eventually need to render that image (resized according to window size). My solution is quite slow and I cannot delete file source before the first require. Any best practices?
Use CacheOption = BitmapCacheOption.OnLoad
This option can be used with the BitmapImage.CacheOption property or as an argument to BitmapDecoder.Create() If you want to access multiple frames once the images is loaded you'll have to use BitmapDecoder.Create. In either case the file will be loaded fully and closed.
See also my answer to this question
Update
The following code works perfectly for loading in all the frames of an image and deleting the file:
var decoder = BitmapDecoder.Create(new Uri(imageFileName), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
List<BitmapFrame> images = decoder.Frames.ToList();
File.Delete(imageFileName);
You can also access decoder.Frames after the file is deleted, of course.
This variant also works if you prefer to open the stream yourself:
List<BitmapFrame> images;
using(var stream = File.OpenRead(imageFileName))
{
var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
images = decoder.Frames.ToList();
}
File.Delete(imageFileName);
In either case it is more efficient than creating a MemoryStream because a MemoryStream keeps two copies of the data in memory at once: The decoded copy and the undecoded copy.
I figured it out. I have to use MemoryStream:
MemoryStream ms = new MemoryStream(File.ReadAllBytes(image));
TiffBitmapDecoder decoder = new TiffBitmapDecoder(ms, BitmapCreateOptions.None, BitmapCacheOption.None);
List<BitmapFrame> images = new List<BitmapFrame>();
foreach (BitmapFrame frame in decoder.Frames) images.Add(frame);