Out of memory exception while loading images - vb.net

I am using the following piece of code to load images as thumbnails to a FlowLayoutPanel control. Unfortunately i get an OutOfMemory exception.
As you already guess the memory leak is found at line
Pedit.Image = System.Drawing.Image.FromStream(fs)
So how could i optimize the following code?
Private Sub LoadImagesCommon(ByVal FlowPanel As FlowLayoutPanel, ByVal fi As FileInfo)
Pedit = New DevExpress.XtraEditors.PictureEdit
Pedit.Width = txtIconsWidth.EditValue
Pedit.Height = Pedit.Width / (4 / 3)
Dim fs As System.IO.FileStream
fs = New System.IO.FileStream(fi.FullName, IO.FileMode.Open, IO.FileAccess.Read)
Pedit.Image = System.Drawing.Image.FromStream(fs)
fs.Close()
fs.Dispose()
Pedit.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom
If FlowPanel Is flowR Then
AddHandler Pedit.MouseClick, AddressOf Pedit_MouseClick
AddHandler Pedit.MouseEnter, AddressOf Pedit_MouseEnter
AddHandler Pedit.MouseLeave, AddressOf Pedit_MouseLeave
End If
FlowPanel.Controls.Add(Pedit)
End Sub
Update: The problem occurs while loading a number of images (3264x2448px at 300dpi - each image is about 3Mb's)

Documentation for Image.FromFile (which is related to your FromStream) says that it will throw OutOfMemoryException if the file is not a valid image format or if GDI+ doesn't support the pixel format. Is it possible you're trying to load an unsupported image type?
Also, documentation for Image.FromStream says that you have to keep the stream open for the lifetime of the image, so even if your code loaded the image you'd probably get an error because you're closing the file while the image is still active. See http://msdn.microsoft.com/en-us/library/93z9ee4x.aspx.

Couple of thoughts:
First off, as Jim has stated, when using Image.FromStream the stream should remain open for the lifetime of the Image as remarked on the MSDN page. As such, I would suggest to copy the contents of the file to a MemoryStream, and use the latter to create the Image instance. So you can release the file handle asap.
Secondly, the images you're using are rather big (uncompressed, as they would exist in memory, Width x Height x BytesPerPixel). Assuming the context you use them in might allow for them to be smaller, consider resizing them, and potentially caching the resized versions somewhere for later use.
Lastly, don't forget to Dispose the image and the Stream when they are no longer needed.

You can solve this in a few steps:
to get free from the File-dependency, you have to copy the images. By really drawing it to a new Bitmap, you can't just copy it.
since you want thumbnails, and your source-bitmaps are rather large, combine this with shrinking the images.

I had the same problem. Jim Mischel answer led me to discover loading an innocent .txt file was the culprit. Here's my method in case anyone is interested.
Here's my method:
/// <summary>
/// Loads every image from the folder specified as param.
/// </summary>
/// <param name="pDirectory">Path to the directory from which you want to load images.
/// NOTE: this method will throws exceptions if the argument causes
/// <code>Directory.GetFiles(path)</code> to throw an exception.</param>
/// <returns>An ImageList, if no files are found, it'll be empty (not null).</returns>
public static ImageList InitImageListFromDirectory(string pDirectory)
{
ImageList imageList = new ImageList();
foreach (string f in System.IO.Directory.GetFiles(pDirectory))
{
try
{
Image img = Image.FromFile(f);
imageList.Images.Add(img);
}
catch
{
// Out of Memory Exceptions are thrown in Image.FromFile if you pass in a non-image file.
}
}
return imageList;
}

Related

Winrt StreamWriter & StorageFile does not completely Overwrite File

Quick search here yielded nothing. So, I have started using some rather roundabout ways to use StreamWriter in my WinRT Application. Reading works well, writing works differently. What' I'm seeing is that when I select my file to write, if I choose a new file then no problem. The file is created as I expect. If I choose to overwrite a file, then the file is overwritten to a point, but the point where the stream stops writing, if the original file was large, then the old contents exist past where my new stream writes.
The code is as such:
public async void WriteFile(StorageFile selectedFileToSave)
{
// At this point, selectedFileToSave is from the Save File picker so can be a enw or existing file
StreamWriter writeStream;
Encoding enc = new UTF8Encoding();
Stream dotNetStream;
dotNetStream = await selectedFileToSave.OpenStreamForWriteAsync();
StreamWriter writeStream = new StreamWriter(dotNetStream, enc);
// Do writing here
// Close
writeStream.Write(Environment.NewLine);
await writeStream.FlushAsync();
await dotNetStream.FlushAsync();
}
Can anyone offer clues on what I could be missing? There are lots of functions missing in WinRT, so not really following ways to get around this
Alternatively you can set length of the stream to 0 with SetLength method before using StreamWriter:
var stream = await file.OpenStreamForWriteAsync();
stream.SetLength(0);
using (var writer = new StreamWriter(stream))
{
writer.Write(text);
}
Why not just use the helper methods in FileIO class? You could call:
FileIO.WriteTextAsync(selectedFileToSave, newTextContents);
If you really need a StreamWriter, first truncate the file by calling
FileIO.WriteBytesAsync(selectedFileToSave, new byte[0]);
And then continue with your existing code.

Access Denied when deleting image file previously used in DataTemplate in WinRT

I have image (PNG) files used by my GridView as part of its DataTemplate. If I try to delete a specific object row in my GridView, I would delete the related image file for that row as well. The images are different for every item on the list.
I'm using this code to delete the image file
StorageFile _file = await DataStore.GetFileAsync(filename);
await _file.DeleteAsync(StorageDeleteOption.Default);
The image file is rendered on the GridView under the GridView's DataTemplate.
So in each object model in my List, I have a public property there that returns an ImageSource for my DataTemplate.
I'm calling my delete procedure right after i deleted the object row from the List and after the GridView has been refreshed of the new List items.
Even though the List does not contain the object (consuming the image) anymore, the app throws the Access is Denied exception if i try to delete the file. While the app is running, if i try to delete that particular file manually (through file explorer), it won't allow me too.
I tried clearing all unused objects in my app, even setting the GridView's ItemSource to null and the List to null before I delete the image. Still the exception persist.
Thanks in advance.
One method you can try is to load the image into a memory steam, then create a BitmapImage object from that stream, you can then set the source of your Image control to that bitmap image.
Since you are not using the actual image file as the source of the image, you can easily delete it anytime :)
Though this is an old question, I have encountered the problem recently in a UWP app and actually managed to find a solution. But first some background information about the problem:
When you create a BitmapImage with a URI, the created URI object holds a reference to the file in your local storage and keeps it open, i.e. non-writable. This btw is only true when the Bitmap is just big enough to fit into the Image entirely, typically small or medium sized Bitmaps. If the Bitmap happens to be big enough, WinRT automatically uses a downsampled version when it is displayed in an Image. In this case the URI does NOT hold a reference to the original file.
Now to the actual solution:
Setting Image.Source to null doesn't do the trick here, as the URI is still alive (until the next GC cycle at least). What DID work for me, was casting the Source to the BitmapImage it originally was and settings the UriSource to null.
var bitmapImage = image.Source as BitmapImage;
if (bitmapImage != null)
bitmapImage.UriSource = null;
And yes, this IS stupid.
Mark already mentioned part of this but there is no need to make it that complicated. Simply, wherever you expect that you need to delete the bitmap while the system is holding it, use a converter like this:
public class SafeImageFileConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
string path = (string)value;
var stream = new MemoryStream(File.ReadAllBytes(path));
return ImageSource.FromStream(() => stream);
}
}
In practice, you might want to check whether the path exists or return an error.png or similar if it doesn't. Also, don't be tempted to use using with the stream, the system will need the stream so you shouldn't dispose it early.
The trick is to use a Uri object to load the image (instead of a string filename) and then to use the EXACT same Uri instance to delete the file (after removing the image from the UI, of course). Here is an example:
//Save the Uri as a member variable so you can get to it later
private Uri uri;
//Create the Uri
uri = new Uri(OriginalImageFilename, UriKind.Absolute);
//Load the image
BitmapImage bitmapImage = new BitmapImage(uri);
//This can also be done by binding a Image control's source property to the uri.
//Delete the image (remember to use the same Uri instance)
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);
await file.DeleteAsync();
As a work-around, i just deleted the unused images during app launch so that no processes are using it. Thanks.

