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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:57:01+00:00 2026-06-18T06:57:01+00:00

The issue of programmatically drawing lines using XNA has been covered here . However,

  • 0

The issue of programmatically drawing lines using XNA has been covered here. However, I want to allow a user to draw on a canvas as one would with a drawing app such as MS Paint.

This of course requires each x and/or y coordinate change in the mouse pointer position to result in another “dot” of the line being drawn on the canvas in the crayon color in real time.

In the mouse move event, what XNA API considerations come into play in order to draw the line point by point? Literally, of course, I’m not drawing a line as such, but rather a sequence of “dots”. Each “dot” can, and probably should, be larger than a single pixel. Think of drawing with a felt tip pen.

  • 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-18T06:57:02+00:00Added an answer on June 18, 2026 at 6:57 am

    The article you provided suggests a method of drawing lines with primitives; vector graphics, in other words. Applications like Paint are mostly pixel based (even though more advanced software like Photoshop has vector and rasterization features).

    Bitmap editor

    Since you want it to be “Paint-like” I would definitely go with the pixel based approach:

    1. Create a grid of color values. (Extend the System.Drawing.Bitmap class or implement your own.)
    2. Start the (game) loop:
      • Process input and update the color values in the grid accordingly.
      • Convert the Bitmap to a Texture2D.
      • Use a sprite batch or custom renderer to draw the texture to the screen.
    3. Save the bitmap, if you want.

    Drawing on the bitmap

    I added a rough draft of the image class I am using here at the bottom of the answer. But the code should be quite self-explanatory anyways.

    As mentioned before you also need to implement a method for converting the image to a Texture2D and draw it to the screen.


    First we create a new 10×10 image and set all pixels to white.

    var image = new Grid<Color>(10, 10);
    image.Initilaize(() => Color.White);
    

    White 10*10 pixel grid

    Next we set up a brush. A brush is in essence just a function that is applied on the whole image. In this case the function should set all pixels inside the specified circle to a dark red color.

    // Create a circular brush
    float brushRadius = 2.5f;
    int brushX = 4;
    int brushY = 4;
    Color brushColor = new Color(0.5f, 0, 0, 1); // dark red
    

    White 10*10 color grid with a circle and cross defining the brush area and center

    Now we apply the brush. See this SO answer of mine on how to identify the pixels inside a circle.
    You can use mouse input for the brush offsets and enable the user to actually draw on the bitmap.

    double radiusSquared = brushRadius * brushRadius;
    
    image.Modify((x, y, oldColor) =>
    {
        // Use the circle equation
        int deltaX = x - brushX;
        int deltaY = y - brushY;
        double distanceSquared = Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2); 
    
        // Current pixel lies inside the circle
        if (distanceSquared <= radiusSquared)
        {
            return brushColor;
        }
    
        return oldColor;
    });
    

    White 10*10 color grid with all pixels inside the circle set to a dark red

    You could also interpolate between the brush color and the old pixel. For example, you can implement a “soft” brush by letting the blend amount depend on the distance between the brush center and the current pixel.

    White 10*10 color grid with a soft dark red dot

    Drawing a line

    In order to draw a freehand line simply apply the brush repeatedly, each time with a different offset (depending on the mouse movement):

    Drawing a line by repeatedly applying a circular brush


    Custom image class

    I obviously skipped some necessary properties, methods and data validation, but you get the idea:

    public class Image
    {
        public Color[,] Pixels { get; private set; }
    
        public Image(int width, int height)
        {
            Pixels= new Color[width, height];
        }
    
        public void Initialize(Func<Color> createColor)
        {
             for (int x = 0; x < Width; x++)
             {
                 for (int y = 0; y < Height; y++)
                 {
                      Pixels[x, y] = createColor();
                 }
             }
        }
    
        public void Modify(Func<int, int, Color, Color> modifyColor)
        {
             for (int x = 0; x < Width; x++)
             {
                 for (int y = 0; y < Height; y++)
                 {
                      Color current = Pixels[x, y];
                      Pixels[x, y] = modifyColor(x, y, current);
                 }
             }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been using the following code to issue clicks programmatically on a Mac void
Update: This issue has been resolved. you can read about the solution in here:
I've encountered an issue whereby when I create an XML Document programmatically using the
How can I programmatically detect whether the current user has CREATE TABLE permissions in
Issue: RODBC (falsely) returning zero rows Situation: I'm using RODBC to connect to a
Issue Using the following just simply doesn't work properly in -webkit- and -moz- browsers:
Issue Garbage gets erroneously entered into date fields and screws up calculations. Goal Using
Issue In the below code , say if i want to change the color
I'm having an issue when trying to programmatically update a DateTime field that already
I'm running into an issue with Wicket generating markup for elements that programmatically need

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.