Click here to Skip to main content
14,924,621 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone,

I have tried to resolve with XDrawRectangles under C#:
Displaying = OK
Drawing = So bad!

How do I fix ff I have tried to created Rectangle as Structure for X11/Xlib?

Looks like this simple:

namespace X
    public static partial class X11

        public struct Rectangle
            public int X { get; set; }
            public int Y { get; set; }
            public uint W { get; set; }
            public uint H { get; set; }

            public Rectangle(int X, int Y, uint W, uint H)
                this.X = X; this.Y = Y;
                this.W = W; this.H = H;

            public override string ToString()
                return "X:" + X + " Y:" + Y + " Width:" + W + " Height:" + H;


And I have created p/invoke like this:
private extern static void XDrawRectangles(IntPtr display, IntPtr drawable, IntPtr gc, IntPtr rectangles, int number_rectangles);
public static void DrawRectangles(ref Display display, Drawable drawable, ref GContext gc, Rectangle[] rectangles, int number_rectangles)
    var size = Marshal.SizeOf(typeof(Rectangle));
    var rect = Marshal.AllocHGlobal(size);
    rectangles = new Rectangle[number_rectangles];
    for (int i = 0; i < number_rectangles; i++)
        IntPtr rect_ptr = new IntPtr(rect.ToInt64() + i * size);
        if (rect_ptr != IntPtr.Zero)
            rectangles[i] = (Rectangle)Marshal.PtrToStructure(rect_ptr, typeof(Rectangle));
        XDrawRectangles(display.Handle, drawable.Handle, gc.Handle, rect_ptr, number_rectangles);

And I have written example for drawing if drawing is not weird.
using System;
using X;

namespace XSharpTest
    class Program
        static void Main(string[] args)
            X11.Display display = X11.OpenDisplay(string.Empty);
            if (display.GetType() == null)
                Console.WriteLine("X11 doesn't load");
                Console.WriteLine("Test for X11");

            int screen_number = X11.DefaultScreen(ref display);
            Console.WriteLine("Screen number is " + Convert.ToString(screen_number));
            uint black = X11.BlackPixel(ref display, screen_number);
            Console.WriteLine("Black is " + Convert.ToString(black));
            uint white = X11.WhitePixel(ref display, screen_number);
            Console.WriteLine("White is " + Convert.ToString(white));
            X11.GContext context = X11.DefaultGC(ref display, screen_number);

            X11.Window root = X11.RootWindow(ref display, screen_number);
            X11.Window window = X11.CreateSimpleWindow(ref display, root, 100, 100, 200, 200, 0, black, white);

            X11.ClearWindow(ref display, window);
            X11.MapWindow(ref display, window);

            X11.Flush(ref display);

            while (!Convert.ToBoolean(X11.Pending(ref display)))
                X11.DrawRectangle(ref display, window, ref context, 20, 20, 160, 160);

                X11.Rectangle[] rectangles = new X11.Rectangle[40];

                for (int i = 0; i < 40; i++)
                    rectangles[i].X = 20 * i;
                    rectangles[i].Y = 20 * i;
                    rectangles[i].W = 20 * 2;
                    rectangles[i].H = 20 * 3;

                    //X11.DrawRectangle(ref display, window, ref context, rectangles[i].X, rectangles[i].Y, rectangles[i].W, rectangles[i].H);
                    X11.DrawRectangles(ref display, window, ref context, rectangles, i);


            X11.DestroyWindow(ref display, window);
            X11.CloseDisplay(ref display);

Looks picture and uuuh drawing is really weird.
Imgur: The magic of the Internet

Workaround with XDrawRectangle and multiple in for-statement
Replace with "//":
X11.DrawRectangle(ref display, window, ref context, rectangles[i].X, rectangles[i].Y, rectangles[i].W, rectangles[i].H);
//X11.DrawRectangles(ref display, window, ref context, rectangles, i);

Look picture like it works fine. Yeah but it is really impossible because other functions are unusable. It is really stupid of faked methods for X11/Xlib.

I really appreciate that X11/Xlib has real functions like real drawing...

Is it bug or Mono has problem. I should move to Dotnet 3?

Thank you for replying my improvement.

// EDIT!!
PS: Please stop using Windows 10 libraries!

I am using Linux/FreeBSD.


What I have tried:

Drawing from XDrawRectangles weird.
I have tried any possibilities - No success... even class or structure...
Updated 19-Nov-20 22:28pm
Gerry Schmitz 19-Nov-20 9:23am
Nobody is doing what you're doing. Switch to Windows Forms or WPF.
[no name] 19-Nov-20 12:42pm
PLEASE STOP USING WINDOWS - I AM NOT FAN OF WINDOWS. I AM ONLY LINUX / FREEBSD. Sorry I don't care Windows 10. Because Windows 10 manipulated since Update into after installation. I hate Windows 10. Please use UNIX/LINUX!!!!

Thanks Geny!

Best regards, Jens
Gerry Schmitz 19-Nov-20 16:19pm
[no name] 20-Nov-20 4:27am
Please check tag "X11" and "Mono" It means not using Windows 10 and I just use Linux/FreeBSD
Gerry Schmitz 20-Nov-20 4:29am
You assume too much.
CHill60 20-Nov-20 5:45am
What on earth does "Drawing = So bad!" actually mean?
[no name] 20-Nov-20 5:48am
CHill60 20-Nov-20 7:41am
That is a picture of multiple rectangles.
I am guessing that is not what you wanted it to look like.
Note my use of the word "guessing". Because we are trying to guess what you want from us.
You need to explain clearly what your problem is and what you want us to help with - otherwise we can't help
[no name] 20-Nov-20 11:04am
I think you don't know what is that? Stop forcing me!


I have tried to explain:
I have ported to C# but C# can't work if I use Rectangle[] and I have tried to fix or to resolve if Rectangles draw multiply like that.

This is from website of C/C++

But I use with multiple DrawRectangle()

Looks like "faked drawing":

It is not nice if I don't use "XDrawRectangles()" That is why I need resolve for XDrawRectangles()

I have tried code:

Looks uuuh....

But it crashes. How do I fix it? If I want marshal from Rectangle array to IntPtr.

That is reason.
[no name] 20-Nov-20 11:39am
I have tried.....

No ok...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900