This is an older post, from another [now dead] blog of mine, but I thought I’d throw it up here as well.
When working in C#, if you are trying to load a jpeg file and you use the System.Drawing.Image.FromFile method on a file that you do not have the proper permissions for, you will get an “Out of Memory” exception. Somewhat misleading – but that is the error resulting from improper permissions.
I found a web site that claimed that you create a FileStream object from your file and then use the FromStream method on the Image object.
My solution for loading a file was as follows
[cc lang="csharp"]
private void addPhotoButton_Click(object sender, EventArgs e)
{
DialogResult newPhoto;
Image newPhotoImage;
string newPhotoFileName;
newPhoto = openFileDialogAddPhoto.ShowDialog();
if(newPhoto == DialogResult.OK)
{
newPhotoFileName = openFileDialogAddPhoto.FileName;
FileStream photoStream =
new FileStream(newPhotoFileName,
FileMode.Open, FileAccess.Read);
newPhotoImage = Image.FromStream(photoStream);
}
}
[/cc]

