Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8077349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:27:17+00:00 2026-06-05T15:27:17+00:00

I am working on a site where I need to be able to split

  • 0

I am working on a site where I need to be able to split and image around 4000×6000 into 4 parts (amongst many other tasks) and I need this to be as quick as possible for multiple users.

My current code for doing this is

var bitmaps = new RenderTargetBitmap[elements.Length];

using (var stream = blobService.Stream(key))
{
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.StreamSource = stream;
    bi.EndInit();

    for (var i = 0; i < elements.Length; i++)
    {
        var element = elements[i];

        TransformGroup transformGroup = new TransformGroup();
        TranslateTransform translateTransform = new TranslateTransform();
        translateTransform.X = -element.Left;
        translateTransform.Y = -element.Top;
        transformGroup.Children.Add(translateTransform);

        DrawingVisual vis = new DrawingVisual();
        DrawingContext cont = vis.RenderOpen();
        cont.PushTransform(transformGroup);
        cont.DrawImage(bi, new Rect(new Size(bi.PixelWidth, bi.PixelHeight)));
        cont.Close();

        RenderTargetBitmap rtb = new RenderTargetBitmap(element.Width, element.Height, 96d, 96d, PixelFormats.Default);
        rtb.Render(vis);
        bitmaps[i] = rtb;
    }
}

for (var i = 0; i < bitmaps.Length; i++)
{
    using (MemoryStream ms = new MemoryStream())
    {
        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bitmaps[i]));
        encoder.Save(ms);
        var regionKey = WebPath.Variant(key, elements[i].Id);
        saveBlobService.Save("image/png", regionKey, ms);
    }
}

I am running multiple threads which take jobs off a queue. I am finding that if this part of code is hit by 4 threads at once I get an OutOfMemory exception. I can stop this happening by wrapping all the code above in a lock(obj) but this isn’t ideal. I have tried wrapping just the first using block (where the file is read from disk and split) but I still get the out of memory exceptions (this part of the code executes quite quickly).

  • I this normal considering the amount of memory this should be taking up?
  • Are there any optimisations I could make?
  • Can I increase the memory available?

UPDATE:

My new code as per Moozhe’s help

public static void GenerateRegions(this IBlobService blobService, string key, Element[] elements)
{
    using (var stream = blobService.Stream(key))
    {
        foreach (var element in elements)
        {
            stream.Position = 0;
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.SourceRect = new Int32Rect(element.Left, element.Top, element.Width, element.Height);
            bi.StreamSource = stream;
            bi.EndInit();

            DrawingVisual vis = new DrawingVisual();
            DrawingContext cont = vis.RenderOpen();
            cont.DrawImage(bi, new Rect(new Size(element.Width, element.Height)));
            cont.Close();

            RenderTargetBitmap rtb = new RenderTargetBitmap(element.Width, element.Height, 96d, 96d, PixelFormats.Default);
            rtb.Render(vis);

            using (MemoryStream ms = new MemoryStream())
            {
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(rtb));
                encoder.Save(ms);
                var regionKey = WebPath.Variant(key, element.Id);
                blobService.Save("image/png", regionKey, ms);
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:27:18+00:00Added an answer on June 5, 2026 at 3:27 pm

    If you’re trying to call DrawImage of a 4000×6000 image in parallel, you’re going to have a bad time. You’re cropping it too late, by the time you’re rendering it to the RenderTargetBitmap it’s already been rendered full size in memory.

    Instead of cropping the image source with a transform, try to use the BitmapImage.SourceRect property like so:

    BitmapImage.SourceRect = new Rect(element.Left, element.Top, element.Width, element.Height);

    You may want to try putting that before you call BeginInit(), and get rid of the transform completely.

    EDIT: Actually in your case you’d have to change the SourceRect in each iteration of the for loop. And remember that you have to change the Size parameter in DrawImage to be the element size.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a site, colorurl.com, and I need users to be able to
I am working on a site where I will need to be able to
I'm working on a social networking site and need users to be able to
I am working on a site in that site I need to customize the
I'm currently working on a auction site where I need to display a countdown
I am working on one joomla(2.5.1) site and i need one module for one
I need to implement facebook connect in Pyrocms based site.The user module is working
I am working on smartphone site. Is there a way to set Bookmark this
I need to be able to download some file from a regular site using
For a website I'm working on, I need to be able to deliver it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.