-4

in my c# application load form i make load like 30 picture and this make my memory full and if I add more pictures I'll get the message "that my memory is full"

  1. how can I free my memory or increase the maximum used memory?
  2. is there are any another way to get all my pictures to picture box without filling my memory?

https://i.stack.imgur.com/bFVjy.png

Thomas Landauer
  • 7,857
  • 10
  • 47
  • 99
  • Please provide more information: code (as text *in the question*: images are too hard to read), details of the error (in the question), how big are the images, and (critical): what kind of application is this? (WPF, WebForms, ...? Is this .NET Core?) – Richard Oct 25 '18 at 09:59
  • c# form and this is how i load my pics in load form ,this is one exemple : Bitmap bmpQ10 = new Bitmap("C:\\Users\\hp\\Desktop\\Arconic_Final\\ArconicFinal\\DMS\\acceuilTEAM1.jpg"); pictureBoxTeam1.Image = bmpQ10; ,, i do that for 30 picturebox – Ayoub Khalfane Oct 25 '18 at 10:04
  • Please *edit the question for additional information* (code in comments, especially when not formatted as such, is too hard to read). – Richard Oct 25 '18 at 10:07

1 Answers1

2

1) how can I free my memory or increase the maximum used memory?

Are you receiving an OutOfMemoryException and your memory isn't filled up in your machine? This might be due to x86 compilation and you should change it to x64.

2) is there are any another way to get all my pictures to picture box without filling my memory?

Yes and no. If you are using Bitmap it will allocate the image in the memory. If you are recreating the image later with all the images together without compressing it, then you could be also losing some memory if you are not disposing the Bitmaps correctly. You could load them, compress them, and then dispose the original one, keeping the compressed/optimized one in memory.

Besides, remember to always dispose your Bitmap after using it. The GC usually takes care of them, but you should always dispose them:

using (Bitmap bitmap = new Bitmap("file.jpg")
{
     // bitmap handling here
}

Of course, if you are displaying it in a picturebox you can't dispose it, but again, you are not being completely clear in your question.

Without your code I cannot provide you a better answer, and I'll gladly edit this one if you update your question.

Read:

Compress bitmap before sending over network

When do I need to use dispose() on graphics?

Update

Upon fixing your thread I've noticed you've given an image but the formatting was broken.

Read my answer and it should help you (specially the part of compression). You can also change to x64.

Update 2 - Compression code example

    public void ExampleMethod()
    {
        pictureBox1.Image = GetCompressedFile("file.jpg", quality: 10);
    }

    private Image GetCompressedFile(string fileName, long quality)
    {
        using (Bitmap bitmap = new Bitmap(fileName))
        {
            return GetCompressedBitmap(bitmap, quality);
        }
    }

    private Image GetCompressedBitmap(Bitmap bmp, long quality)
    {
        using (var mss = new MemoryStream())
        {
            EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
            ImageCodecInfo imageCodec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(o => o.FormatID == ImageFormat.Jpeg.Guid);
            EncoderParameters parameters = new EncoderParameters(1);
            parameters.Param[0] = qualityParam;
            bmp.Save(mss, imageCodec, parameters);
            return Image.FromStream(mss);
        }
    }

In the quality parameter you set the quality. 100 means 100%, 50 means 50%.

Try setting quality as 10 and see if it works.

Remove all Bitmap bmp = new Bitmap from the code, and display with your PictureBoxInstance.Image = GetCompressedFile(...);

Check the ExampleMethod().

Code based from: https://stackoverflow.com/a/48274706/4352946

You have to remember though, that even using Dispose, the memory won’t be freed right away, you should wait the GC for that.

P.S: compressing the file on-the-go you MIGHT end up with both images (the compressed and uncompressed) in the memory

Lucca Ferri
  • 1,308
  • 12
  • 23
  • thank you so much , i know that my question is not clear at all , first is because my english language second because im know here i really dont know how to use the web site correctly – Ayoub Khalfane Oct 25 '18 at 10:31
  • It's fine. Please read my answer and report back. It should work. I've fixed your question with the image that wasn't appearing. Please read: https://stackoverflow.com/conduct and https://stackoverflow.com/help/how-to-ask – Lucca Ferri Oct 25 '18 at 10:33
  • Because you answear my first question i can load more picture and use more memory in my application but is 72% and scared that make my application to heavy , how after load all my pictures to picturebox i want make my memory empty or how i can make my bitmap not heavy – Ayoub Khalfane Oct 25 '18 at 10:47
  • Your answear for my first question was good and this make more memory to my application and dont stop anymore , i did also use your methode for make quality less but this dont make my memory used less is the same – Ayoub Khalfane Oct 26 '18 at 08:49
  • The memory usage will still use a lot on system’s memory. The idea of compression would be for a degrade on the size of the image. Have you checked if it’s using less with a process monitor? – Lucca Ferri Oct 26 '18 at 08:52
  • How i can do that ? – Ayoub Khalfane Oct 26 '18 at 22:15