DowngradeDocument() function not doing proper downgrade?

I am trying to downgrade a document using VSTO.
I have a webservice, that receive a byte array. This byte is from the current active document.
The webservice can only handle a 2007/2003 word doc file.
So I want to use the
document.DowngradeDocument();
But the webservice report an error, when sending the byte array.
If a do a SaveAs and force word to save as 2007/2003 doc format, then there is no problem.
So my question is:
1) Why is DowngradeDocument() function not working. Why is it not doing a proper downgrade.
2) Do I need to do something else when I have called DowngradeDocument()
This must be in memory, since the file a happen to be working on, is not saved on disk.
// Dennis
Thank you for taking the time to read this
--- edit d. 20120904 ---
I cant use the webservice error, since it does not make sense of the error.
It says that it can finde the file, and this is an error within and application at the other side.
So I have tryed, to save a document in the right format, and one that was downgraded.
Used the same code. One work and the other did not.
But here is how I save the file as a temp. Before I call this function I have done a document.DowngradeDocument();
So I need, when it save to also change the format, while calling the downgrade function.
In the documentation for this function, it is clear that all previous version of office can read it, if the function is called.
/// <summary>
/// Make a copy of ActiveDocument in current user temp folder, and get a byte[].
/// </summary>
/// <param name="filename"></param>
/// <param name="document"></param>
/// <param name="file"></param>
/// <returns></returns>
private byte[] MakeCopy(string filename, Document document, out FileInfo file)
{
// http://blogs.msdn.com/b/pranavwagh/archive/2008/04/03/how-to-do-a-save-copy-as-in-word.aspx
// http://stackoverflow.com/questions/12175273/serialize-current-activedocument-from-office-2007-add-in
Microsoft.Office.Interop.Word.Application wdApp = new Microsoft.Office.Interop.Word.Application();
wdApp.Visible = false;
{
// make a fil i Current user temp folder
// http://stackoverflow.com/questions/944483/how-to-get-temporary-folder-for-current-user
string tempPath = System.IO.Path.GetTempPath();
string fileName = Path.Combine(tempPath, GenerateValidFileName(filename)) + ".doc";
IPersistFile compoundDocument = document as IPersistFile;
compoundDocument.Save(fileName, false);
byte[] content = File.ReadAllBytes(fileName);
file = new FileInfo(fileName);
wdApp.Quit();
return content;
}
}

