Skip to content

Solution to: Exception 'Out of Memory' while drawing a big imageΒ #37

@carlshe

Description

@carlshe

While load a big image, for example 15000*8000 pixels, DrawImage almost always throw an exception 'Out of Memeory'.

I tested a solution, which works very well. Its key point is to divide image into several small parts and then draw each part one by one.

The code as follow:

            int CountOfStepX = (int)Math.Round(srcPortion.Width / MaxDrawImageSidePixels + 0.5);
            int CountOfStepY = (int)Math.Round(srcPortion.Height / MaxDrawImageSidePixels + 0.5);

            float stepDstX = (int)(dstView.Width / CountOfStepX);
            float stepDstY = (int)(dstView.Height / CountOfStepY);
            float stepSrcX = (int)(srcPortion.Width / CountOfStepX);
            float stepSrcY = (int)(srcPortion.Height / CountOfStepY);
            for(int w=0;w< CountOfStepX;w++)
                for(int h=0; h<CountOfStepY;h++)
                {
                    rfDst = new RectangleF(dstView.X+ stepDstX*w, dstView.Y + stepDstY * h, stepDstX, stepDstY);
                    rfSrc=  new RectangleF(srcPortion.X+ stepSrcX *w, srcPortion.Y + stepSrcY * h, stepSrcX, stepSrcY);
                    Rectangle txtRect = new Rectangle((int)rfDst.X, (int)rfDst.Y, (int)rfDst.Width, (int)rfDst.Height);
                    g.DrawImage(this.Image, rfDst, rfSrc, GraphicsUnit.Pixel);
                }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions