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 7842739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:23:29+00:00 2026-06-02T16:23:29+00:00

I am making a paint project like photoshop using c#. I have used GDI+

  • 0

I am making a paint project like photoshop using c#.
I have used GDI+ for the drawing. Sadly I cannot post the screen shot cuz of the reputation points required.
EDIT : Ok i got enough rep to upload a pic.
enter image description here

My drawing using the mouse lags when the brush size increases.
I have a canvasBuffer which is drawn to the canvas panel

protected override void OnPaint(PaintEventArgs e)
    {
        if (canvasBuffer != null)
        {
            using (Graphics g = e.Graphics)
            {
                g.DrawImage(canvasBuffer, e.ClipRectangle, e.ClipRectangle, GraphicsUnit.Pixel);
            }
        }
    }

And this is what happens when any painting is done.

  1. I store the list of points of the mouse drag event.
  2. I draw a series of circle from point a to point b with the recorded point as its center to draw a smooth line
  3. This drawing is done on the bitmap stored in the list of strokes in the layer class.
    This drawing is also done with CompositingMode.SourceCopy to implement alpha value drawings
  4. I have a layerBuffer that stores the final image of the layer. I draw the changes affected by the stroke to this layerBuffer by clearing it with a drawing of a transparent color using SourceCopy Compatibility mode and then draw the bitmaps in the list of strokes using SourceOver
  5. Due to the layering system I am implementing, I draw all the layer buffer to a pictureBuffer.
    This picture Buffer is finally drawn to the canvasBuffer with scaling transformations.

Note : The affected area of the canvas Buffer is done in the same manner as the layer Buffer i.e by clearing the affected part and redrawing the entire affected part of the picture buffer back again.
If I do not clear the previous drawing, drawing with alpha value does not work as expected.

Please help me to optimize this code or suggest some new ways to improve the performance and reduce the lag while drawing using the mouse.
Also, would separating the drawing code and the calculation of the points and drawing of the buffers using threads help ?

Here is the code.

public void PSF_Painted(PSF_PaintEvent e)
    {
        Layer SelectedLayer = psf.Layers[0];//Get selected layer here
        if ((BrushTool)getActiveTool() != null)
        {
            //getting the pen attributes from the brush tool
            BrushTool brushTool = (BrushTool)getActiveTool();
            Pen pen = brushTool.getPen();
            Brush brush = pen.Brush;
            int brushSize = (int)pen.Width;
            //loading points data
            List<Point> recordedPoints = null;
            Point currentPoint = new Point(0, 0);
            Point previousPoint = new Point(0, 0);
            if (e.RecordedPoints != null)
            {
                recordedPoints = e.RecordedPoints;
                if (recordedPoints.Count > 1)
                {
                    currentPoint = recordedPoints[recordedPoints.Count - 1];
                    previousPoint = recordedPoints[recordedPoints.Count - 2];
                }
                else if (recordedPoints.Count == 1)
                {
                    currentPoint = recordedPoints[0];
                    previousPoint = currentPoint;
                }
            }
            if (e.PaintEventType == PSF_PaintEvent.StrokeStarted)
            {
                //Console.WriteLine("StrokeStarted");
                SelectedLayer.Strokes.Add(new Bitmap(SelectedLayer.Width, SelectedLayer.Height));
            }
            else if (e.PaintEventType == PSF_PaintEvent.Painting)
            {
                //Draw the drawing in the bitmap of the layer's stroke data
                using (Graphics g = Graphics.FromImage(SelectedLayer.Strokes[SelectedLayer.Strokes.Count - 1]))
                {
                    List<Point> points = Global.GetPointsOnLine(previousPoint.X, previousPoint.Y, currentPoint.X, currentPoint.Y);
                    for (int i = 0; i < points.Count; i++)
                    {
                        g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                        if (pen.Width == 1)
                        {
                            g.FillRectangle(brush, new Rectangle(points[i].X, points[i].Y, brushSize, brushSize));
                        }
                        else
                        {
                            g.FillEllipse(brush, new Rectangle(points[i].X, points[i].Y, brushSize, brushSize));
                        }
                        int xt, xb, yt, yb;
                        xt = points[i].X;
                        xb = points[i].X + brushSize;
                        yt = points[i].Y;
                        yb = points[i].Y + brushSize;

                        if (xt < 0) xt = 0;
                        if (xb > psf.Width) xb = SelectedLayer.Width;
                        if (yt < 0) yt = 0;
                        if (yb > psf.Height) yb = SelectedLayer.Height;

                        //Refresh changes to the affected part of the canvas buffer
                        Rectangle affectedRect = new Rectangle(xt, yt, xb - xt, yb - yt);
                        float zf = psf.ZoomFactor;
                        Rectangle canvasAffectedRect = new Rectangle((int)(affectedRect.X * zf), (int)(affectedRect.Y * zf), (int)(affectedRect.Size.Width * zf), (int)(affectedRect.Size.Height * zf));
                        SelectedLayer.invalidateLayerBuffer(affectedRect);
                        invalidateCanvasBuffer(canvasAffectedRect);
                    }
                }
            }
            else if (e.PaintEventType == PSF_PaintEvent.StrokeCompleted)
            {
                //Console.WriteLine("StrokeCompleted");
            }
        }
        this.Invalidate();
    }
  • 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-02T16:23:30+00:00Added an answer on June 2, 2026 at 4:23 pm

    Ok, I found a solution for this optimization.
    I only recorded the points which were at a distance > 1/5th the size of the brush.
    I realized that I did not need a very perfect line. Therefore the compromise in the quality of the line for performance can be traded.

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

Sidebar

Related Questions

I'm making a Scorched Earth like game in Java (for my exam project :D),
Within my project I'm using the camera function of the iPhone. I'd like to
I'm having trouble making python print out texts properly aligned. I have tried everything
I'm making a painting app, and I currently have the app at a point
Making a word document of our network set-up. We have about 7 servers and
I am making a small game as sort of a test project, nothing major.
What do you guys do, when you have huge project built with ant for
I've been learning C# over the summer and now feel like making a small
I am creating a test project using IBM's Rational Functional Tester(RFT) tool(we are using
I'm using CML to manage the 3D math in an OpenGL-based interface project I'm

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.