Converting TIFF to PDF results in Insufficient Data For Image

I have a utility that converts batches of TIFF images to PDFs using the PDFSharp library. The following code performs the actual conversion. When I open the resulting PDF files in Acrobat Reader, I receive an error message for certain ones stating "Insufficient Data for Image." Others are fine.
What could be causing this? Is there anything I'm missing in the code that could prevent this?
Public Shared Function ConvertImageToPDF(ByVal img As Image) As Byte()
Using ms As New MemoryStream()
Using pdf As New PdfDocument()
Dim pageCount = GetPageCount(img)
For index = 0 To (pageCount - 1)
Dim page = New PdfPage()
Using sourceImage = GetPage(img, index)
Using pageImage = XImage.FromGdiPlusImage(sourceImage)
page.Width = pageImage.PointWidth
page.Height = pageImage.PointHeight
pdf.Pages.Add(page)
Using xgr = XGraphics.FromPdfPage(pdf.Pages(index))
xgr.DrawImage(pageImage, 0, 0)
End Using
End Using
End Using
Next
pdf.Save(ms, False)
pdf.Close()
End Using
Return ms.ToArray()
End Using
End Function
Public Shared Function GetPageCount(ByVal img As Image) As Integer
If (img Is Nothing) Then
Return -1
End If
Return img.GetFrameCount(FrameDimension.Page)
End Function
Public Shared Function GetPage(ByVal img As Image, ByVal pageNumber As Integer) As Image
img.SelectActiveFrame(FrameDimension.Page, pageNumber)
Dim ms = New MemoryStream()
img.Save(ms, ImageFormat.Tiff)
Return Image.FromStream(ms)
End Function
UPDATE:
If I run the same code over the same TIFF files, then the PDF files that were corrupt before are now OK, and ones that were OK before are now corrupt.
UPDATE 2:
After reviewing this connect issue (https://connect.microsoft.com/VisualStudio/feedback/details/584681/system-drawing-image-flags-has-different-value-in-vista-and-windows-7) and the community comment on this MSDN page (http://msdn.microsoft.com/en-us/library/system.drawing.image.save.aspx), it appears the issue is related to an operating system level bug in Windows 7. Can anyone confirm this or offer a workaround?
As stated in my update, after reviewing this connect issue (https://connect.microsoft.com/VisualStudio/feedback/details/584681/system-drawing-image-flags-has-different-value-in-vista-and-windows-7) and the community comment on this MSDN page (http://msdn.microsoft.com/en-us/library/system.drawing.image.save.aspx), it appears the issue is related to an operating system level bug in Windows 7.
This is supported by the comment from PDFsharpTeam.
In addition, when the images are read in Windows XP, the flags property on the image object is set to 77888. On Win7, it is set to 77840. After reviewing the MSDN documentation for the flags property (http://msdn.microsoft.com/en-us/library/system.drawing.image.flags.aspx), the difference is that WinXP flagged the image as a grayscale image (which mine are), but Win7 flagged it as an RGB image. This appears to be a symptom of the problem, but I don't know enough about image formats and color spaces to speak with authority on this.
UPDATE (2014-06-13):
After continuing to experience this problem, I researched a bit further and found a post on the PDFSharp forums mentioning this issue and linking to another post with a fix.
http://forum.pdfsharp.net/viewtopic.php?f=2&t=2729
http://forum.pdfsharp.net/viewtopic.php?p=5967#p5967
Basically, there are two methods in the PdfImage.FaxEncode.cs file that need to be updated.
In both the CountOneBits() and CountZeroBits() methods, replace the following code:
return found + hits;
with
found += hits;
if (found >= bitsLeft)
return bitsLeft;
return found;

Filetype check in VB.NET?

I have an image resized program and it works. The problem is when a user selects a non-image file in the file select dialog, it crashes. How can I check for image files?
UPDATE: 2022-04-05
Since it may not be feasible to validate the binary structure of every supported image, the fastest way to check if a file contains an image is to actually load it. If it loads successfully, then it is valid. If it doesn't then it is not.
The code below can be used to check if a file contains a valid image or not. This code is updated to prevent locking the file while the method is called. It also handles resource disposal after the tests (thanks for pointing out this issue user1931470).
Public Function IsValidImage(fileName As String) As Boolean
Dim img As Drawing.Image = Nothing
Dim isValid = False
Try
' Image.FromFile locks the file until the image is disposed.
' This might not be the wanted behaviour so it is preferable to
' open the file stream and read the image from it.
Using stream = New System.IO.FileStream(fileName, IO.FileMode.Open)
img = Drawing.Image.FromStream(stream)
isValid = True
End Using
Catch oome As OutOfMemoryException
' Image.FromStream throws an OutOfMemoryException
' if the file does not have a valid image format.
isValid = False
Finally
' clean up resources
If img IsNot Nothing Then img.Dispose()
End Try
Return isValid
End Function
ORIGINAL ANSWER
⚠️⚠️ WARNING ⚠️⚠️
This code has a bug that causes a high memory consumption when called several times in a program's lifetime.
DO NOT USE THIS CODE!!
Here's the VB.NET equivalent of 0xA3's answer since the OP insisted on a VB version.
Function IsValidImage(filename As String) As Boolean
Try
Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(filename)
Catch generatedExceptionName As OutOfMemoryException
' Image.FromFile throws an OutOfMemoryException
' if the file does not have a valid image format or
' GDI+ does not support the pixel format of the file.
'
Return False
End Try
Return True
End Function
You use it as follows:
If IsValidImage("c:\path\to\your\file.ext") Then
'do something
'
Else
'do something else
'
End If
Edit:
I don't recommend you check file extensions. Anyone can save a different file (text document for instance) with a .jpg extension and trick you app into beleiving it is an image.
The best way is to load the image using the function above or to open the first few bytes and check for the JPEG signature.
You can find more information about JPEG files and their headers here:
http://www.fastgraph.com/help/jpeg_header_format.html
http://en.wikipedia.org/wiki/JPEG
A very primitive check is to simply try to load the image. If it is not valid an OutOfMemoryException will be thrown:
static bool IsImageValid(string filename)
{
try
{
System.Drawing.Image img = System.Drawing.Image.FromFile(filename);
}
catch (OutOfMemoryException)
{
// Image.FromFile throws an OutOfMemoryException
// if the file does not have a valid image format or
// GDI+ does not support the pixel format of the file.
//
return false;
}
return true;
}
If I understood your question correctly your application it going to load the image anyway. Therefore simply wrapping the load operation in a try/catch block does not mean any additional overhead. For the VB.NET solution of this approach check the answer by #Alex Essilfie.
The ones wondering why Image.FromFile is throwing an OOM on invalid files should read the answer of Hans Passant to the following question:
Is there a reason Image.FromFile throws an OutOfMemoryException for an invalid image format?
Your first line of defense, of course, would be simply to check the file's extension:
Function IsImageFile(ByVal filename As String) As Boolean
Dim ext As String = Path.GetExtension(filename).ToLowerInvariant()
' This supposes your program can deal only with JPG files; '
' you could add other extensions here as necessary. '
Return ext = ".jpg" OrElse ext = ".jpeg"
End Function
Better yet, as SLC suggests in a comment, set your dialog's Filter property:
dialog.Filter = "Image files|*.jpg;*.jpeg"
This isn't a guarantee -- ideally you'd want to check the file itself to verify it's an image, and theoretically you should also be able to load files with anomalous extensions if they are in fact image files (maybe just ask for the user's acknowledgement first) -- but it's an easy start.
The VB and C# answers are great but also contain a "gotcha" if you plan to alter or move the file: the created 'img' object will lock the image file unless the dispose() method is invoked to release it. See below:
VB
Function IsValidImage(filename As String) As Boolean
Try
Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(filename)
img.dispose() ' Removes file-lock of IIS
Catch generatedExceptionName As OutOfMemoryException
' Image.FromFile throws an OutOfMemoryException
' if the file does not have a valid image format or
' GDI+ does not support the pixel format of the file.
'
Return False
End Try
Return True
End Function
C#
static bool IsImageValid(string filename)
{
try
{
System.Drawing.Image img = System.Drawing.Image.FromFile(filename);
img.dispose(); // Removes file-lock of IIS
}
catch (OutOfMemoryException)
{
// Image.FromFile throws an OutOfMemoryException
// if the file does not have a valid image format or
// GDI+ does not support the pixel format of the file.
//
return false;
}
return true;
}
The most robust way would be to understand the signatures of the files you need to load.
JPEG has a particular header format, for example.
This way your code won't be as easily fooled if you just look at the extension.
163's answer should get you most of the way along these lines.