How to speed up these graphics functions?

Viewed 107

Here are a couple of methods I'm using in my application

This 'highlights' an area selected (called on a MouseMove event, so it's used very often to give a live view of what you've selected).

Ignore the scaling variable, this is so that if the image has been resized to fit the control the selected area is reflected properly

private Bitmap DrawCropArea(Bitmap bm, Rectangle area)
{
    int x = (int)Math.Round(area.X / scaling);
    int y = (int)Math.Round(area.Y / scaling);
    int cropWidth = (int)Math.Round(area.Width / scaling);
    int cropHeight = (int)Math.Round(area.Height / scaling);

    using (Graphics gr = Graphics.FromImage(bm))
    {
        Brush brush = new Pen(Color.FromArgb(200, Color.DarkGray)).Brush;

        Rectangle rect1 = new Rectangle(0, 0, bm.Width, y);
        Rectangle rect2 = new Rectangle(0, y, x, cropHeight);
        Rectangle rect3 = new Rectangle(0, (y + cropHeight), bm.Width, bm.Height);
        Rectangle rect4 = new Rectangle((x + cropWidth), y, (bm.Width - x - cropWidth), cropHeight);

        gr.FillRectangle(brush, rect1);
        gr.FillRectangle(brush, rect2);
        gr.FillRectangle(brush, rect3);
        gr.FillRectangle(brush, rect4);
    }

    return bm;
}

And this sets the brightness and contrast, and like the previous method it's called very often as it's on 2 trackbar EditValueChanging events

private Bitmap ChangeBrightnessContrast(Bitmap bm, float cont, int bright)
{
    float[][] arr =
    {
        new float[] { 1, 0, 0, 0, 0 },
        new float[] { 0, 1, 0, 0, 0 },
        new float[] { 0, 0, 1, 0, 0 },
        new float[] { 0, 0, 0, 1, 0 },
        new float[] { 0, 0, 0, 0, 1 },
    };

    ColorMatrix cMatrix = new ColorMatrix(arr);
    cMatrix.Matrix00 = cMatrix.Matrix11 = cMatrix.Matrix22 = cont;
    cMatrix.Matrix40 = cMatrix.Matrix41 = cMatrix.Matrix42 = bright / 255.0F;

    ImageAttributes imgAttributes = new ImageAttributes();
    imgAttributes.ClearColorMatrix();
    imgAttributes.SetColorMatrix(cMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
    //imgAttributes.SetGamma(1.0F, ColorAdjustType.Bitmap);

    using (Graphics gr = Graphics.FromImage(bm))
    {
        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
        gr.DrawImage(bm, new Rectangle(0, 0, bm.Width, bm.Height), 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel, imgAttributes);
    }

    return bm;
}

It's fine until I am using these on some larger images, where it starts to really lag.

I've been reading a lot of mixed information; GDI+ does support hardware acceleration, no it doesn't you should use Direct2D etc.. So I'm not sure if there's something I can change with the current Drawing libraries I'm using, if I should switch to something else, or if I should do a lot of legwork and turn the image been worked on into a lower resolution thumbnail, then re-apply all the adjustments to the fullsize image after the user is happy?

0 Answers
Related