Memory issues when copying JPEG into bmp I am trying as a part of a bigger job to merge parts of several JPEGs into one huge BMP which will be processed further. 

As a start, I tried to just copy a few JPEGs each to his own BMP file. In doing so I noticed that Visual Studio 2015 Community shows increased memory consumption if I repeat the copying of files in a single debug-run. The responsible part of the code is as follows. 

Please, can you help me with this increasing memory consumption? In the future, I might need to read a few thousand JPEGs so this might be a serious issue.

private void GluePhotos(string[] files) {
    foreach (string file in files) {
        //Read original JPEG
        Stream imageSource = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
        JpegBitmapDecoder jpegDecoder = new JpegBitmapDecoder(imageSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
        BitmapSource bitmapSource = jpegDecoder.Frames[0];

        //Prepare memory
        int stride = bitmapSource.PixelWidth * ((bitmapSource.Format.BitsPerPixel + 7) / 8);
        byte[] pixels = new byte[bitmapSource.PixelHeight * stride]; 

        //Copy JPEG into memory
        bitmapSource.CopyPixels(pixels, stride, 0);

        //Copy memory into a new BMP-to-be
        WriteableBitmap bitmap = new WriteableBitmap(bitmapSource.PixelWidth, bitmapSource.PixelHeight, bitmapSource.DpiX, bitmapSource.DpiY, bitmapSource.Format, bitmapSource.Palette);
        bitmap.WritePixels(new System.Windows.Int32Rect(0, 0, bitmapSource.PixelWidth, bitmapSource.PixelHeight), pixels, stride, 0);

        //Encode it as a BMP
        BmpBitmapEncoder bmpEncoder = new BmpBitmapEncoder();
        BitmapFrame newBitmapFrame = BitmapFrame.Create(bitmap);
        bmpEncoder.Frames.Add(newBitmapFrame);

        //Prepare paths for saving, save and cleaning(?)
        string dirPath = file.Insert(file.LastIndexOf('\\'), "\\ripoffs");
        dirPath = dirPath.Remove(dirPath.LastIndexOf('\\'));
        Directory.CreateDirectory(dirPath);
        string fileName = file.Insert(file.LastIndexOf('\\'), "\\ripoffs");
        fileName = fileName.Remove(fileName.LastIndexOf('.')) + "_ripoff.bmp";
        Stream imageOutput = new FileStream(fileName, FileMode.Create);
        bmpEncoder.Save(imageOutput);
        imageOutput.Close();
        imageSource.Close();
    }
}
If you need comments to document the flow of your method, then you're doing it wrong. Obviously this needs to be split into much smaller methods, each dedicated to their own logic. 

That was you could move the three lines below //Read original JPEG to a method of their own that would return a BitmapSource, and you could encapsulate imageSource in a using block (considering it's an IDisposable).

Same for imageOutput: another stream you're not properly disposing of.

Quite frankly, I'd move everything inside the foreach into a class of its own and start refactoring from there.



I had to look up what this does:

string dirPath = file.Insert(file.LastIndexOf('\\'), "\\ripoffs");


I can't help but feel that this is a hacky way of something that should be done with for instance FileInfo and Path etc;

Consider this (I'm assuming file is a global variable of the class that contains all of the code in the foreach):

    private string GetResultPath()
    {
        var fileInfo = new FileInfo(file);

        var directoryPath = fileInfo.DirectoryName;
        var newDirectoryPath = Path.Combine(directoryPath, "ripoffs");

        Directory.CreateDirectory(newDirectoryPath);

        var fileName = fileInfo.Name.Replace(fileInfo.Extension, string.Empty)
                                   + "_ripoff"
                                   + fileInfo.Extension;

        return Path.Combine(newDirectoryPath, fileName);
    }


Yes, that's longer, but it's also much clearer and informs the next developer who looks at that code what it does.