0

I'm creating a console app game and I would like to display some images on the screen. So, I found some tricks on google and I created that script to display an image :

[DllImport("user32.dll")] public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
    [DllImport("user32.dll", SetLastError = true)] public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
    [DllImport("kernel32.dll", EntryPoint = "GetConsoleWindow", SetLastError = true)] private static extern IntPtr GetConsoleHandle();

    private static void ShowImage(string filePath, int posX, int posY)
    {
        Image img = Image.FromFile(filePath);
        var form = new Form
        {
            FormBorderStyle = FormBorderStyle.None
        };

        var parent = GetConsoleHandle();
        var child = form.Handle;

        SetParent(child, parent);
        MoveWindow(child, 50, 50, img.Width, img.Height, true);

        form.Paint += delegate (object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Rectangle rc = new Rectangle(new Point(0, 0), img.Size);
            g.DrawImage(img, form.ClientRectangle, rc, GraphicsUnit.Pixel);
        };
        Application.Run(form);
    }

My problem is that, sometimes, I don't know why, my image doesn't appear ! And it's random, not 1 time on 2 or something like that !

Note : I use this code to display a png image

So, if someone know were is the problem, I'm ready to take notes :)

Aiixu
  • 29
  • 9
  • "console app game and I would like to display some images on the screen" And that is unfortunately where you went wrong. My best advise is: Stop trying. use a proper GUI or Gaming Display Technology. Simulate a console if you really need too. – Christopher Nov 02 '18 at 13:37
  • I can't because i'm doing a batch game, and the image would be the logo. I've already seen a batch game with images and the external command "affbmp" or something like that works perfectly.I made some test and I found the probleme : the Application.Run(form) line, it should be async I think, but I don't understand how to do that, all of the interesting topics are in english and i'm french :/ – Aiixu Nov 02 '18 at 13:40
  • 1
    If you want to show images in the console you need to control both the textual and graphical rendering; otherwise, the console may erase the graphical content when something changes. I had a similar project earlier, and I could not overcome every issue. Alternative consoles, such as ConEmu may help but as they are not official solutions will never be really supported. – György Kőszeg Nov 02 '18 at 13:43
  • 1
    @Aiixu: _i'm doing a batch game_ - now C# or batch? If the latter forget the images. You will not be able to call some DisplayImage.exe and keep the console consistent. Better if you convert the images into characters and display them in the console. See also this related post: https://stackoverflow.com/questions/33538527/display-a-image-in-a-console-application – György Kőszeg Nov 02 '18 at 13:47
  • No, I would like to do that in the base console, because if I do it in an emulator, that's useless, I want to make a game in cmd.exe because just to say "oh ! look ! I made that game in the base console" :D I'm doing a batch game with an external command to write in color and other stuff like that. About the calling, I'm doing it with a pipe and reading the console output to read the arguments, all works perfectly, but not the image displaying. About the conversion, I already tried but my windows is two little (the launcher), so that's looks weird. – Aiixu Nov 02 '18 at 13:53

0 Answers0