C
C#•5mo 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•5mo ago
How big is the image?
Tim
TimOP•5mo ago
11KB it looks like
Kouhai
Kouhai•5mo ago
And it's height and width?
Tim
TimOP•5mo ago
612 x 369
Kouhai
Kouhai•5mo 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•5mo ago
Not really sure what any of this means so just ss'ed lol
Kouhai
Kouhai•5mo ago
Actually, it seems like Image.FromFile throws Out Of Memory for if the file's format is invalid
Tim
TimOP•5mo ago
Ohh
Kouhai
Kouhai•5mo ago
No description
Tim
TimOP•5mo ago
Why would it be invalid? Its a jpg
Kouhai
Kouhai•5mo 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•5mo 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•5mo ago
Probably 😅
Tim
TimOP•5mo ago
😭 is there any better way to do this lol
Kouhai
Kouhai•5mo ago
If you have ImageMagick installed, sure you can write a script to do it automatically
Tim
TimOP•5mo 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•5mo ago
Oh, maybe you could try loading the image using ImageSharp
Tim
TimOP•5mo ago
Gothca thanks for the help
Kouhai
Kouhai•5mo ago
Load the images using ImageSharp then use Image.FromStream to load the images from ImageSharp's streams
Tim
TimOP•5mo ago
But woudl i be able to get it into a bitmap datatype like I do for this one
Kouhai
Kouhai•5mo ago
Yeah
Tim
TimOP•5mo ago
Ohh Is it sixlabors.imagesharp?
Kouhai
Kouhai•5mo ago
Yeah And loading an image just as simple as Image.Load, which will return an Image instance
Tim
TimOP•5mo ago
do you happen to know the method for imagesharp to load the images
Kouhai
Kouhai•5mo ago
using var image = Image.Load("IMAGE_PATH_GOES_HERE");
Tim
TimOP•5mo 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•5mo 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•5mo ago
is jpgfromat a package? its not recogfnzied
Kouhai
Kouhai•5mo ago
Should be provided by ImageSharp
Tim
TimOP•5mo ago
hmm doesnt seem to work when i add that to it ah its jpeg lol
Kouhai
Kouhai•5mo ago
lmao my bad :kekYep:
Tim
TimOP•5mo ago
woohoo that all seemed to work!
Kouhai
Kouhai•5mo ago
:HYPERS:
Tim
TimOP•5mo ago
tyyy
Kouhai
Kouhai•5mo ago
Please make sure you have using before var image Just to make sure the image instance is disposed
Tim
TimOP•5mo ago
how does using worked in that context?
Kouhai
Kouhai•5mo 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•5mo ago
ahh ok awesome tysm !
Want results from more Discord servers?
Add your server