Click here to Skip to main content
15,883,883 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:

C#
namespace X
{
    public static partial class X11
    {
        ...

        [StructLayout(LayoutKind.Sequential)]
        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:
C#
[DllImport(libX11)]
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.
C#
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");
            }
            else
            {
                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);
                }
            }

            System.Threading.Thread.Sleep(15000);

            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 "//":
C#
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.

Thanks!

What I have tried:

Drawing from XDrawRectangles weird.
I have tried any possibilities - No success... even class or structure...
Posted
Updated 19-Nov-20 22:28pm
v3
Comments
[no name] 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
[no name] 19-Nov-20 16:19pm    
THEN PUT LINUX IN YOUR TAGS. Thank you.
[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
[no name] 20-Nov-20 4:29am    
You assume too much.

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