C
C#•3mo ago
Tim

Getting Out of Memory Exception when loading in images

I get a system out of memory error after calling Image.FromFile in GetTarget(). How should I fix this?
public ImageManager()
{
_tiles = new List<Bitmap>();
GetTarget();
LoadAll();
}

private void GetTarget()
{
_target = (Bitmap)Image.FromFile(@"C:\Users\timo2\OneDrive\Desktop\The Grind\Personal Projects\PhotoMosaic\PhotoMosaicGenerator\downloaded_images\image_20.jpg");
}
public ImageManager()
{
_tiles = new List<Bitmap>();
GetTarget();
LoadAll();
}

private void GetTarget()
{
_target = (Bitmap)Image.FromFile(@"C:\Users\timo2\OneDrive\Desktop\The Grind\Personal Projects\PhotoMosaic\PhotoMosaicGenerator\downloaded_images\image_20.jpg");
}
40 Replies
Kouhai
Kouhai•3mo ago
How big is the image?
Tim
TimOP•3mo ago
11KB it looks like
Kouhai
Kouhai•3mo ago
And it's height and width?
Tim
TimOP•3mo ago
612 x 369
Kouhai
Kouhai•3mo ago
Hmmm, that shouldn't cause any memory problems How much memory is the application using in diagnostic tools? (assuming you're using visual studio)
Tim
TimOP•3mo ago
Not really sure what any of this means so just ss'ed lol
Kouhai
Kouhai•3mo ago
Actually, it seems like Image.FromFile throws Out Of Memory for if the file's format is invalid
Tim
TimOP•3mo ago
Ohh
Kouhai
Kouhai•3mo ago
No description
Tim
TimOP•3mo ago
Why would it be invalid? Its a jpg
Kouhai
Kouhai•3mo ago
Then it's most likely a problem with the pixel format, can you try saving the image again with paint for example?
Tim
TimOP•3mo ago
It looks like that worked .. ? Agh I have like 20 photos i wanted to load in... I have to do this for all of them now?
Kouhai
Kouhai•3mo ago
Probably 😅
Tim
TimOP•3mo ago
😭 is there any better way to do this lol
Kouhai
Kouhai•3mo ago
If you have ImageMagick installed, sure you can write a script to do it automatically
Tim
TimOP•3mo ago
nah thats fine, i just meant would there be a way to use the existing images still like a diff method or smth
Kouhai
Kouhai•3mo ago
Oh, maybe you could try loading the image using ImageSharp
Tim
TimOP•3mo ago
Gothca thanks for the help
Kouhai
Kouhai•3mo ago
Load the images using ImageSharp then use Image.FromStream to load the images from ImageSharp's streams
Tim
TimOP•3mo ago
But woudl i be able to get it into a bitmap datatype like I do for this one
Kouhai
Kouhai•3mo ago
Yeah
Tim
TimOP•3mo ago
Ohh Is it sixlabors.imagesharp?
Kouhai
Kouhai•3mo ago
Yeah And loading an image just as simple as Image.Load, which will return an Image instance
Tim
TimOP•3mo ago
do you happen to know the method for imagesharp to load the images
Kouhai
Kouhai•3mo ago
using var image = Image.Load("IMAGE_PATH_GOES_HERE");
Tim
TimOP•3mo ago
private void GetTarget()
{
var image = SixLabors.ImageSharp.Image.Load(@"C:\Users\timo2\OneDrive\Desktop\The Grind\Personal Projects\PhotoMosaic\PhotoMosaicGenerator\downloaded_images\image_17.jpg");

_target = (Bitmap)System.Drawing.Image.FromStream(image);
}
private void GetTarget()
{
var image = SixLabors.ImageSharp.Image.Load(@"C:\Users\timo2\OneDrive\Desktop\The Grind\Personal Projects\PhotoMosaic\PhotoMosaicGenerator\downloaded_images\image_17.jpg");

_target = (Bitmap)System.Drawing.Image.FromStream(image);
}
this isnt right but its what i have so far what am i missing?
Kouhai
Kouhai•3mo ago
You need to save the ImageSharp's Image to a temporary stream So something like
var mem = new MemoryStream();
image.Save(mem, JpgFormat.Instance);
_target = (Bitmap)System.Drawing.Image.FromStream(mem);
var mem = new MemoryStream();
image.Save(mem, JpgFormat.Instance);
_target = (Bitmap)System.Drawing.Image.FromStream(mem);
Tim
TimOP•3mo ago
is jpgfromat a package? its not recogfnzied
Kouhai
Kouhai•3mo ago
Should be provided by ImageSharp
Tim
TimOP•3mo ago
hmm doesnt seem to work when i add that to it ah its jpeg lol
Kouhai
Kouhai•3mo ago
lmao my bad :kekYep:
Tim
TimOP•3mo ago
woohoo that all seemed to work!
Kouhai
Kouhai•3mo ago
:HYPERS:
Tim
TimOP•3mo ago
tyyy
Kouhai
Kouhai•3mo ago
Please make sure you have using before var image Just to make sure the image instance is disposed
Tim
TimOP•3mo ago
how does using worked in that context?
Kouhai
Kouhai•3mo ago
It would look something like this using var image = SixLabors.ImageSharp.Image.Load(....); using essentially is just syntactic sugar that ensures Dispose is called on the image instance, that way you won't leak resources
Tim
TimOP•3mo ago
ahh ok awesome tysm !
Want results from more Discord servers?
Add your server